SpringMVCを以前に学んだとき、彼の転送価値は驚くべきものだと感じました。シンプルで、速く、効率的です。
今日、私はあなたと共有するためにいくつかの簡単な転送を書きます。
1つ、
後ろから正面へ:
(1)
@controller @requestmapping(value = {"/hello"})public class hellocontroller {@requestmapping(value = {"sub"})public modelsview submit(httpservletrequest request)throws {// todo auto-jenerated method stub modelandview m = new modelandview(); M.Addobject( "ok"、 "hello"); M.SetViewName( "Success"); mを返します。 }}addobject(string、object)に渡したいものを置くと、値はオブジェクトタイプであり、何でもすることができます。
setViewName()は、(success.jspページ)にジャンプするページを設定します。
success.jspページで$ {requestscope}または$ {ok}を使用して削除します。とても簡単で速いのではありませんか?
この方法で渡すこともできます。
@controller @requestmapping(value = {"/user"})public class usercontroller {@requestmapping(value = {"/get"})public modelandview user(user user)throws {modelandview mv = new modelandview(); mv.addobject( "ok"、user.getusername()+" - "+user.getPassword()); mv.setViewName( "success"); MVを返します。 }}フロントエンドはシンプルなフォームです:
<form action = "user/get" method = "post"> <input type = "text" name = "username" = "username"> <input type = "text" name = "password" = "password"> <input type = "submit"> </form>
(2)返品値はModelandViewではないかどうか
@RequestMapping(value = {"/map"})public string ok(Map Map、Model Model、ModelMap ModelMap、ユーザーユーザー)スロー{map.put( "ok1"、user); model.addattribute( "ok2"、user); ModelMap.Addattribute( "ok3"、user); "show"を返します;}二、
前から後ろまで:
(1)
@RequestMapping(value = {"ant/{username}/topic/{topic}"}、method = {requestmethod.get})public modelandview ant( @pathvariable(value = "username")string username、 @pathvariable(value = "topic")string thows expecution {/dodo aut aut aut-jenerate System.out.println(username); System.out.println(トピック); mを返します。 }フロントエンドは次のようになります:
<a href = "hello/ant/tom/topic/cat"> ant </a>
value = {"ant/{username}/topic/{topic}"}に対応します。
また、この形式にすることもできます。
@RequestMapping(value = {"/regex/{number:// d+} - {tel:// d+}"})public modelandview regex( @pathvariable(value = "number")int number、 @pathvariable(value = "tel")string tel)Throws exception System.out.println(number); System.out.println(tel); mを返します。 }フロントエンドは次のようになります:
<a href = "hello/regex/100-111"> regex(通常)</a>
(2)これはキーパス値です。
@RequestMapping(value = {"/ok1"})public string ok1(@requestparam(value = "username")string username)throws exception {system.out.println(username); 「show」を返します。 }フロントエンドは次のようになります:
<a href = "user/ok1?username = tom"> key-transfer値</a>
これは、キーなしで渡される値です。
@RequestMapping(value = {"/ok2"})public string ok2(@requestparam string password、@requestparam string username)throws exception {system.out.println(username); System.out.println(パスワード); 「show」を返します。 }フロントエンドは次のようになります:
<a href = "user/ok2?username = tom&password = 111">キー送信なし</a>
興味深いことに、2つの値に正確に対応できます。
上記の記事「SpringMVCのSimple Value Transmission(実装コード)は、私が共有するすべてのコンテンツです。参照を提供できることを願っています。Wulin.comをもっとサポートできることを願っています。