0
我工作的一個小項目,努力學習thymeleaf值從一個表中的列。 我有3個表時刻表其中包含一個ID,分配ID作爲外鍵和其他的東西,其中包含作業和ID和項目ID爲外鍵和項目包含與ID和項目名稱。Thymeleaf如何顯示基於從另一個表
我需要在一個HTML顯示時間表,很容易的,但是我想顯示的項目名稱,而不是分配ID列。
我已經有一個選擇,其中調用所需的列我的服務類,但我怎麼能告訴thymeleaf閱讀而不是時間表尋找它該列。
這裏是我的代碼
HTML
<table class="table table-bordered">
<thead>
<tr>
<th>id</th>
<th>assignment</th>
<th>date</th>
<th>number of hours</th>
</tr>
</thead>
<tbody>
<tr th:each = "timetable: ${timetables}">
<td th:text="${timetable.timetableId}">45</td>
<td th:text="${timetable.assignmentId}">vasi</td>
<td th:text="${timetable.date}">1 ian</td>
<td th:text="${timetable.hoursWorked}">3000</td>
</tr>
</tbody>
</table>
服務類
@Autowired
JdbcTemplate template;
public List<Timetable> findAll(String loginname) {
String sql = " SELECT timetables.timetableId, timetables.assignmentId, timetables.date, timetables.hoursWorked, users.username, projects.projectName FROM timetables INNER join assignments on assignments.assignmentId = timetables.assignmentId INNER JOIN users on users.userId = assignments.userId " +
"INNER JOIN projects on assignments.projectId = projects.projectId where username= ?";
RowMapper<Timetable> rm = new RowMapper<Timetable>() {
@Override
public Timetable mapRow(ResultSet resultSet, int i) throws SQLException {
Timetable timetable = new Timetable(resultSet.getInt("timetableId"),
resultSet.getInt("assignmentId"),
resultSet.getDate("date"),
resultSet.getInt("hoursWorked"));
return timetable;
}
};
return template.query(sql, rm, loginname);
}
控制器
@Autowired
TimetableService service;
@Autowired
AssignmentsService serv;
@RequestMapping(value = {"/Timetable"}, method = RequestMethod.GET)
public String index(Model md){
org.springframework.security.core.Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String loginname = auth.getName();
md.addAttribute("timetables", service.findAll(loginname));
return "Timetable";
}
完美的作品。非常感謝你。 對於除了上述我需要一個場在我的模型類中調用項目,getter和setter它將來參考和使用的字符串的項目,而不是assignmentId – Adi
另一個構造@Adi高興的是,它的工作! –