1

我是 Spring 框架的新手,非常感谢您对我遇到的问题的指导。我正在开发基于 Spring 框架的 Web 服务,我已经在我的本地 wrkspace 上单独开发了它,现在我正在尝试将它与我现有的 Web 应用程序集成,该应用程序基本上使用旧的 jsps,没有特定的框架。我的应用程序托管在 tomcat 上,其原始 web.xml 为:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<display-name>BOBCAT</display-name>
<description>
BOBCAT
</description>

<servlet>
    <servlet-name>DFPServlet</servlet-name>
    <servlet-class>uk.co.blackwell.dfp.DFPServlet</servlet-class>
    <init-param>
     <param-name>properties</param-name>
    <param-value>/opt/bobcat/dev2/apprunner/webapps/bobcat/WEB-INF/classes/uk/co/blackwell/dfp/dfpprops.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>DFPServlet</servlet-name>
    <url-pattern>*.dfp</url-pattern>
</servlet-mapping>

<!-- Cannot redirect to static apache-served pages -->
<!-- File not found (404),  unauthorized/forbidden (403, 401) -->
<error-page>
    <error-code>401</error-code>
    <location>/not_found.jsp</location>
</error-page>   
<error-page>
    <error-code>403</error-code>
    <location>/not_found.jsp</location>
</error-page>   
<error-page>
    <error-code>404</error-code>
    <location>/not_found.jsp</location>
</error-page>   
</web-app>

我想在这个旧版本中集成的新 web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

    <servlet>
    <servlet-name>restful</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/restful-context.xml</param-value>            
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>restful</servlet-name>
    <url-pattern>/restful/*</url-pattern>
</servlet-mapping>    
</web-app>

我想我在定义新的 web.xml 本身时可能做错了什么,因为当我将块从 spring 版本放到旧版本时,它给了我所有请求的 404 not found 错误。我已经尝试咨询了很多在线资源,但其中大多数要么从头开始支持新的 Spring 应用程序,要么在某些意义上是不完整的。任何帮助或努力将不胜感激。

4

1 回答 1

0

尝试了几种组合,发现实际上比想象的要容易。我在这里没有使用任何侦听器,因为当我尝试使用侦听器时,它会为所有请求提供 404 not found 警告。我现在可以摆脱它,因为我只需要使用 Spring 将 Restful Web 服务与我的旧 Web 应用程序集成。它现在工作得很好,无论是在我的本地工作区(Win 7)还是 linux 开发盒上。

最终的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>BOBCAT</display-name>
<description>
BOBCAT
</description>

<!-- Added on 20/03/2013 by Anshul for Spring -->
<servlet>
    <servlet-name>restful</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/restful-context.xml</param-value>            
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>restful</servlet-name>
    <url-pattern>/restful/*</url-pattern>
</servlet-mapping>
<!-- End of addition by Anshul -->


<servlet>
    <servlet-name>DFPServlet</servlet-name>
    <servlet-class>uk.co.blackwell.dfp.DFPServlet</servlet-class>
    <init-param>
     <param-name>properties</param-name>
    <param-value>/opt/bobcat/dev2/apprunner/webapps/bobcat/WEB-INF/classes/uk/co/blackwell/dfp/dfpprops.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>DFPServlet</servlet-name>
    <url-pattern>*.dfp</url-pattern>
</servlet-mapping>


<!-- Cannot redirect to static apache-served pages -->
<!-- File not found (404),  unauthorized/forbidden (403, 401) -->
<error-page>
    <error-code>401</error-code>
    <location>/not_found.jsp</location>
</error-page>   
<error-page>
    <error-code>403</error-code>
    <location>/not_found.jsp</location>
</error-page>   
<error-page>
    <error-code>404</error-code>
    <location>/not_found.jsp</location>
</error-page>   

</web-app>
于 2013-04-04T14:36:27.443 回答