コントローラーから別のコントローラーにリダイレクトしています。ただし、モデル属性を 2 番目のコントローラーに渡す必要もあります。
モデルをセッションに入れたくありません。
助けてください。
コントローラーから別のコントローラーにリダイレクトしています。ただし、モデル属性を 2 番目のコントローラーに渡す必要もあります。
モデルをセッションに入れたくありません。
助けてください。
私はSpring 3.2.3を使用していますが、同様の問題を解決した方法は次のとおりです。
1) コントローラー 1 のメソッド パラメーター リストに RedirectAttributes redirectAttributes を追加しました。
public String controlMapping1(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model,
final RedirectAttributes redirectAttributes)
2) メソッド内に、redirectAttributes に flash 属性を追加するコードを追加しました。redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
3) 次に、2 番目のコントローラーで @ModelAttribute アノテーションが付けられたメソッドパラメーターを使用して、リダイレクト属性にアクセスします。
@ModelAttribute("mapping1Form") final Object mapping1FormObject
コントローラ 1 のサンプル コードは次のとおりです。
@RequestMapping(value = { "/mapping1" }, method = RequestMethod.POST)
public String controlMapping1(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model,
final RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("mapping1Form", mapping1FormObject);
return "redirect:mapping2";
}
コントローラ 2 から:
@RequestMapping(value = "/mapping2", method = RequestMethod.GET)
public String controlMapping2(
@ModelAttribute("mapping1Form") final Object mapping1FormObject,
final BindingResult mapping1BindingResult,
final Model model) {
model.addAttribute("transformationForm", mapping1FormObject);
return "new/view";
}
それを行う最もエレガントな方法は、SpringMVCにカスタムFlashScopeを実装することだと思います。
フラッシュスコープの主なアイデアは、1つのコントローラーからのデータを2番目のコントローラーにリダイレクトするまで保存することです。
カスタムスコープの質問に関する私の回答を参照してください。
このコードに欠けているのは、次のxml構成だけです。
<bean id="flashScopeInterceptor" class="com.vanilla.springMVC.scope.FlashScopeInterceptor" />
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list><ref bean="flashScopeInterceptor"/></list>
</property>
</bean>
org.springframework.web.servlet.mvc.support.RedirectAttributes を使用して解決できます。
これが私のコントローラーのサンプルです。
@RequestMapping(method = RequestMethod.POST)
public String eligibilityPost(
@ModelAttribute("form") @Valid EligibiltyForm form,
Model model,
RedirectAttributes redirectAttributes) {
if(eligibilityService.validateEligibility(form)){
redirectAttributes.addFlashAttribute("form", form);
return "redirect:<redirect to your page>";
}
return "eligibility";
}
http://mayurshah.in/596/how-do-i-redirect-to-page-keeping-model-valueの私のブログで詳細をお読み ください
すべての属性をリダイレクトに渡すだけの場合...
public String yourMethod( ...., HttpServletRequest request, RedirectAttributes redirectAttributes) {
if(shouldIRedirect()) {
redirectAttributes.addAllAttributes(request.getParameterMap());
return "redirect:/newPage.html";
}
}
を使用@ControllerAdviceしました。Spring 3.X で利用可能であることを確認してください。Spring 4.0で使用しています。
@ControllerAdvice
public class CommonController extends ControllerBase{
@Autowired
MyService myServiceInstance;
@ModelAttribute("userList")
public List<User> getUsersList()
{
//some code
return ...
}
}
すべてのモデル属性をリダイレクト URL にクエリ文字列として追加します。