2016-06-28 52 views
0

我的需求是在不同的JVM中的兩個不同的應用程序之間重定向。並且還可以在兩者之間傳輸數據。我嘗試使用Flash屬性,但在控制器中,屬性爲空。我也嘗試創建攔截器,但即使在那裏,flash屬性也是空的。任何人都可以幫助我如何在兩個不同的應用程序之間傳遞屬性?Spring mvc重定向到不同的應用程序在不同的jvm上

這裏是我的代碼: 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 - 稱爲應用

dispatcher- 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; 
} 
+0

請分享您的解決方法。 –

+0

這是沒有主骨架的問題。分享您的工作目錄,讓Doc可以查看它。 –

+0

解決方法是使用彈簧重定向和休息服務來傳輸數據。 – ggelect

回答

0

你可以使用這個, 只需使用redirectAttributes.addFlashAttribute(...) -> "redirect:..."工作爲好,沒得 「重新插入」 模型屬性。

+0

它在重定向到同一應用程序中的另一個控制器時起作用。但是,當移動到不同的應用程序時,flash屬性始終爲空。 – ggelect

+0

@RequestMapping(值= 「addcustomer」,方法= RequestMethod.POST) \t公共字符串addCustomer(@ModelAttribute( 「顧客」)的客戶的客戶, \t \t \t最終RedirectAttributes redirectAttributes){ \t \t redirectAttributes.addFlashAttribute(」顧客「,顧客); \t \t redirectAttributes.addFlashAttribute(「message」,「Added added。」); \t \t return「redirect:showcustomer.html」; \t \t} – Rajesh

+0

我已經嘗試過它,它的工作原理,但它是爲同一個應用程序。但對於不同的應用程序,這是行不通的,當我返回「重定向:http:// localhost:8080/zpoc2」;並從「poc1」重定向到另一個應用程序「poc2」,則數據丟失。 – ggelect

相關問題