gpt4 book ai didi

java - 将 Spring 服务与旧的 jsp Web 应用程序集成

转载 作者:行者123 更新时间:2023-12-01 04:46:10 25 4
gpt4 key购买 nike

我是 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 中的新 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 版本中的 block 放入旧版本时,它会给我所有请求 404 未找到错误。我尝试咨询很多在线资源,但其中大多数要么从头开始支持新的 Spring 应用程序,要么在某些意义上不完整。任何帮助或努力将不胜感激。

最佳答案

尝试了几种组合,发现其实比想象的要容易。我在这里没有使用任何监听器,因为当我尝试使用监听器时,它对所有请求发出 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>

关于java - 将 Spring 服务与旧的 jsp Web 应用程序集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com