Web applications 我的应用程序无法打开ServletContext资源
我有一个EclipseMavenWeb项目,我使用SpringMVC和SpringSecurity。 当我真的启动它时,它不需要初始化上下文:Web applications 我的应用程序无法打开ServletContext资源,web-applications,spring-mvc,spring-security,maven-eclipse-plugin,Web Applications,Spring Mvc,Spring Security,Maven Eclipse Plugin,我有一个EclipseMavenWeb项目,我使用SpringMVC和SpringSecurity。 当我真的启动它时,它不需要初始化上下文: 无法打开ServletContext资源[/WEB-INF/spring dispatcher servlet.xml] 详情如下: 这是我的web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSche
无法打开ServletContext资源[/WEB-INF/spring dispatcher servlet.xml]
详情如下:
这是我的
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Web Manager</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml, /WEB-INF/spring/security-config.xml
</param-value>
</context-param>
<!-- Spring MVC -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这是我的
security config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="fr.tessa.webmanager" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1000000" />
</bean>
<!-- Configures Hibernate - Database Config -->
<import resource="dbconfig/validationdb-config.xml" />
<import resource="dbconfig/db-config.xml" />
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http use-expressions="true">
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="mkyong" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
它说它找不到/WEB-INF/spring dispatcher servlet.xml
,但我没有把它放在WEB.xml
,我的配置文件是app config.xml
引用自:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="fr.tessa.webmanager" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="1000000" />
</bean>
<!-- Configures Hibernate - Database Config -->
<import resource="dbconfig/validationdb-config.xml" />
<import resource="dbconfig/db-config.xml" />
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http use-expressions="true">
<intercept-url pattern="/welcome*" access="ROLE_USER" />
<form-login login-page="/login" default-target-url="/welcome"
authentication-failure-url="/loginfailed" />
<logout logout-success-url="/logout" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="mkyong" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
初始化DispatcherServlet后,Spring MVC在WEB应用程序的WEB-INF目录中查找名为[servlet name]-servlet.xml的文件,并创建在其中定义的bean
您的servlet名为spring dispatcher
,因此它查找/WEB-INF/spring dispatcher servlet.xml
。您需要有这个servlet配置,并在其中定义与web相关的bean(如控制器、视图解析器等)。有关servlet上下文与全局应用程序上下文(在您的示例中是app config.xml
)之间关系的说明,请参阅链接文档
还有一件事,如果您不喜欢servlet配置xml的命名约定,可以显式指定配置:
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
春季调度员
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
如果您在Java配置中遇到此错误,通常是因为您忘记将应用程序上下文传递给DispatcherServlet
构造函数:
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebConfig.class);
ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher",
new DispatcherServlet()); // <-- no constructor args!
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
AnnotationConfigWebApplicationContext ctx=新的AnnotationConfigWebApplicationContext();
注册(网络配置类);
ServletRegistration.Dynamic dispatcher=sc.addServlet(“dispatcher”,
新DispatcherServlet());// 我在WebLogic中遇到了这个异常,原来它是WebLogic中的一个bug。有关更多详细信息,请参见此处:在构建war时,确保pom.xml中的maven war插件块包含所有文件(尤其是xml文件)。但是您不需要包含.java文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webResources>
<resources>
<directory>WebContent</directory>
<includes>
<include>**/*.*</include> <!--this line includes the xml files into the war, which will be found when it is exploded in server during deployment -->
</includes>
<excludes>
<exclude>*.java</exclude>
</excludes>
</resources>
</webResources>
<webXml>WebContent/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
org.apache.maven.plugins
maven战争插件
2.5
网络内容
**/*.*
*.爪哇
WebContent/WEB-INF/WEB.xml
您使用的文件名spring-dispatcher-servlet.xml
请登录web.xml
标记和标记处作为spring调度程序的servlet名称
你的情况应该是这样的
春季调度员
春季调度员
谢谢Zagyi,我从context param中删除了“/WEB-INF/spring/app config.xml”,并在servlet init param中导出,它工作了,+1感谢您的回复:-)这正是我的案例,4天,这很有帮助。谢谢记录在案。帮助在标准spring boot应用程序上部署wildfly 10 openshift。还可以尝试在提供的maven javax.servlet javax.servlet-api中设置
The file name u used spring-dispatcher-servlet.xml
kindly check in web.xml
servlet name as spring-dispatcher at both tag <servlet> and <servlet-mapping>
in your case it should be
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class></servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>