【问题标题】:Redirecting from a legacy Servlet to Spring 3 and from Spring 3 to a legacy Servlet从旧版 Servlet 重定向到 Spring 3 以及从 Spring 3 重定向到旧版 Servlet
【发布时间】:2012-05-17 02:48:52
【问题描述】:

我通过将 Spring 3 集成到旧版 Servlet 应用程序中并逐步转换旧版应用程序来学习 Spring。

下面发布了一个类似于我正在使用的 web.xml *-servlet.xml。基本上,事情是这样设置的,检索像“search”这样的字符串,Spring 会将其路由到控制器,视图解析器会将“search”转换为“/jsp/search.jsp”

我在从旧版 Servlet 和旧版 ServletFilter 到 Spring 执行 response.sendRedirect("search") 时遇到了问题。 URL 正确显示,但尽管 System.out.println() 调用表明已到达 JSP,但我得到了一个空白页面。 Spring 没有错误消息,浏览器只告诉我重定向出了点问题。

我通过转发解决了这个问题,而不是从旧版 Servlet 和 ServletFilters 重定向:

request.getRequestDispatcher("search").forward(request,response);

getServletConfig().getServletContext().getRequestDispatcher("search").forward(request,response);

从 Spring 3 中完成的新 JSP 到旧版 Servlet 的另一个方向,我在屏幕上有按钮,所以我只使用了对“location.href=/helloworld”的 javascript 调用。我需要发送一些参数,所以我可能会将这些按钮转换为提交微小的 HTML 表单。

我想知道的是,是否有更好的方法让 Spring 3 和 Legacy Servlet 以更好的方式进行通信。

旧版 Servlet => Spring 3

和

Spring 3 => 旧版 Servlet

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app  id="WebApp_ID" 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>Acme</display-name>

  <!--welcome-file-list>
    <welcome-file>/login</welcome-file>
  </welcome-file-list-->

  <servlet>
    <servlet-name>acme</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>acme</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <!-- Help Find The Spring Config Files -->
  <listener>
    <listener-class>
                  org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            /WEB-INF/nsd-servlet.xml,
            /WEB-INF/nsd-security.xml
    </param-value>
  </context-param>


  <!-- Spring Security -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <!-- Integrate A Legacy Screen Done With A Servlet -->
  <servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>
            com.legacy.HelloWorldServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/helloworldservlet</url-pattern>
  </servlet-mapping>

</web-app>

我的 acme-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


  <context:component-scan base-package="com.acme.controller" />

  <mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/acme/common/,file:/c:/weblogic_common_files/acme/"/>
  <mvc:annotation-driven/>

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name = "prefix" value = "/jsp/"/>
    <property name = "suffix" value = ".jsp"/>
  </bean>


</beans>

【问题讨论】:

    标签: spring servlets integration


    【解决方案1】:

    当您使用response.sendRedirect(String url) 时,您实际上是在传递一个您想要重定向到的 URL。

    sendRedirect 遵循以下规则:

    /*
    * Destination, it can be any relative or context specific path.
    * if the path starts without '/' it is interpreted as relative to the current request URI.
    * if the path starts with '/' it is interpreted as relative to the context.
    */
    
    String destination  ="/jsp/destination.jsp";        
    response.sendRedirect(response.encodeRedirectURL(destination));
    

    当您输入“搜索”时,这是相对于当前请求的 URI。因此,除非您当前的请求是针对 /acme/something,并且假设 /acme/search 是您的 servlet,否则请求将失败。

    但是,如果您使用 /acme/search 放置相对于根的路径,则该请求将在任何上下文中工作。

    话虽如此,我不相信这真的是最好的方法,因为重定向涉及将响应发送回客户端浏览器,告诉它再次从另一个 URL 获取内容。这似乎是一次往返服务器的浪费之旅。

    更好的方法可能是使用 Spring MVC 之类的东西将 servlet 连接到 Spring。它非常灵活,您可以将新的控制器类包装在现有的 servlet 周围,然后通过传入 HttpServletRequest 和 HttpServletResponse 对象直接调用它们。一旦完成,您就可以慢慢消除任何多余的东西,使用一个有效的系统,而不是一个与重定向链接在一起的系统。

    package samples;
    
    public class SampleController extends AbstractController {
    
        private int cacheSeconds;
    
        // for wiring in cacheSeconds
        public void setCacheSeconds(int cacheSeconds) {
            this.cacheSeconds = cacheSeconds; 
        }
        public int getCacheSeconds() {
            return cacheSeconds;
        }
    
        public ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    
            // you now have a Spring Controller wired in, and you could delegate to 
              // your legacy servlet in this manner
            YourServlet servlet = new YourServlet();
    
    
            servlet.doGet(request, response);
    
            ModelAndView mav = new ModelAndView("hello");
            mav.addObject("message", "Hello World!");
            return mav;        
        }
    }
    
    <bean id="sampleController" class="samples.SampleController">
        <property name="cacheSeconds" value="120"/>
    </bean>
    

    请注意,手动实例化 servlet 并不是真正的好习惯或好设计。相反,这更像是让您的 Spring 控制器连接和连接的垫脚石,这样您就可以系统地重构您的代码并通过将您的业务逻辑移动到服务层来消除遗留的 servlet。

    这种方法与您计划系统地迁移到 Spring 并了解更多关于 Spring 的计划相吻合,解决一些技术债务,同时仍然致力于与您的网站相关的业务目标。

    【讨论】:

    • 哇。谢谢jmort253。这是 Spring 3 => Legacy Servlet 方面的一个非常聪明的解决方案。拥有这样的包装器可以让我使用更多的 Spring 来处理事情,然后再将事情发送到遗留代码。
    • 不客气。我还只是认为您甚至可以将 Legacy servlet 作为 bean 属性进行连接,就像使用 cacheSeconds bean 一样。这将消除每次调用控制器时实例化它的需要。不是 100% 确定是否有任何理由每次都实例化,所以如果你愿意,我将把它作为练习留给自己尝试:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多