您的位置:首页 > 编程语言 > Java开发

maven项目管理 集成常见错误一解决:spring 在启动服务器的时候报错 Could not open ServletContext resource [/WEB-INF/dispather-se

2017-05-24 16:51 711 查看
解决:spring 在启动服务器的时候报错 Could not open ServletContext resource [/WEB-INF/dispather-servlet.xml]

程序使用文件:spring-bispathcher-servelt.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- HandlerMapping 处理器映射 -->
<!-- BeanNameUrlHandlerMapping:表示将请求的URL和Bean名字映射,如URL为 “上下文/hello”,
则Spring配置文件必须有一个名字为“/hello”的Bean,上下文默认忽略。 -->

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

<!-- HandlerAdapter 处理器适配-->
<!-- SimpleControllerHandlerAdapter:表示所有实现了org.springframework.web.servlet.mvc.Controller接口
的Bean可以作为Spring Web MVC中的处理器。如果需要其他类型的处理器可以通过实现HandlerAdapter来解决。 -->

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

<!-- ViewResolver 视图解析器 -->
<!-- InternalResourceViewResolver:用于支持Servlet、JSP视图解析;
viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包;
prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,
则该该sp视图页面应该存放在“WEB-INF/jsp/hello.jsp”; -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
<property name="suffix" value=".jsp" />
</bean>

<!-- 处理器 -->
<bean name="/hello"
class="controller.HellowordController" />
</beans>

程序执行:加载web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<display-name>maven Dome</display-name>

<servlet>
<servlet-name>dispathServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。
如:dispathServlet-servlet.xml
-->
<!-- 指定spring配置文件在classpath目录下 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-dispathcher-servelt.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispathServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web一般情况下,程序在执行时,会通过加载web.xml文件中的servlet,通过属性设置,读取spring-dispathcher-servlet.xml文件,以此关联springMVC的controller文件,但是在运行时不尽其然,系统报出Could not open ServletContext resource [/WEB-INF/dispather-servlet.xml] 的错误,但我们的配置文件中却没有dispather-servlet,xml文件;那为什么会加载dispatcher-servlet.xml文件呢?因为dispather-servlet.xml是程序默认去查找的文件,所以当程序执行过程中,没有去加载我们配置的spring-dispathcher-servelt.xml文件,主要原因是由于pom.xml中配置plugins
tomcat插件引起的,与eclipse中的tomcat相冲突;
pom.xml文件

<build>
<finalName>mavenDome</finalName>
<!-- 配置tomcat插件 -->
<!-- <plugins> -->
<!-- <plugin> -->
<!-- <groupId>org.apache.tomcat.maven</groupId> -->
<!-- <artifactId>tomcat7-maven-plugin</artifactId> -->
<!-- <version>2.2</version> -->
<!-- </plugin> -->
<!-- </plugins> -->
</build>

解决方案:
一:将pom.xml配置文件中的plugins tomcat 配置插件的代码注掉,然后执行maven clean一下,再重新编译即可;

二:设置run configration



点击左侧的maven Build 设置,使用tomcat:run指令运行tomcat插件



然后在运行maven test即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven 集成错误
相关文章推荐