Last active
July 30, 2019 09:57
-
-
Save A-pZ/af69319f89d78e197f6f768f9bd1965c to your computer and use it in GitHub Desktop.
ThymeleafのDiarectサンプル:TextAreaに入力された改行つきテキストを HTMLのbreakをつけて出力する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html xmlns:th="http://www.thymeleaf.org"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="viewport" | |
| content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | |
| <title>Title</title> | |
| <link rel="stylesheet" th:href="@{/webjars/bootstrap/{version}/css/bootstrap.min.css(version=${@webjarsConfig.bootstrap})}" /> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <form th:action="@{/textarea}" method="post"> | |
| <textarea name="sample" cols="30" rows="3" th:text="${sample}"></textarea> | |
| <input type="submit" value="送信" class="btn btn-primary"> | |
| </form> | |
| <th:block th:utext="${#textarea.convertBreak(sample)}" /> | |
| </div> | |
| <script th:src="@{/webjars/jquery/{version}/jquery.min.js(version=${@webjarsConfig.jquery})}"></script> | |
| <script th:src="@{/webjars/bootstrap/{version}/js/bootstrap.min.js(version=${@webjarsConfig.bootstrap})}"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.apz.config; | |
| import java.util.Arrays; | |
| import java.util.stream.Collectors; | |
| import org.springframework.util.StringUtils; | |
| public class TextArea { | |
| final static String LINE_SEPARATOR = "\r\n"; | |
| public String convertBreak(final Object object) { | |
| if (object == null) { | |
| return null; | |
| } | |
| String value = object.toString(); | |
| if (StringUtils.isEmpty(value)) { | |
| return value; | |
| } | |
| return Arrays.stream(value.split(LINE_SEPARATOR)).map(v -> v + "<br />").collect(Collectors.joining()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.apz.controller; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.PostMapping; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestParam; | |
| import org.springframework.web.servlet.ModelAndView; | |
| @RequestMapping("/textarea") | |
| @Controller | |
| public class TextAreaController { | |
| @GetMapping("") | |
| public ModelAndView display(ModelAndView mnv) { | |
| mnv.setViewName("textarea"); | |
| return mnv; | |
| } | |
| @PostMapping("") | |
| public ModelAndView post(ModelAndView mnv, @RequestParam String sample) { | |
| mnv.addObject("sample", sample); | |
| mnv.setViewName("textarea"); | |
| return mnv; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.apz.config; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| import org.thymeleaf.context.IExpressionContext; | |
| import org.thymeleaf.dialect.IExpressionObjectDialect; | |
| import org.thymeleaf.expression.IExpressionObjectFactory; | |
| public class TextAreaDiarect implements IExpressionObjectDialect { | |
| static final String name = "textarea"; | |
| HashSet<String> names = new HashSet<String>() { | |
| {add(name);} | |
| }; | |
| @Override | |
| public String getName() { | |
| return "textAreaDiarect"; | |
| } | |
| @Override | |
| public IExpressionObjectFactory getExpressionObjectFactory() { | |
| return new IExpressionObjectFactory() { | |
| @Override | |
| public Set<String> getAllExpressionObjectNames() { | |
| return names; | |
| } | |
| @Override | |
| public Object buildObject(IExpressionContext context, String expressionObjectName) { | |
| if (name.equals(expressionObjectName)) return new TextArea(); | |
| return null; | |
| } | |
| @Override | |
| public boolean isCacheable(String expressionObjectName) { | |
| return true; | |
| } | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.github.apz.config; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| @Configuration | |
| public class ThymeleafConfig { | |
| @Bean | |
| public TextAreaDiarect textAreaDiarect() { | |
| return new TextAreaDiarect(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment