2016-05-10 25 views
0

我使用Spring來創建和下載Excel表我想添加一些變量模型requestiong映射方法,這樣我可以在其他請求馬平方法如何在不使用會話的情況下在spring mvc中共享@RequestMapping方法中的模型對象?

@RequestMapping("/contentUploadDetails/{content_type}/{date}") 
public ModelAndView contentUpload(
     @PathVariable(value = "content_type") String content_type, 
     @PathVariable(value = "date") String date) { 
    List<CountAndValue> ls = contentCountImp 
      .getuploadedContentCountDatewise(content_type, date); 
    model.addObject("CountAndValue", ls); 
    return model; 
} 

使用正如你可以看到上面

model.addObject("CountAndValue", ls); 

我想在我的其他requestMapping的方法來使用這個模型值

@RequestMapping(value = "/ContentUploadExport", method = RequestMethod.GET) 
public ModelAndView getExcel() { 

    return new ModelAndView("CountAndValueExcel", "CountAndValue", CountAndValue); 
} 

我如何使用CountAndValueExcel模型對象在第二種方法中使用會話通過第一種方法設置?我可以將模型對象(包含類對象列表)從視圖發送回控制器嗎?

+0

假設你從一個控制器重定向到另一個控制器,你可以使用flash屬性,checkout - http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-flash-屬性 –

回答

0

可以保存對象到一個會話:

@RequestMapping("/contentUploadDetails/{content_type}/{date}") 
public ModelAndView contentUpload(HttpServletRequest request, 
     @PathVariable(value = "content_type") String content_type, 
     @PathVariable(value = "date") String date) { 
    List<CountAndValue> ls = contentCountImp 
      .getuploadedContentCountDatewise(content_type, date); 
    model.addObject("CountAndValue", ls); 
    request.getSesion().setAttribute("CountAndValue", ls); 
    return model; 
} 

然後你找回它是這樣的:

@RequestMapping(value = "/ContentUploadExport", method = RequestMethod.GET) 
public ModelAndView getExcel(HttpServletRequest request) { 
    List<CountAndValue> CountAndValue = (List<CountAndValue>) request.getSession().getAttribute("CountAndValue"); 
    return new ModelAndView("CountAndValueExcel", "CountAndValue", CountAndValue); 
} 

寫它從我的頭,沒有測試。

+0

thanx的回覆我已經完成了會議其工作,但有任何其他方式,如果dnt想要使用會話變量 –

+1

恐怕不是因爲你不能保持兩個請求之間的狀態,而不使用服務器上的對象側。它也可能是定製的東西,但最好的方法是與會議一起去。我也不是會議的忠實粉絲,但有時對於某個問題沒有其他辦法。也許它也可能是一個設計問題,如果不深入洞察用例就無法分辨。 – localhost

相關問題