2016-05-10 4 views
0

私はあなたがセッションを使わずにspring mvcの@RequestMappingメソッドでモデルオブジェクトを共有する方法は?

上で見ることができるように、私は他のリクエストmaping方法

@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; 
} 

で使用できるように、マッピング方法をrequestiongにモデルにいくつかの変数を追加したいExcelシートを作成し、ダウンロードして春を使用しています

model.addObject("CountAndValue", ls); 

私は私の他のrequestMapping方法私はあることCountAndValueExcelモデルオブジェクトを使用する方法

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

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

でこのモデルの値を使用したいですセッションを使用して第2の方法で第1の方法によって設定されるか?モデルオブジェクト(クラスオブジェクトのリストを含む)をビューからコントローラに返すことはできますか?

+0

あるコントローラから別のコントローラにリダイレクトすると仮定すると、フラッシュ属性、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

ありがとうございました。私は既にセッションでそれを行っていますが、セッション変数を使用したい場合には他の方法があります –

+1

サーバ上のオブジェクトを使わずに2つのリクエスト間で状態を保存できないので、側。それはカスタムメイドのものかもしれませんが、最良の方法はセッションをとることです。私はセッションのファンではありませんが、時には特定の問題のための他の方法はありません。たぶんそれはまた設計上の問題かもしれません、ユースケースについてのより深い洞察をすることなく伝えることはできません。 – localhost

関連する問題