小编典典

Tomcat无法在War文件中找到JSP

jsp

最近,我将我的静态html文件之一切换到了使用JSP呈现其视图的Spring控制器。我使用码头进行本地测试,本地测试显示页面渲染良好。部署到使用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>

front-controller-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>

开发实例和测试实例之间的区别在于,开发实例部署到了根目录(localhost /
search.html),而测试实例则部署到了server.com/appname/search.html。Appname是我部署的*
.war文件的名称。我尝试将完整路径作为前缀添加到jps文件(/ appname / WEB-INF / jsp
/)和许多其他没有运气的组合中。我已经验证了jsp-api jar位于tomcat lib目录中。我的tomcat安装是基本/默认安装。


阅读 260

收藏
2020-06-08

共1个答案

小编典典

该问题是由于从tomcat安装的默认web.xml中删除了默认的jsp
servlet引起的。这是由一位前同事完成的,而据我所知。将测试服务器的web.xml替换为默认的web.xml(包括用于处理jsp文件的url-
patterns / servlet)可以解决此问题。

2020-06-08