spring – Tomcat无法在war文件中找到jsp
作者:互联网
我最近将一个静态html文件切换到Spring控制器,该控制器使用JSP来呈现其视图.我使用jetty进行本地测试,本地测试显示页面渲染正常.部署到使用Tomcat 6.0.26的测试服务器后,我收到以下异常:
javax.servlet.ServletException: Could not get RequestDispatcher for [/WEB-INF/jsp/index.jsp]: check that this file exists within your WAR
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:219)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我已经确认JSP存在于我部署的战争中以及tomcat在部署时创建的展开目录.这是我的web.xml和front-controller-servlet.xml文件分别是什么样的(web.xml稍微缩短):
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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_2_5.xsd">
<!-- Initialize the Spring DispatcherServlet -->
<servlet>
<servlet-name>gwtrpc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map the DispatcherServlet to only intercept RPC requests -->
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>*.gwtrpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/search.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>front-controller</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
前端控制器-servlet.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"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/search.html">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="com.company.search.web.server.controller.IndexController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
我的dev实例和测试实例之间的区别在于dev实例部署到root(localhost / search.html),而测试实例部署到server.com/appname/search.html. Appname是我部署的* .war文件的名称.我已经尝试添加完整路径作为jps文件(/ appname / WEB-INF / jsp /)的前缀和许多其他组合,没有运气.我已经验证了jsp-api jar是在tomcat lib目录中.我的tomcat安装是基本/默认安装.
解决方法:
问题是由默认的jsp servlet从tomcat安装的默认web.xml中删除引起的.这是由一位前同事完成的,而不是我所知道的.使用默认的web.xml(包括用于处理jsp文件的url-patterns / servlet)替换测试服务器的web.xml更正了该问题.
标签:jsp,spring,tomcat,war,web-xml 来源: https://codeday.me/bug/20190710/1422179.html