
表中的資料未正確顯示...這是我在模板中的 html 頁面
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<form th:action="@{/indexone}">
Filter <input type="text" name="keyword" id="keyword" size="50"
th:value="${keyword}" required /> <input type="submit"
value="Search"></input>
</form>
<table border="2">
<thead>
<tr>
<th>modelNum</th>
<th>companyName</th>
<th>price</th>
</tr>
</thead>
<tbody>
<tr th:each="mobile:${listMobiles}">
<td th:text="#{mobile.modelNum}"></td>
<td th:text="#{mobile.companyName}"></td>
<td th:text="#{mobile.price}"></td>
</tr>
</tbody>
</table>
</body>
</html>
移動存盤庫
package login.example;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface MobileRepository extends JpaRepository<Mobile, String> {
@Query("SELECT mobile FROM Mobile mobile WHERE CONCAT(mobile.modelNum,' ',mobile.companyName,' ',mobile.price) LIKE %?1%")
public List<Mobile> search(String keyword);
}
這里是移動服務
package login.example;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
@Service
public class MobileService {
@Autowired
private MobileRepository repo;
public List<Mobile> listAll(String keyword) {
if (keyword != null) {
return repo.search(keyword);
}else
return repo.findAll();
}
}
這是移動控制器
package login.example;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MobileController {
@Autowired
private MobileService service;
@RequestMapping("/indexone")
public String viewHomePage(Model model, @Param("keyword") String keyword) {
List<Mobile> listMobiles = service.listAll(keyword);
model.addAttribute("listMobiles", listMobiles);
model.addAttribute("keyword", keyword);
return "indexone.html";
}
}
我的移動表無法很好地呈現..我應該怎么做才能正確地從 mysql 呈現表..即使在那之后搜索也能正常作業..資料沒有呈現..這個問題想說什么?它沒有給出例外。 .
uj5u.com熱心網友回復:
您應該使用${...}運算式而不是#{...}運算式。 ${...}是正則變數運算式,而#{...}運算式用于系統/國際化屬性。
<tr th:each="mobile: ${listMobiles}">
<td th:text="${mobile.modelNum}"></td>
<td th:text="${mobile.companyName}"></td>
<td th:text="${mobile.price}"></td>
</tr>
uj5u.com熱心網友回復:
我有替換
<td th:text="#{mobile.modelNum}"></td>
<td th:text="#{mobile.companyName}"></td>
<td th:text="#{mobile.price}"></td>
<td th:text="#{mobile.price}"></td>
和
<td th:text="${mobile.modelNum}"></td>
<td th:text="${mobile.companyName}"></td>
<td th:text="${mobile.price}"></td>
<td th:text="${mobile.price}"></td>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/432218.html
