Spring mvc重定向到不同的jvm上的不同应用程序

时间:2016-06-28 06:31:07

标签: spring-mvc

我的要求是在不同JVM中的两个不同应用程序之间进行重定向。并且还在两者之间传输数据。我尝试使用Flash属性,但在控制器中,属性为null。我也试过创建一个拦截器,但即使闪存属性为null也是如此。任何人都可以帮助我如何在两个不同的应用程序之间传递属性?

这是我的代码: poc1 - 调用应用程序

调度-servlet.xml中

    <context:component-scan base-package="controller" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean name="flashMapManager" class="org.springframework.web.servlet.support.SessionFlashMapManager" />
    <mvc:annotation-driven />
</beans>

Controller.java

 @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@ModelAttribute("customer") Customer customer,
            final RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("customer", customer);
        redirectAttributes.addFlashAttribute("message", "Added successfully.");
        return "redirect:http://localhost:8080/poc2";
    }

poc2 - 调用应用程序

调度-servlet.xml中

<context:component-scan base-package="controller" />
    <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean name="flashMapManager"
    class="org.springframework.web.servlet.support.SessionFlashMapManager" />
    <!-- <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
        <property name="interceptors"> <list> <ref bean="requestInterceptor" /> </list> 
        </property> </bean> -->
    <bean id="requestInterceptor" class="RequestInterceptor" />
    <mvc:annotation-driven />
    <mvc:interceptors>
        <ref bean="requestInterceptor" />
    </mvc:interceptors>
</beans>

Controller.java

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index(Model model, HttpServletRequest request,
        HttpSession session) {
    Map<String, ?> inputFlashMap = RequestContextUtils
            .getInputFlashMap(request);
    Customer cust1 = (Customer) model.asMap().get("customer");
    Customer cust = (Customer) inputFlashMap.get("customer");
    ModelAndView modelMap = new ModelAndView("showCustomer");
    System.out.println("Calling controller");
    return modelMap;
}

1 个答案:

答案 0 :(得分:0)

你可以用这个, 仅使用redirectAttributes.addFlashAttribute(...) -> "redirect:..."也可以,不必“重新插入”模型属性。