ModelAndViewを使用したSpringMVCリダイレクト



Spring Mvc Redirect Using Modelandview



1、サーブレットは前方にリダイレクトし、リダイレクトします。

サーブレットリダイレクタを使用するには、2つの方法があります。1つは転送、もう1つはリダイレクトです。 forwardは、内部サーバーがクライアントを、現在のリクエストをリダイレクトする場所がわからないサーバーにリダイレクトすることです。URLURLアドレスバーと以前に変更なしでアクセスしました。リダイレクトはクライアントをリダイレクトし、サーバーは現在のリクエストを返し、次に指定された状態に戻り、リクエストを別のURLに戻すように指示します。特定のパフォーマンスは、URLアドレスバーを新しいURLに変換することです。

2、ModelAndViewリダイレクト:

ModelAndViewビューを返すために通常使用されるSpringMVCを使用します。 ModelAndViewは、実際には2つの方法でサーブレットリダイレクトをサポートしています。たとえば、次のコードがリダイレクトリダイレクトであるように、通常はリダイレクトページ404リダイレクト方法を使用します。



public ModelAndView getPage404MV() { ModelAndView mv = new ModelAndView('redirect:/404.htm') return mv }

これを使用するには、単にリダイレクトして転送先にリダイレクトします。特に、デフォルトで転送ModelAndViewリダイレクトモードを使用します。


  1. 需要の背景
    要件:Spring MVCフレームワークコントローラー間でジャンプし、リダイレクトされます。いくつかの状況があります:引数なしでジャンプする、引数がスプライスされたフォームのURLジャンプパラメーターがステッチパラメーターなしでジャンプする、ページを表示することができます。

もともと私は非常に単純なことを考えていましたが、個人的にはもっと一般的な方法はすべてBaiduを持っていると思いますが、これは問題ではありませんが、Baiduは実際に私を驚かせました。どうしようもないああ、結局のところみんなの百度の比較を書くために、ハハハ。 。 。これらはすべて書いているわけではなく、人々は私にこのブログを書く力を与えてくれました。
2.解決策
需要は間違いなく解決策であり、すべての解決策は、進行中のジャンプスプリングを説明します。私はここで、素敵で人気のあることを言います。セルフパッキングのクラスと方法のいくつかはスプリングを信じています。



(1) I jump in the background a controller to another controller, why there is such a demand for it, is this. I have a list page, then I will carry out new operations, new backstage after completing my list to jump to the page, do not need to pass parameters, the default page lists all the queries. Method 1: Use ModelAndView return new ModelAndView('redirect:/toList') So this method can be redirected to toList Second way: return String return 'redirect:/ toList ' Otherwise: There are many other ways, this is no longer done introduced, for example, response and so on. This is without redirection parameters. (2) the second case, the page has a list of query, the query after the jump I can not lose, so you need to bring arguments, and arguments can be spliced ​​url One way: to manually splicing url new ModelAndView('redirect:/toList?param1='+value1+'¶m2='+value2) This has a flaw, that might have passed Chinese garbage problem. A class with RedirectAttributes, this is found in a more useful: Second way AddAttribute method using it here, in fact, after this past redirect you look url, it automatically gives you fight your url. Instructions: attr.addAttribute('param', value) return 'redirect:/namespace/toController' Thus this parameter can be obtained by means of the parameters obtained in this method toController, and then transferred to the page. The past and the way a url or the same. (3) arguments are not spliced ​​url pages can get value (emphasis on this) I want to redirect to a general estimate in this way: @RequestMapping('/save') public String save(@ModelAttribute('form') Bean form,RedirectAttributes attr) throws Exception { String code = service.save(form) if(code.equals('000')){ attr.addFlashAttribute('name', form.getName()) attr.addFlashAttribute ( 'success', 'Add success!') return 'redirect:/index' }else{ attr.addAttribute('projectName', form.getProjectName()) attr.addAttribute('enviroment', form.getEnviroment()) attr.addFlashAttribute ( 'msg', 'Add error error code:!' + rsp.getCode () getCode () + ', with error:'.. + rsp.getCode () getName ()) return 'redirect:/maintenance/toAddConfigCenter' } }

@RequestMapping(“ / index”)

public String save(@ModelAttribute('form') Bean form,RedirectAttributes attr) throws Exception { return 'redirect:/main/list' }

ページ値私はそれを言いません、あなたは直接el式に行くことができます、ここでの原則はセッションを置くことです、セッションはジャンプページの後にオブジェクトをすぐに削除します。したがって、更新すると値が失われます。
3.まとめ
ボトム、またはその両方のジャンプですが、スプリングはパッケージのみを実施しているため、実際にはジャンプする方法がたくさんあり、最も原始的な応答を使用することもできます。問題ありません。さて、ここ。

