{"id":341,"date":"2019-09-12T10:27:33","date_gmt":"2019-09-12T01:27:33","guid":{"rendered":"http:\/\/2019se3.satoshis.jp\/?p=341"},"modified":"2019-09-12T11:04:22","modified_gmt":"2019-09-12T02:04:22","slug":"9%e6%9c%8812%e6%97%a5","status":"publish","type":"post","link":"https:\/\/2019se3.satoshis.jp\/?p=341","title":{"rendered":"9\u670812\u65e5"},"content":{"rendered":"<h3>\u30c4\u30a4\u30fc\u30c8\u3092\u6295\u7a3f\u3059\u308b\u51e6\u7406\u306e\u5b9f\u88c5<\/h3>\n<p>MyController.java<\/p>\n<pre class=\"brush: java; first-line: 74; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/tweet&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView tweet(ModelAndView mav,\r\n\t\t\t@ModelAttribute(&quot;tweet&quot;) @Validated Tweet tweet,\r\n\t\t\tErrors errors,\r\n\t\t\tPrincipal p) {\r\n\t\tif (errors.hasErrors()) {\r\n\t\t\tmav.setViewName(&quot;mypage&quot;);\r\n\t\t\tmav.addObject(&quot;msg&quot;, &quot;\u30a8\u30e9\u30fc\u3067\u3059&quot;);\r\n\t\t\treturn mav;\r\n\t\t}\r\n\r\n\t\treturn new ModelAndView(&quot;redirect:\/mypage&quot;);\r\n\t}\r\n<\/pre>\n<p>\u3069\u306e\u3088\u3046\u306a\u60c5\u5831\u304cTweet\u306b\u5165\u3063\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u3001Tweet\u306btoString()\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<p>Tweet.java<\/p>\n<pre class=\"brush: java; first-line: 64; title: ; notranslate\" title=\"\">\r\n\t@Override\r\n\tpublic String toString() {\r\n\t\tStringBuilder builder = new StringBuilder();\r\n\t\tbuilder\r\n\t\t\t.append(&quot;Tweet [id=&quot;).append(id)\r\n\t\t\t.append(&quot;, content=&quot;).append(content)\r\n\t\t\t.append(&quot;, time=&quot;).append(time)\r\n\t\t\t.append(&quot;, profile=&quot;).append(profile)\r\n\t\t\t.append(&quot;]&quot;);\r\n\t\treturn builder.toString();\r\n\t}\r\n<\/pre>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304b\u3089Tweet\u3092\u30b3\u30f3\u30bd\u30fc\u30eb\u306b\u51fa\u529b\u3057\u3066\u307f\u308b\u3002<\/p>\n<p>MyContoller.java<\/p>\n<pre class=\"brush: java; first-line: 74; highlight: [84]; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/tweet&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView tweet(ModelAndView mav,\r\n\t\t\t@ModelAttribute(&quot;tweet&quot;) @Validated Tweet tweet,\r\n\t\t\tErrors errors,\r\n\t\t\tPrincipal p) {\r\n\t\tif (errors.hasErrors()) {\r\n\t\t\tmav.setViewName(&quot;mypage&quot;);\r\n\t\t\tmav.addObject(&quot;msg&quot;, &quot;\u30a8\u30e9\u30fc\u3067\u3059&quot;);\r\n\t\t\treturn mav;\r\n\t\t}\r\n\t\tSystem.out.println(tweet);\r\n\t\treturn new ModelAndView(&quot;redirect:\/mypage&quot;);\r\n\t}\r\n<\/pre>\n<p>Tweet\u306b\u73fe\u5728\u6642\u523b\u3068Profile\u3092\u8a2d\u5b9a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; highlight: [85,86,87]; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/tweet&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView tweet(ModelAndView mav,\r\n\t\t\t@ModelAttribute(&quot;tweet&quot;) @Validated Tweet tweet,\r\n\t\t\tErrors errors,\r\n\t\t\tPrincipal p) {\r\n\t\tif (errors.hasErrors()) {\r\n\t\t\tmav.setViewName(&quot;mypage&quot;);\r\n\t\t\tmav.addObject(&quot;msg&quot;, &quot;\u30a8\u30e9\u30fc\u3067\u3059&quot;);\r\n\t\t\treturn mav;\r\n\t\t}\r\n\t\tOptional&lt;Profile&gt; data = repository.findByUsername(p.getName());\r\n\t\ttweet.setProfile(data.get());\r\n\t\ttweet.setTime(new Date());\r\n\t\tSystem.out.println(tweet);\r\n\t\treturn new ModelAndView(&quot;redirect:\/mypage&quot;);\r\n\t}\r\n<\/pre>\n<p>Tweet\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4fdd\u5b58\u3059\u308b\u305f\u3081\u306b\u3001\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>TweetRepository.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage jp.abc;\r\n\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\n\r\npublic interface TweetRepository extends JpaRepository&lt;Tweet, Long&gt; {\r\n\r\n}\r\n<\/pre>\n<p>\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u305f\u306e\u3067\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304b\u3089\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u3068\u3057\u3066\u8ffd\u52a0\u3057\u3001@Autowired\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u3051\u3066DI\u3057\u3066\u3082\u3089\u3046\u3002<\/p>\n<p>MyController.java<\/p>\n<pre class=\"brush: java; first-line: 22; highlight: [28,29]; title: ; notranslate\" title=\"\">\r\n@Controller\r\npublic class MyController {\r\n\t@Autowired\r\n\tprivate JdbcUserDetailsManager userManager;\r\n\t@Autowired\r\n\tprivate ProfileRepository repository;\r\n\t@Autowired\r\n\tprivate TweetRepository tweetRepository;\r\n<\/pre>\n<pre class=\"brush: java; first-line: 77; highlight: [91]; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/tweet&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView tweet(ModelAndView mav,\r\n\t\t\t@ModelAttribute(&quot;tweet&quot;) @Validated Tweet tweet,\r\n\t\t\tErrors errors,\r\n\t\t\tPrincipal p) {\r\n\t\tif (errors.hasErrors()) {\r\n\t\t\tmav.setViewName(&quot;mypage&quot;);\r\n\t\t\tmav.addObject(&quot;msg&quot;, &quot;\u30a8\u30e9\u30fc\u3067\u3059&quot;);\r\n\t\t\treturn mav;\r\n\t\t}\r\n\t\tOptional&lt;Profile&gt; data = repository.findByUsername(p.getName());\r\n\t\ttweet.setProfile(data.get());\r\n\t\ttweet.setTime(new Date());\r\n\t\tSystem.out.println(tweet);\r\n\t\ttweetRepository.saveAndFlush(tweet);\r\n\t\treturn new ModelAndView(&quot;redirect:\/mypage&quot;);\r\n\t}\r\n<\/pre>\n<p>mypage \u306b\u30c4\u30a4\u30fc\u30c8\u4e00\u89a7\u3092\u8868\u793a\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304b\u3089ModelAndView\u306b\u6e21\u3057\u3066\u304a\u304f\u3002<\/p>\n<p>MyController.java<\/p>\n<pre class=\"brush: java; first-line: 61; highlight: [75,76]; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/mypage&quot;)\r\n\tpublic ModelAndView top(ModelAndView mav, Principal p) {\r\n\t\tmav.setViewName(&quot;mypage&quot;);\r\n\t\tmav.addObject(&quot;username&quot;, p.getName());\r\n\t\tOptional&lt;Profile&gt; data = repository.findByUsername(p.getName());\r\n\t\tProfile profile;\r\n\t\tif (!data.isPresent()) {\r\n\t\t\tprofile = new Profile();\r\n\t\t\tprofile.setUsername(p.getName());\r\n\t\t\trepository.saveAndFlush(profile);\r\n\t\t} else {\r\n\t\t\tprofile = data.get();\r\n\t\t}\r\n\t\tmav.addObject(&quot;profile&quot;, profile);\r\n\t\tList&lt;Tweet&gt; list = tweetRepository.findAll();\r\n\t\tmav.addObject(&quot;datalist&quot;, list);\r\n\t\treturn mav;\r\n\t}\r\n<\/pre>\n<p>mypage \u306b\u30c4\u30a4\u30fc\u30c8\u4e00\u89a7\u3092\u8868\u793a\u3059\u308b\u3002<\/p>\n<p>mypage.html<\/p>\n<pre class=\"brush: xml; highlight: [20,21,22,23,24,25,26,27,28,29,30]; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html xmlns:th=&quot;http:\/\/www.thymeleaf.org&quot;&gt;\r\n&lt;head&gt;\r\n    &lt;meta charset=&quot;UTF-8&quot; \/&gt;\r\n    &lt;title&gt;login&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;\u30de\u30a4\u30da\u30fc\u30b8&lt;\/h1&gt;\r\n&lt;p&gt;\u3088\u3046\u3053\u305d\u3001&lt;span th:text=&quot;${username}&quot;&gt;&lt;\/span&gt;\u3055\u3093\uff01&lt;\/p&gt;\r\n&lt;p th:text=&quot;${profile.username}&quot;&gt;&lt;\/p&gt;\r\n&lt;form th:action=&quot;@{\/tweet}&quot; th:object=&quot;${tweet}&quot; method=&quot;post&quot;&gt;\r\n\t&lt;textarea name=&quot;content&quot; cols=&quot;40&quot; rows=&quot;10&quot;&gt;&lt;\/textarea&gt;\r\n\t&lt;br \/&gt;\r\n\t&lt;input type=&quot;submit&quot; value=&quot;\u30c4\u30a4\u30fc\u30c8\u3059\u308b&quot; \/&gt;\r\n&lt;\/form&gt;\r\n&lt;form th:action=&quot;@{\/logout}&quot; method=&quot;post&quot;&gt;\r\n\t&lt;input type=&quot;submit&quot; value=&quot;\u30ed\u30b0\u30a2\u30a6\u30c8&quot; \/&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;hr \/&gt;\r\n&lt;table&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;th&gt;\u540d\u524d&lt;\/th&gt;&lt;th&gt;\u65e5\u6642&lt;\/th&gt;&lt;th&gt;\u6295\u7a3f\u5185\u5bb9&lt;\/th&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;tr th:each=&quot;obj : ${datalist}&quot;&gt;\r\n\t\t&lt;td th:text=&quot;${obj.profile.username}&quot;&gt;&lt;\/td&gt;\r\n\t\t&lt;td th:text=&quot;${obj.time}&quot;&gt;&lt;\/td&gt;\r\n\t\t&lt;td th:text=&quot;${obj.content}&quot;&gt;&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u30c4\u30a4\u30fc\u30c8\u3092\u6295\u7a3f\u3059\u308b\u51e6\u7406\u306e\u5b9f\u88c5 MyController.java \u3069\u306e\u3088\u3046\u306a\u60c5\u5831\u304cTweet\u306b\u5165\u3063\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u3001Tweet\u306btoString()\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3002 Tweet.java \u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304b\u3089Twe &hellip; <a href=\"https:\/\/2019se3.satoshis.jp\/?p=341\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;9\u670812\u65e5&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/341"}],"collection":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341"}],"version-history":[{"count":7,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions"}],"predecessor-version":[{"id":348,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/341\/revisions\/348"}],"wp:attachment":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}