実現したいこと
Spring Bootの学習をしている初学者です。
テキストを用いて学習をしているのですが、
mavenベースのプロジェクトでプロパティファイルmessages_ja.propertiesからテキストを読み込んで、表示しようとしています。
ですが、画像のようにプロパティファイルを作成して、htmlにメッセージ式を入力しても、うまく表示がされません。(??content.title_ja??の部分にテキストを表示したいです。)
プロパティファイルを作成してメッセージ式をビューのファイルに記述するだけでは足りないのでしょうか。
ご教示お願い致します。
該当のソースコード
Helocontroller.java
java
1package com.tuyano.springboot; 2 3 4 5import org.springframework.stereotype.Controller; 6import org.springframework.transaction.annotation.Transactional; 7import org.springframework.validation.BindingResult; 8import org.springframework.validation.annotation.Validated; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.PathVariable; 11import org.springframework.web.bind.annotation.RequestMapping; 12import org.springframework.web.bind.annotation.RequestMethod; 13import org.springframework.web.bind.annotation.RequestParam; 14import org.springframework.web.servlet.ModelAndView; 15import org.springframework.beans.factory.annotation.Autowired; 16 17 18import java.util.Optional; 19 20import javax.annotation.PostConstruct; 21 22import com.tuyano.springboot.repositories.MyDataRepository; 23 24@Controller 25public class HeloController { 26 27 @Autowired 28 MyDataRepository repository; 29 30 @RequestMapping(value="/", method=RequestMethod.GET) 31 public ModelAndView index( 32 @ModelAttribute("formModel") MyData mydata, 33 ModelAndView mav) { 34 mav.setViewName("index"); 35 mav.addObject("msg","this is sample content."); 36 mav.addObject("formModel",mydata); 37 Iterable<MyData> list=repository.findAll(); 38 mav.addObject("datalist",list); 39 return mav; 40 } 41 42 43 @RequestMapping(value="/edit/{id}",method=RequestMethod.GET) 44 public ModelAndView edit( 45 @ModelAttribute MyData mydata, 46 @PathVariable int id, 47 ModelAndView mav){ 48 mav.setViewName("edit"); 49 mav.addObject("title","edit mydata"); 50 Optional<MyData> data=repository.findById((long)id); 51 mav.addObject("formModel",data.get()); 52 return mav; 53 } 54 55 56 @RequestMapping(value="/delete/{id}" ,method=RequestMethod.GET) 57 public ModelAndView delete( 58 @PathVariable int id, 59 ModelAndView mav) { 60 mav.setViewName("delete"); 61 mav.addObject("title","delete mydata"); 62 Optional<MyData> data=repository.findById((long)id); 63 mav.addObject("formModel",data.get()); 64 return mav; 65 } 66 67 68 69 70 @RequestMapping(value="/",method=RequestMethod.POST) 71 @Transactional(readOnly=false) 72 public ModelAndView form( 73 @ModelAttribute("formModel") 74 @Validated MyData mydata, 75 BindingResult result, 76 ModelAndView mov) { 77 ModelAndView res=null; 78 if(!result.hasErrors()) { 79 repository.saveAndFlush(mydata); 80 res=new ModelAndView("redirect:/"); 81 }else { 82 mov.setViewName("index"); 83 mov.addObject("msg","sorry, error is occured"); 84 Iterable<MyData>list=repository.findAll(); 85 mov.addObject("datalist",list); 86 res=mov; 87 } 88 return res; 89 } 90 91 92 @RequestMapping(value="/edit", method=RequestMethod.POST) 93 @Transactional(readOnly=false) 94 public ModelAndView update( 95 @ModelAttribute MyData mydata, 96 ModelAndView mav) { 97 repository.saveAndFlush(mydata); 98 return new ModelAndView("redirect:/"); 99 } 100 101 102 @RequestMapping(value="/delete",method=RequestMethod.POST) 103 @Transactional(readOnly=false) 104 public ModelAndView remove( 105 @RequestParam long id, 106 ModelAndView mav) { 107 repository.deleteById(id); 108 return new ModelAndView("redirect:/"); 109 } 110 111 112 113 114 @PostConstruct 115 public void init() { 116 //1つ目のデータ作成 117 MyData d1=new MyData(); 118 d1.setName("tuyano"); 119 d1.setAge(123); 120 d1.setMail("syoda@tuyano"); 121 d1.setMemo("09099994444"); 122 repository.saveAndFlush(d1); 123 124 //2つ目のデータ作成 125 MyData d2=new MyData(); 126 d2.setName("naoki"); 127 d2.setAge(23); 128 d2.setMail("gorilla227naoki@gmail.com"); 129 d2.setMemo("070498394222"); 130 repository.saveAndFlush(d2); 131 132 //3つ目のデータ作成 133 //1つ目のデータ作成 134 MyData d3=new MyData(); 135 d3.setName("sachiko"); 136 d3.setAge(14); 137 d3.setMail("sachiko@happy"); 138 d3.setMemo("09099996767"); 139 repository.saveAndFlush(d3); 140 141 } 142 143} 144
messages_ja.properties
content.title=\u3053\u308C\u306F\u30B5\u30F3\u30D7\u30EB\u30DA\u30FC\u30B8\u3067\u3059\u3002
index.html
html
1<!DOCTYPE html> 2<html lang="ja" xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 5<title>TOP PAGE</title> 6<style> 7h1{font-size:18pt; font-weight:bold; color:gray;} 8body{font-size:13pt; color:gray; margin:5px 25px;} 9tr{margin:5px;} 10th{padding:5px; color:white; background:darkgray;} 11td{padding:5px; color:black; background:#f0f0f0f;} 12.err{color:red;} 13</style> 14</head> 15 16<body> 17<h1 th:text="#{content.title}">Helo Page</h1> 18<p th:text="${msg}"></p> 19<table> 20<form method="post" action="/" th:object="${formModel}"> 21 22 23<tr> 24 <td><label for="name">名前</label></td> 25 <td><input type="text" name="name" th:value="*{name}" th:errorclass="err"/> 26 <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" th:errorclass="err"> 27 </div></td> 28</tr> 29<tr> 30 <td><label for="age">年齢</label></td> 31 <td><input type="text" name="age" th:value="*{age}" th:errorclass="err"/> 32 <div th:if="${#fields.hasErrors('age')}" th:errors="*{age}" th:errorclass="err"></div></td> 33</tr> 34<tr> 35 <td><label for="mail">メール</label></td> 36 <td><input type="text" name="mail" th:value="*{mail}" th:errorclass="err"/> 37 <div th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}" th:errorclass="err"></div></td> 38</tr> 39<tr> 40 <td><label for="memo">メモ</label></td> 41 <td><textarea name="memo" th:value="*{memo}" cols="20" rows="5"></textarea> 42 <div th:if="${#fields.hasErrors('memo')}" th:errors="*{memo}" th:errorclass="err"></div></td> 43</tr><td></td><tr> 44<td> 45<input type="submit"/> 46</td> 47</tr> 48</form> 49</table> 50<hr/> 51 52<table> 53<tr><th>ID</th><th>名前</th></tr> 54<tr th:each="obj:${datalist}"> 55<td th:text="${obj.id}"></td> 56<td th:text="${obj.name}"></td> 57</tr> 58</table> 59 60 61</body> 62</html>
messages.properties
content.title=this is sample page!
pom.xml
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.7.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.tuyano.springboot</groupId> 12 <artifactId>MyBootApp</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>MyBootApp</name> 15 <description>Sample project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-data-jpa</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 <dependency> 32 <groupId>com.h2database</groupId> 33 <artifactId>h2</artifactId> 34 <scope>runtime</scope> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-test</artifactId> 39 <scope>test</scope> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-thymeleaf</artifactId> 44 </dependency> 45 </dependencies> 46 47 <build> 48 <plugins> 49 <plugin> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-maven-plugin</artifactId> 52 </plugin> 53 </plugins> 54 </build> 55 56</project> 57
補足情報(FW/ツールのバージョンなど)




回答1件
あなたの回答
tips
プレビュー