In fact, there is nothing, but know this is very simple, did not get to know before, now they get to know and share. There are questions give me a shout.

addFlashAttribute springmvc3のメソッド
URL:http://www.software8.co/wzjs/java/2943.html



POJOがデータベースに保存された後、春のmvc2で、ページの成功に戻ることを忘れないでください。この時点で少し情報が必要な場合は、
これがあります:
Javaコード:

// The third parameter (UserModel user) by default binding object @RequestMapping(value = '/user/save', method = RequestMethod.POST) public ModelAndView saveUser(HttpServletRequest request, HttpServletResponse response,UserModel user) throws Exception { ModelAndView mv = new ModelAndView ( '/ user / save / result') // default forward mode // ModelAndView mv = new ModelAndView ( 'redirect: / user / save / result') // redirect mode mv.addObject ( 'message', 'save the user successfully!') return mv }

春のmvc3.1では、
Javaコード:

@RequestMapping(value = '/user/save', method = RequestMethod.POST) public ModelAndView saveUser(UserModel user, RedirectAttributes redirectAttributes) throws Exception { redirectAttributes.addFlashAttribute ( 'message', 'save the user successfully!') // use addFlashAttribute, parameter does not appear in the url address bar return 'redirect:/user/save/result' }

いくつかの情報を記入する最初のフォームのやや完全な例を示しています。

Javaコード:

@RequestMapping(value='/saveUserDetails.action', method=RequestMethod.POST) public String greetingsAction(@Validated User user,RedirectAttributesredirectAttributes){ someUserdetailsService.save(user) redirectAttributes.addFlashAttribute('firstName', user.getFirstName()) redirectAttributes.addFlashAttribute('lastName', user.getLastName()) return 'redirect:success.html' } success.html:

次に、コントローラーは次のようになります。
Javaコード:

@RequestMapping(value='/success.html', method=RequestMethod.GET) public String successView(HttpServletRequest request){ Map map = RequestContextUtils.getInputFlashMap(request) if (map!=null) return 'success' else return 'redirect: someOtherView' // give others tips

ただし、F5の場合、実際にはリダイレクトのフラッシュスコープのみをサポートしているため、パラメータが欠落していることがわかります。次のように判断できます。

Javaコード:

return 'forward:/index.action' }

Spring MVCはどのようにリクエストを転送し、リダイレクトしますか?

URL:http://blog.sina.com.cn/s/blog_9cd9dc7101016abw.html

見下ろしてください:

この部分は単純なので、お見逃しなく。

1.リクエストを転送します。

(1)ModelAndViewを返します:
@RequestMapping(value =” / model”、method = RequestMethod.GET)
public ModelAndView testForward(ModelAndView model、@ RequestParam(value =” id”、defaultValue =” 1”、required = false)Long id){
ユーザーu = getBaseService()。get(User.class、id)
model.addObject(“ user”、u)
model.setViewName(“ forward:index.jsp”)
リターンモデル
}

上記のコードは、戻りモデルAndViewが赤でマークされている場合、転送に追加され、リダイレクトする場合は、リダイレクトを置き換えることができ、転送オブジェクトを実現できます。

(2)文字列を返します

@RequestMapping(value =” / forward”、method = RequestMethod.GET)
public String testForward(){

@RequestMapping(value='/redirect',method=RequestMethod.GET) public String testRedirect(RedirectAttributes attr){ attr.addAttribute('a', 'a') attr.addFlashAttribute('b', 'b') return 'redirect:/index.action' }

コードの赤い部分として。

2.リダイレクトをリクエストする

リクエストの転送は次のように分類できます。1。2。パラメータのないパラメータ

(1)パラメータ付き:

Javaコードコレクションコード

Note:. 1 addAttribute method of passing parameters using RedirectAttributes will follow behind the URL, as the code that is http:? /Index.action a = a 2. addFlashAttribute not follow behind URL, the parameter value will temporarily stored in session, until the session is removed from the redirect url on the parameters, redirect here must be a method for mapping the path, jsp invalid. You will find jsp page after redirect only once in b, b refresh no longer appear, which validated the above said, b after being accessed will be removed from the session. For duplicate submission can use this to complete. In addition, if the RedirectAttributes as an argument, but no redirect it? RedirectAttributes does not pass parameters in this case in the past, the default model, the official advice is to pass forward correspondence:

パラメータは、パラメータの受け渡しに使用できますRedirectAttributes:

|_+_|

p:ignoreDefaultModelOnRedirect =” true” />

ignoreDefaultModelOnRedirect RequestMappingHandlerAdapter属性を設定します。これにより、効率が向上し、不要な取得を回避できます。

(2)パラメータなし

@RequestMapping(value =” / redirect”、method = RequestMethod.GET)
public String testRedirect(){

「redirect:/index.action」を返します
}

転載:https://blog.csdn.net/paincupid/article/details/52263817