42

コントローラーから別のコントローラーにリダイレクトしています。ただし、モデル属性を 2 番目のコントローラーに渡す必要もあります。

モデルをセッションに入れたくありません。

助けてください。

4

10 に答える 10

114

私は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";  
}
于 2013-06-27T14:44:48.710 に答える
14

それを行う最もエレガントな方法は、SpringMVCにカスタムFlashScopeを実装することだと思います。

フラッシュスコープの主なアイデアは、1つのコントローラーからのデータを2番目のコントローラーにリダイレクトするまで保存することです。

カスタムスコープの質問に関する私の回答を参照してください。

SpringMVCカスタムスコープBean

このコードに欠けているのは、次の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>
于 2011-09-15T11:25:52.203 に答える
4

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の私のブログで詳細をお読み ください

于 2016-05-10T08:58:24.760 に答える
1

すべての属性をリダイレクトに渡すだけの場合...

public String yourMethod( ...., HttpServletRequest request, RedirectAttributes redirectAttributes) {
    if(shouldIRedirect()) {
        redirectAttributes.addAllAttributes(request.getParameterMap());
        return "redirect:/newPage.html";
    }
}
于 2017-08-04T22:44:09.200 に答える
0

を使用@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 ...
    }
}
于 2015-10-29T02:54:08.760 に答える
0

多分あなたはこのようにすることができます:

最初のコントローラーでモデルを使用しないでください。他の共有オブジェクトにデータを保存し、2 番目のコントローラーで取得できるようにします。

これこの投稿を見てください。似たような問題についてです。

PS

おそらく、その共有データにセッションスコープのBeanを使用できます...

于 2011-09-15T11:15:59.763 に答える
-1

すべてのモデル属性をリダイレクト URL にクエリ文字列として追加します。

于 2011-09-15T11:12:45.150 に答える