Spring Boot ã§ãã°ã¤ã³ç»é¢ + ä¸è¦§ç»é¢ + ç»é²ç»é¢ã® Webã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( ç»é²ç»é¢ã®ãããããã¦ã³ãªã¹ããè¨å®ãã¡ã¤ã«ããåå¾ããæ¹æ³ã®å¤æ´ )
æ¦è¦
Spring Boot ã§ãã°ã¤ã³ç»é¢ + ä¸è¦§ç»é¢ + ç»é²ç»é¢ã® Webã¢ããªã±ã¼ã·ã§ã³ãä½ã ( ãã®ï¼ï¼ )( logback.xmlã®è¨å® ) ã®ç¶ãã§ãã
-
ä»åã®æé ã§ç¢ºèªã§ããã®ã¯ä»¥ä¸ã®å 容ã§ãã
- ç»é²ç»é¢ã® Continent ã®ãããããã¦ã³ãªã¹ãã«è¡¨ç¤ºãããªã¹ããè¨å®ãã¡ã¤ã« constant.properties ããèªã¿è¾¼ãããã«ãããã£ãã®ã§ãCountryController ã¯ã©ã¹å
ã§
return "country/input";
ã®åã«model.addAttribute("continentList", constant.CONTINENT_LIST);
ãå¼ã³åºãã¦ãã³ãã¬ã¼ããã¡ã¤ã«ã«è¨å®ãã¡ã¤ã«ã®ãã¼ã¿ã渡ãããã«ãã¦ãã¾ããããmodel.addAttribute ãå¼ã³åºããªãã¦ãããæ¹æ³ã«å¤æ´ãã¾ãã
- ç»é²ç»é¢ã® Continent ã®ãããããã¦ã³ãªã¹ãã«è¡¨ç¤ºãããªã¹ããè¨å®ãã¡ã¤ã« constant.properties ããèªã¿è¾¼ãããã«ãããã£ãã®ã§ãCountryController ã¯ã©ã¹å
ã§
-
ç»é²ç»é¢ã表示ããæã«æ¯å model.addAttribute ãå¼ã³åºãã®ã¯ã¹ãã¼ããªæ¹æ³ã§ã¯ãªããªãã¨æãã¤ã¤ãããã以å¤ã®æ¹æ³ãåãããªãã£ããããã¨ããããåãã°ããã¨èãã¦å®è£ ãã¦ãã¾ããããæ¯å model.addAttribute ãå¼ã³åºããªãã¦ãããæ¹æ³ãè¦ã¤ããã¾ããã®ã§å¤æ´ãã¾ãã
ã½ããã¦ã§ã¢ä¸è¦§
åèã«ãããµã¤ã
stackoverflow - Access from Thymeleaf to class field
http://stackoverflow.com/questions/25120799/access-from-thymeleaf-to-class-field- Thymeleaf ã®ãã³ãã¬ã¼ããã¡ã¤ã«ãã static ãã£ã¼ã«ãã®å¤ãåç §ããæ¹æ³ãæ¸ããã¦ãã¾ãã
æé
ç»é²ç»é¢ã®ãããããã¦ã³ãªã¹ããè¨å®ãã¡ã¤ã«ããåå¾ããæ¹æ³ã®å¤æ´
IntelliJ IDEA ä¸ã§ 1.0.x-chgoptiontagvalue ãã©ã³ããä½æãã¾ãã
src/main/java/ksbysample/webapp/basic/config ã®ä¸ã® Constant.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/resources/templates/country ã®ä¸ã® input.html ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
src/main/java/ksbysample/webapp/basic/web ã®ä¸ã® CountryController.java ã ãªã³ã¯å ã®å 容 ã«å¤æ´ãã¾ãã
åä½ç¢ºèªãã¾ããbootRun ã¿ã¹ã¯ãå®è¡ããTomcat ãèµ·åãã¾ãã
ãã©ã¦ã¶ãããã°ã¤ã³ãã¦ç»é²ç»é¢ã表示ããContinent ã®ãããããã¦ã³ãªã¹ãã«å¤ã表示ããããã¨ã確èªãã¾ãã
Run View 㧠Ctrl+F2 ãæ¼ã㦠Tomcat ãåæ¢ãã¾ãã
commit ã®åã« build ã¿ã¹ã¯ãå®è¡ããBUILD SUCCESSFUL ã表示ããããã¨ã確èªãã¾ãã
commitãGitHub 㸠Pushã1.0.x-chgoptiontagvalue -> 1.0.x 㸠Pull Requestã1.0.x ã§ãã¼ã¸ã1.0.x-chgoptiontagvalue ãã©ã³ããåé¤ãããã¾ãã
èå¯
- ä»åã®å®è£
ããã¦ãã¦ã
@Component
ã¢ããã¼ã·ã§ã³ãã¯ã©ã¹ã«ä»å ãã㨠Tomcat èµ·åæã«ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããããã¨ã«æ°ã¥ãã¾ãããã¨ãããã¨ã¯@Component
ã¢ããã¼ã·ã§ã³ãä»ããã¯ã©ã¹ãå¢ãã㨠Tomcat ã®èµ·åæéãå¢ããã£ã¦ãã¨ã§ãããããããèµ·åæã§ã¯ãªãæåã«ã¢ã¯ã»ã¹ãããæã«çæãã¦ããããªãã·ã§ã³ãããã¨ããã®ã§ããããããããã®ã¯ãªãã®ã§ããããï¼
ã½ã¼ã¹ã³ã¼ã
Constant.java
@Component @PropertySource("classpath:constant.properties") public class Constant { public static List<String> CONTINENT_LIST; @Autowired private Constant( @Value("#{'${CONTINENT_LIST}'.split(',')}") List<String> CONTINENT_LIST ) { this.CONTINENT_LIST = CONTINENT_LIST; } }
public final List<String> CONTINENT_LIST;
âpublic static List<String> CONTINENT_LIST;
ã¸å¤æ´ãã¾ããfinal 修飾åãä»ãããã£ãã®ã§ãããä»ããã¨ã¨ã©ã¼ã«ãªãã¾ãã ( ã³ã³ã¹ãã©ã¯ã¿ã§åæåãããã¨ãã¦ããã®ããã¡ãªããã§ã ) ã- ãã®ã¯ã©ã¹ã¯ static ãã£ã¼ã«ãåç
§å°ç¨ã«ããã®ã§ãã³ã³ã¹ãã©ã¯ã¿ã®ã¢ã¯ã»ã¹ä¿®é£¾åã
public Constant {
âprivate Constant {
ã¸å¤æ´ããã¤ã³ã¹ã¿ã³ã¹åã§ããªãããã«ãã¾ãã - ã¯ã©ã¹ã«
@Component
ã¢ããã¼ã·ã§ã³ããã³ã³ã¹ãã©ã¯ã¿ã«@Autowired
ã¢ããã¼ã·ã§ã³ãä»ããã¾ã¾ã«ãã¾ãããããããã¨ã§ãTomcat èµ·åæã«ã³ã³ã¹ãã©ã¯ã¿ãå®è¡ããã¦è¨å®ãã¡ã¤ã«ã®å¤ã CONTINENT_LIST ã«ã»ãããããããã«ãªãã¾ãã
input.html
<select name="continent" id="continent" class="form-control input-sm" th:field="*{continent}"> <option th:each="item : ${T(ksbysample.webapp.basic.config.Constant).CONTINENT_LIST}" th:value="${item}" th:text="${item}">continent</option> </select>
<option th:each="item : ${continentList}"
â<option th:each="item : ${T(ksbysample.webapp.basic.config.Constant).CONTINENT_LIST}"
ã¸å¤æ´ãã¾ãããããããã¦ã³ãªã¹ãã«è¡¨ç¤ºããå¤ã Constant ã¯ã©ã¹ã® static ãã£ã¼ã«ãããç´æ¥åå¾ããããã«ãã¾ãã
CountryController.java
package ksbysample.webapp.basic.web; import ksbysample.webapp.basic.domain.Country; import ksbysample.webapp.basic.exception.InvalidRequestException; import ksbysample.webapp.basic.service.CountryRepository; import ksbysample.webapp.basic.service.CountryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Locale; @Controller @RequestMapping("/country") public class CountryController { @Autowired private CountryFormValidator countryFormValidator; @Autowired private CountryService countryService; @Autowired private CountryRepository countryRepository; @Autowired private MessageSource messageSource; @InitBinder public void initBinder(WebDataBinder binder) { binder.addValidators(countryFormValidator); } @RequestMapping("/input") public String input(CountryForm countryForm , Model model) { return "country/input"; } @RequestMapping("/input/back") public String inputBack(CountryForm countryForm , RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("countryForm", countryForm); return "redirect:/country/input"; } @RequestMapping("/confirm") public String confirm(@Validated CountryForm countryForm , BindingResult bindingResult , Model model) { if (bindingResult.hasErrors()) { return "country/input"; } // code ã«å ¥åãããæååããã¼ã«æã¤ãã¼ã¿ãç»é²ããã¦ããå ´åã«ã¯ã¨ã©ã¼ã«ãã Country country = countryRepository.findOne(countryForm.getCode()); if (country != null) { bindingResult.reject("countryForm.global.duplicate"); return "country/input"; } return "country/confirm"; } @RequestMapping("/update") public String update(@Validated CountryForm countryForm , BindingResult bindingResult , Locale locale , Model model , HttpServletResponse response) throws IOException, InvalidRequestException { if (bindingResult.hasErrors()) { throw new InvalidRequestException(messageSource.getMessage("common.invalidRequestException.message", new Object[]{bindingResult.toString()}, locale)); } countryService.save(countryForm); return "redirect:/country/complete"; } @RequestMapping("/complete") public String complete() { return "country/complete"; } }
private Constant constant;
ãåé¤ãã¾ããmodel.addAttribute("continentList", constant.CONTINENT_LIST);
ãæ¸ãã¦ããã¨ãããå ¨ã¦åé¤ãã¾ãã
å±¥æ´
2015/03/29
åççºè¡ã