为什么 thymeleaf 不能正确渲染表中的数据
表中的数据未正确显示...这是模板
<?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>
MobileRepository
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);
}
中的我的 html 页面这是 MobileService
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();
}
}
这里是 mobileController
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 渲染表正确..即使在那之后搜索也运行良好..数据未呈现..这个问题想说什么?它没有给出异常..
data in table is not shown properly...here is my html page in template
<?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>
MobileRepository
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);
}
Here is MobileService
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();
}
}
here is mobileController
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";
}
}
my mobile table is not rendering well..what should i do to render table from mysql properly..searching is working well even after that..data is not rendered..what is this problem trying to say?it does not give exception..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
${...}
表达式,而不是#{...}
表达式。${...}
是常规变量表达式,而#{...}
表达式用于系统/国际化属性。You should be using
${...}
expressions and not#{...}
expressions.${...}
are regular variable expressions while#{...}
expressions are for system/internationalization properties.我已经替换
为
i have replace
with