A string containing a new line entered from textarea is stored in a DB with a new line code and
I would like to display it as th:text on another page with a new line.
When I output CSV of values registered with new lines from the input form, I found a new line code\n.
Also, when viewing existing data in textarea of the input form, it is displayed with a new line.
My DB is MySQL.
How to write a new line in Thymeleaf on the page you want to display When I tried to convert the new line code of the string to <br/>
and output it, an error occurred.
If you do nothing and display it as th:text, it somehow appears as a space instead of a new line.
Error Message (Page I Want to View)
Causeed by: org.springframework.expression.spel.SpelEvaluationException:EL1004E:Method call:Method split(java.lang.String,java.lang.Integer) cannot be found on type jp.co.itc.mbo.entity.CoGoal
Caused by: org.thymeleaf.exception.TemplateProcessingException: Exception evaluation SpringEL expression: "cogoal.split('\r\n|\r|\n',-1)" (template: "goals/goal_list" - line 32, col 17)
Affected Source Code
cogoal.html (Input Page)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF-8">
<title>Company Objectives </title>
</head>
<body>
<h1>Edit Company Objectives</h1>
<form method="post" th:action="@{/cogoal/complete}">
<table>
<tr>
<th>Fiscal year</th>
<th>Company Goals</th>
</tr>
<trth:object="${cogoal}">
<td>
<pth:text="*{year}">/p> input type="hidden" name="year"
th:value="*{year}">
</td>
<td><textarea name="cogoal" th:field="*{cogoal}"cols="40"
rows="8" required wrap="hard">/textarea>/td>
<tr>
</table>
<br><input type="submit" value="Update"/>
</form>
<div class=return>
<button onclick="location.href='/admin/menu'">Back</button>
</div>
</body>
</html>
Controller (input page)
@RequestMapping("admin/cogoalinput")
public String coGoalInput (Model model) {
// View Existing Objectives
CoGoal cogoal=cogoalservice.findCurrent();
if(cogoal==null){
return "admins/cogoal_new";
}
model.addAttribute("cogoal",cogoal);
return "admins/cogoal";
}
@RequestMapping(path="/cogoal/complete", method=RequestMethod.POST)
public String createCoGoal (Principal principal, CoGoalForm cogoalform) {
// ID of the person who is currently logged in
Authentication auth=(Authentication) principal;
UserMaster loginuser=(UserMaster) auth.getPrincipal();
// Get me some time
Timestamp timestamp = new Timestamp (System.currentTimeMillis());
// Set delete_flg to true for existing goal
CoGoal cogoalold = cogoalsservice.findCurrent();
cogoalold.setDeleteflg(true);
cogoalold.setUpdatedat(timestamp);
cogoalold.setUpdatedby(loginuser.getId());
cogoalservice.save(cogoalold);
// Create a new goal
CoGoal cogoal = new CoGoal();
cogoal.setYear(cogoalform.getYear());
cogoal.setCogoal(cogoalform.getCogoal());
cogoal.setCreatedat(timestamp);
cogoal.setDeleteflg(false);
cogoal.setUpdatedat(timestamp);
cogoal.setUpdatedby(loginuser.getId());
cogoalsservice.save(cogoal);
return "redirect: /admin/cogoalinput";
}
goal_list.html (page you want to view)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"th:fragment="">
<head>
<metacharset="UTF-8">
<title>Target List</title>
<link th:href="@{/css/style.css}"rel="stylesheet"/>
<scriptth:src="@{/js/mboapp.js}" type="text/javascript"></script>
</head>
<body>
<h1>List of company objectives</h1>
<table>
<tr>
<th>Fiscal year</th>
<th>Company Goals</th>
<th>Partial Goals </th>
<th>Team Goals</th>
</tr>
<tr>
<!--th:get one line of object -->
<td>
<pth:if="${cogoal}"th:text="${cogoal.year}">/p>
<pth:if="${cogoal}==null">Not configured</p>
</td>
<td>
<p>
<th:blockth:if="${cogoal}">
<!--Anti-Nulpo-->
<th:blockth:each="line:${cogoal.split('\r\n|\r|\n',-1)}">
<!--Split with newline code and loop -->
<th:blockth:text="${line}"/>
<br of >
</th:block>
</th:block>
</p>
<pth:if="${cogoal}==null">Not configured</p>
</td>
<td><pth:if="${deptgoal}"th:text="${deptgoal.deptgoal}">/p>
<pth:if="${deptgoal}==null">Not configured</p>/td>
<!--<td th:text="${deptgoal.deptgoal}">/td>-->
<td>
<pth:if="${teamgoal}"th:text="${teamgoal.teamgoal}">/p>
<pth:if="${teamgoal}==null">Not configured</p>
</td>
<!--<td th:text="${teamgoal.teamgoal}">/td>-->
</tr>
</table>
</body>
</html>
Controller (Page I Want to View)
@RequestMapping("/{id}")
public String showGoal(@PathVariableIntegerid,Modelmodel){
// Obtaining User Information
UserMaster usermaster=usermasterservice.findOne(id);
model.addAttribute("userid", usermaster);
// Method for displaying company objectives
CoGoal cogoal=cogoalservice.findCurrent();
model.addAttribute("cogoal",cogoal);
// Method for displaying partial objectives
DeptMaster userdeptid=usermaster.getDeptid();
DeptGoal deptgoal=deptgoalsservice.findCurrentOne(userdeptid.getId());
model.addAttribute("deptgoal", deptgoal);
// Method for displaying team objectives
TeamMaster userTeamid=usermaster.getTeamid();
TeamGoal teamgoal=teamgoalsservice.findCurrentOne(userteamid.getId());
model.addAttribute("teamgoal", teamgoal);
return "goals/goal_list";
}
·The → value surrounded by textarea instead of the p tag of the page you want to display has become blank
·A new line code was written directly into the DB → it was considered as a string
No matter how many hours I look into it, I don't know and I'm really in trouble.
I'm a beginner, but I appreciate your cooperation.
"Based on the error, ""Cogoal doesn't have a method to split,"" so I think it's better to give them the ""String"" type."
Specifically, shouldn't it be <th:blockth:each="line:${cogoal.cogoal.split('\r\n|\r|\n',-1)}">
?
Also, HTML uses <br>
tags to express new lines, so this problem occurs, but I think \n
will be reflected as it is if you enclose it with the <pre>
tag.
© 2024 OneMinuteCode. All rights reserved.