SpringMVC学習シリーズ(5) データバインディング-2

黄舟
リリース: 2017-03-03 10:54:54
オリジナル
1395 人が閲覧しました

シリーズ (SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2) では、@RequestParam を使用してデータをバインドする方法を紹介しました。他のデータ バインディング アノテーションの使用方法を見てみましょう。

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.@PathVariable は URL テンプレート変数の値をバインドするために使用されます。これの使用方法はシリーズ (SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2) ですでに紹介したので、ここでは詳しく説明しません。

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.@CookieValue は Cookie 内のデータをバインドするために使用されます。 Cookie 内の sessionId を取得してテストしてみましょう:

DataBindController に cookiebind アクションを追加します。コードは次のとおりです:

//@CookieValue Test@RequestMapping(value="/cookiebind", method = {RequestMethod.GET})public String cookieBind(HttpServletRequest request, Model model, @CookieValue(value="JSESSIONID", defaultValue="") String jsessionId){
    
    model.addAttribute("jsessionId", jsessionId);    return "cookiebindresult";
}
ログイン後にコピー


views フォルダーに cookiebindresult.jsp ビューを追加します。コードは次のとおりです:

nbsp;html PUBLIC "-//WSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2C//DTD HTML SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.0SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 Transitional//EN" "http://www.wSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.org/TR/htmlSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2/loose.dtd"><meta><title>Insert title here</title>
    ${jsessionId}
ログイン後にコピー


Run Test:

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2

sessionId が取得されていることがわかります。

注: @CookieValue にも @RequestParam と同様に SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 つのパラメータがあり、その意味は @RequestParam パラメータと同じです。

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2. @RequestHeader は、リクエスト ヘッダー内のデータをバインドするために使用されます。 @RequestHeader を使用して、デモンストレーション用の User-Agent を取得します。

DataBindController に requestheaderbind アクションを追加します。コードは次のとおりです。

//@RequestHeader Test@RequestMapping(value="/requestheaderbind", method = {RequestMethod.GET})public String requestHeaderBind(HttpServletRequest request, Model model, @RequestHeader(value="User-Agent", defaultValue="") String userAgent){
    
    model.addAttribute("userAgent", userAgent);    return "requestheaderbindresult";
}
ログイン後にコピー


views フォルダーに requestheaderbindresult.jsp ビューを追加します。コードは次のとおりです:

nbsp;html PUBLIC "-//WSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2C//DTD HTML SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.0SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 Transitional//EN" "http://www.wSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.org/TR/htmlSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2/loose.dtd"><meta><title>Insert title here</title>
    ${userAgent}
ログイン後にコピー


テストを実行します:

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2

User-Agent が取得されたことがわかります。

注: @RequestHeader にも @RequestParam と同様に SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 つのパラメーターがあり、その意味は @RequestParam パラメーターと同じです。

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.@ModelAttribute はデータをモデルにバインドします。シリーズ (SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2) の modelAutoBind アクションでは、フォームによって送信されたデータをモデルに追加するコードは次のとおりです:

@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})public String modelAutoBind(HttpServletRequest request, Model model, AccountModel accountModel){
    
    model.addAttribute("accountmodel", accountModel);    return "modelautobindresult";
}
ログイン後にコピー

@ModelAttribute を使用すると、より簡単にデータをモデルに追加して変更できます。上記のコードのテストを実行します:

@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})public String modelAutoBind(HttpServletRequest request, @ModelAttribute("accountmodel") AccountModel accountModel){    
    return "modelautobindresult";
}
ログイン後にコピー

:

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2

送信されたデータがまだ正常にバインドされていることがわかります。

SpringMVC学習シリーズ(5) データバインディング-2. Model のデータ スコープはリクエスト レベルです。これは、SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 つのリクエスト リクエストでは、他のリクエスト リクエストでリクエストされたモデルのデータを取得できないことを意味します。ただし、@SessionAttributes を使用してセッションにデータを保存し、複数のリクエスト間でデータを維持できるため、段階的にフォームを送信するなどの要件を実装できます。 SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 つのステップでデータを AccountModel にバインドする方法を見てみましょう:

在DataBindController上添加:

@SessionAttributes(value = "sessionaccountmodel")
ログイン後にコピー

在DataBindController添加usernamebind和passwordbind action,代码如下:

//@SessionAttributes Test@ModelAttribute("sessionaccountmodel")public AccountModel initAccountModel(){    
    return new AccountModel();
}

@RequestMapping(value="/usernamebind", method = {RequestMethod.GET})public String userNameBind( Model model, AccountModel accountModel){
    
    model.addAttribute("sessionaccountmodel", new AccountModel());    return "usernamebind";
}

@RequestMapping(value="/usernamebind", method = {RequestMethod.POST})public String userNameBindPost( @ModelAttribute("sessionaccountmodel") AccountModel accountModel){    
    //重定向到密码绑定测试
    return "redirect:passwordbind";
}

@RequestMapping(value="/passwordbind", method = {RequestMethod.GET})public String passwordBind(@ModelAttribute("sessionaccountmodel") AccountModel accountModel){    
    return "passwordbind";
}

@RequestMapping(value="/passwordbind", method = {RequestMethod.POST})public String passwordBindPost(@ModelAttribute("sessionaccountmodel") AccountModel accountModel, SessionStatus status){    
    //销毁@SessionAttributes存储的对象    status.setComplete();    //显示绑定结果
    return "sessionmodelbindresult";
}
ログイン後にコピー


由于我们在controller上指定了@SessionAttributes,所以在@ModelAttribute(“xxx”)注解的参数会直接在@SessionAttributes中查找名为”xxx”的对象,如果没有找到则调用@ModelAttribute(“xxx”)注解的方法返回对象并存入@SessionAttributes(如果没有找到且没有@ModelAttribute(“xxx”)注解的方法就会抛出HttpSessionRequiredException)。当执行到最后一步就可以调用SessionStatus .setComplete()方法把@SessionAttributes中保存对象销毁了(不会清除HttpSession中的数据)。

在views文件夹中添加usernamebind.jsp、passwordbind.jsp和sessionmodelbindresult.jsp视图内容分别如下:

nbsp;html PUBLIC "-//WSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2C//DTD HTML SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.0SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 Transitional//EN" "http://www.wSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.org/TR/htmlSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2/loose.dtd"><meta><title>Insert title here</title>
    <form>     
        用户名:<input><br>
        <input>
    </form>  
ログイン後にコピー


nbsp;html PUBLIC "-//WSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2C//DTD HTML SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.0SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 Transitional//EN" "http://www.wSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.org/TR/htmlSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2/loose.dtd"><meta><title>Insert title here</title>
    <form>     
        密 码:<password></password><br>
        <input>
    </form>  
ログイン後にコピー


nbsp;html PUBLIC "-//WSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2C//DTD HTML SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.0SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 Transitional//EN" "http://www.wSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.org/TR/htmlSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2/loose.dtd"><meta><title>Insert title here</title>
    用户名:${sessionaccountmodel.username}<br>
    密 码:${sessionaccountmodel.password}
ログイン後にコピー


运行测试:

SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2

SpringMVC学習シリーズ(5) データバインディング-2

SpringMVC学習シリーズ(5) データバインディング-2

可以看到我们已经成功的分SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2步把数据绑定到AccountModel中了。

注:

@SessionAttributes有value和types两个参数其中value指明要对象的名称,types指定要绑定对象的类型,如@SessionAttributes(value = "sessionaccountmodel", types=AccountModel.class)两者是and关系,需要同时满足。也可以同时指定多个value和types 如:@SessionAttributes(value = {"aa", "aa"} , types={XXX.class, YYY.class}) 。

SpringMVC学習シリーズ(5) データバインディング-2.@RequestBody 调用合适的MessageConvert来把非application/x-www-form-urlencoded请求中的内容转换为指定的对象它通常与@ResponseBody合用,@ResponseBody与.@RequestBody刚好相反,他把指定的对象转换为合适的内容(请求头为Accept:application/json 则返回json数据)并返回。这里我们用一个ajax请求做演示:

由于Spring默认解析json用的是Jackson,所以我们这里要把jackson-core-asl-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.9.SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.jar和jackson-mapper-asl-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.9.SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.jar两个包添加到我们项目。

修改AccountModel让其继承Serializable接口,并添加一个空的构造函数(为了Jackson做转换)。

在DataBindController添加requestBodyBindaction,代码如下:

//@RequestBody Test@RequestMapping(value="/requestbodybind", method = {RequestMethod.GET})public String requestBodyBind(Model model){
    
    model.addAttribute("accountmodel", new AccountModel());    return "requestbodybind";
}

@RequestMapping(value="/requestbodybind", method = {RequestMethod.POST})public @ResponseBody AccountModel requestBodyBind(@RequestBody AccountModel accountModel){            
    return accountModel;
}
ログイン後にコピー


在views文件夹中添加requestbodybind.jsp视图内容如下:

nbsp;html PUBLIC "-//WSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2C//DTD HTML SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.0SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2 Transitional//EN" "http://www.wSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.org/TR/htmlSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2/loose.dtd"><script></script><meta><title>Insert title here</title>
    <form>     
        用户名:<input><br>
        密 码:<password></password><br>
        <input>
    </form> 
    
    <script>
        $(function() { 
            $("#submit").click(function() {   
                var postdata = &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;{"username":"&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29; + $(&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;#username&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;).val() + &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;","password":"&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29; + $(&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;#password&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;).val() + &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;"}&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;;   
                $.ajax({  
                    type : &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;POST&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;,  
                    contentType : &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;application/json&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;,  
                    url : &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;http://www.php.cn/:8080/SpringMVCLesson/databind/requestbodybind&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;,  
                    processData : false,  
                    dataType : &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;json&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;,  
                    data : postdata,  
                    success : function(data) {  
                        alert(&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;username : &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;+data.username+&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;\npassword : &#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;+data.password);  
                    },  
                    error : function() {  
                        alert(&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;error...&#SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-29;);  
                    }  
                }); 
            });
        });    </script> 
ログイン後にコピー


运行测试:

SpringMVC学習シリーズ(5) データバインディング-2

結果は正しく、変換が成功したことが証明されています。

SpringMVC学習シリーズ(5) データバインディング-2.@RequestPart は、「multipart/form-data」型データをバインドし、javax.servlet.http.Part ファイルのアップロードをサポートし、型変換を実行できます。詳細については、公式ドキュメントを参照してください:

http://docs。 spring .io/spring-framework/docs/SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.x/spring-framework-reference/htmlsingle/#new-in-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2.SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2-mvc-requestpart

上記はSpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2)のData Binding-SpringMVC学習シリーズ(SpringMVC学習シリーズ(5) データバインディング-2) データバインディング-2の内容です。 )、その他の関連コンテンツについては、PHP 中国語 Web サイト (www.php.cn) に注目してください。


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!