我是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找不到所有请求的错误。我曾尝试咨询大量的在线资源,但大多数都是从头开始支持新的Spring应用程序,或者在某种意义上是不完整的。任何帮助或努力都将深受赞赏。
答案 0 :(得分:0)
尝试了几种组合,发现它实际上比思想容易。我没有在这里使用任何监听器,因为当我尝试使用监听器时,它为所有请求提供404未找到警告。我现在可以逃脱它,因为我只需要使用Spring和我的遗留Web应用程序集成restful 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>