09009
[Spring] 예제 2 - html escape 본문
html로 인식하고 enter로 인식이 안되는 문제 발생
html escape 외부 라이브러리
html escape 라이브러리
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
pom.xml에 추가
BoardController
@RequestMapping("readContentPage")
public String readContentPage(Model model, int id) {
// 사실 int는 null 값이 선언되지 못하므로 null이면 터진다.
boardService.increaseReadCount(id);
Map<String, Object> map = boardService.getBoard(id);
// html escape
BoardDto boardDto = (BoardDto) map.get("boardDto");
String content = boardDto.getContent();
// import할 때 주의할 것
content = StringEscapeUtils.escapeHtml4(content);
boardDto.setContent(content);
model.addAttribute("data", map);
return "board/readContentPage";
}
개행 적용
content = content.replaceAll("\n", "<br>");
@RequestMapping("readContentPage")
public String readContentPage(Model model, int id) {
// 사실 int는 null 값이 선언되지 못하므로 null이면 터진다.
boardService.increaseReadCount(id);
Map<String, Object> map = boardService.getBoard(id);
// html escape
BoardDto boardDto = (BoardDto) map.get("boardDto");
String content = boardDto.getContent();
// import할 때 주의할 것
content = StringEscapeUtils.escapeHtml4(content);
content = content.replaceAll("\n", "<br>");
boardDto.setContent(content);
model.addAttribute("data", map);
return "board/readContentPage";
}
'Back-End > Spring' 카테고리의 다른 글
[Spring] 예제 2 - 파일 업로드 (0) | 2023.05.23 |
---|---|
[Spring] 예제 2 - 검색 기능 구현 (0) | 2023.05.23 |
[Spring] 예제 2 - 페이징 처리 (0) | 2023.05.22 |
[Spring] 예제 2 - (중요) 카테고리 속성을 적용한 회원가입 구현 수정 (0) | 2023.05.22 |
[Spring] 예제 2 (예제 1에서 기능 추가) (0) | 2023.05.22 |
Comments