
{"id":2679,"date":"2020-12-12T16:13:25","date_gmt":"2020-12-12T08:13:25","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2679"},"modified":"2021-01-05T14:10:53","modified_gmt":"2021-01-05T06:10:53","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bspring%e5%a4%84%e7%90%86%e6%a8%a1%e5%9e%8b%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2679","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u5904\u7406\u6a21\u578b\u6570\u636e"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u5904\u7406\u6a21\u578b\u6570\u636e<\/h3>\n<p><strong>1.\u4f7f\u7528entity\u5c06\u6570\u636e\u4ece\u9875\u9762\u4f20\u9012\u5230 controller<\/strong><\/p>\n<p>\u7ed1\u5b9a\u53c2\u6570<\/p>\n<p>entity\uff1a<\/p>\n<p>User:<\/p>\n<p>String username,String password,Integer age,String email,Address address<\/p>\n<p>Address:<\/p>\n<p>String addr,String zipCode<\/p>\n<p>JSP:<br \/>\n<!--more--><\/p>\n<pre>&lt;form action=\"add\" method=\"post\"&gt;\r\n    &lt;label for=\"username\"&gt;\r\n        username : &lt;input type=\"text\" id=\"username\" name=\"username\"&gt;&lt;br&gt;\r\n    &lt;\/label&gt;\r\n    &lt;label for=\"password\"&gt;\r\n        password : &lt;input type=\"password\" name=\"password\" id=\"password\"&gt;&lt;br&gt;\r\n    &lt;\/label&gt;\r\n    &lt;label for=\"age\"&gt;\r\n        age : &lt;input type=\"text\" name=\"age\" id=\"age\"&gt;&lt;br&gt;\r\n    &lt;\/label&gt;\r\n    &lt;label for=\"email\"&gt;\r\n        email : &lt;input type=\"email\" name=\"email\" id=\"email\"&gt;&lt;br&gt;\r\n    &lt;\/label&gt;\r\n    &lt;label for=\"address.addr\"&gt;\r\n        addr : &lt;input type=\"text\" name=\"<strong><span style=\"color: #ff0000;\">address.addr<\/span><\/strong>\" id=\"address.addr\"&gt;&lt;br&gt;\r\n    &lt;\/label&gt;\r\n    &lt;label for=\"address.zipCode\"&gt;\r\n        zipCode : &lt;input type=\"text\" name=\"<span style=\"color: #ff0000;\"><strong>address.zipCode<\/strong><\/span>\" id=\"address.zipCode\"&gt;&lt;br&gt;\r\n    &lt;\/label&gt;\r\n    &lt;button&gt;submit&lt;\/button&gt;\r\n&lt;\/form&gt;<\/pre>\n<p>controller:<\/p>\n<pre>@RequestMapping(\"\/user\")\r\n@Controller\r\npublic class UserController {\r\n    <span style=\"color: #ff0000;\"><strong>@RequestMapping(\"\/form\")<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>    public String testUser(){<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>        return \"form\";<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>    }<\/strong><\/span>\r\n    <span style=\"color: #ff0000;\"><strong>@RequestMapping(\"\/add\")<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>    public String add(User user){<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>        System.out.println(user);<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>        return \"success\";<\/strong><\/span>\r\n<span style=\"color: #ff0000;\"><strong>    }<\/strong><\/span>\r\n}<\/pre>\n<p><strong>2.ModelAndView<\/strong><\/p>\n<pre>@RequestMapping(\"\/edit\")\r\npublic ModelAndView edit(){\r\n    User u=new User(\"\u5f20\u4e09\",\"123\",12,\"12@qq.com\",new Address(\"ah\",\"123\"));\r\n    ModelAndView mav=new ModelAndView(\"form\");\r\n    mav.addObject(\"user\",u);\r\n    return mav;\r\n}<\/pre>\n<p>\u7136\u540e\u5728\u8868\u5355\u7684value\u503c\u7528\u8868\u8fbe\u5f0f\u586b\u5165<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/12\/3.png\" alt=\"\" width=\"680\" \/><\/p>\n<p><strong>3.Map<\/strong><\/p>\n<pre>@RequestMapping(\"\/edit1\")\r\npublic String edit1(Map&lt;String,Object&gt; map){\r\n    User u=new User(\"\u5f20\u4e091\",\"123\",12,\"12@qq.com\",new Address(\"ah\",\"123\"));\r\n    map.put(\"user\",u);\r\n    return \"form\";\r\n}<\/pre>\n<p><strong>4.Model<\/strong><\/p>\n<pre>@RequestMapping(\"\/edit2\")\r\npublic String edit2(Model model){\r\n    User u=new User(\"\u5f20\u4e092\",\"123\",12,\"12@qq.com\",new Address(\"ah\",\"123\"));\r\n    model.addAttribute(\"user\",u);\r\n    return \"form\";\r\n}<\/pre>\n<p><strong>5.sessionScope<\/strong><\/p>\n<pre><span style=\"color: #ff0000;\"><strong>@SessionAttributes(value = {\"user\"},types = {String.class})<\/strong><\/span>\r\n\/\/\u53ea\u5c06String\u7c7b\u578b\u53c2\u6570\u4fdd\u5b58\u5728session\u4f5c\u7528\u57df\uff0c\u5176\u4ed6\u7c7b\u578b\u4e0d\u4fdd\u5b58\r\npublic class UserController {<\/pre>\n<pre>@RequestMapping(\"\/edit1\")\r\npublic String edit1(Map&lt;String,Object&gt; map){\r\n    <span style=\"color: #ff0000;\">map.put(\"strvalue\",\"strvalues\");<\/span>\r\n<span style=\"color: #ff0000;\">    map.put(\"intvalue\",123);<\/span>\r\n    return \"form\";\r\n}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u5904\u7406\u6a21\u578b\u6570\u636e 1.\u4f7f\u7528entity\u5c06\u6570\u636e\u4ece\u9875\u9762\u4f20\u9012\u5230 controlle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2679","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2679"}],"version-history":[{"count":3,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2679\/revisions"}],"predecessor-version":[{"id":2685,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2679\/revisions\/2685"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}