为什么 thymeleaf 不能正确渲染表中的数据

发布于 2025-01-09 08:37:12 字数 3432 浏览 0 评论 0原文

输入图片这里的描述

表中的数据未正确显示...这是模板

<?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 /> &nbsp; <input type="submit"
            value="Search"></input> &nbsp;

    </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 渲染表正确..即使在那之后搜索也运行良好..数据未呈现..这个问题想说什么?它没有给出异常..

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2025-01-16 08:37:13

您应该使用 ${...} 表达式,而不是 #{...} 表达式。 ${...} 是常规变量表达式,而 #{...} 表达式用于系统/国际化属性。

<tr th:each="mobile: ${listMobiles}">
  <td th:text="${mobile.modelNum}"></td>
  <td th:text="${mobile.companyName}"></td>
  <td th:text="${mobile.price}"></td>
</tr>

You should be using ${...} expressions and not #{...} expressions. ${...} are regular variable expressions while #{...} expressions are for system/internationalization properties.

<tr th:each="mobile: ${listMobiles}">
  <td th:text="${mobile.modelNum}"></td>
  <td th:text="${mobile.companyName}"></td>
  <td th:text="${mobile.price}"></td>
</tr>
请帮我爱他 2025-01-16 08:37:13

我已经替换

     <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>

i have replace

     <td th:text="#{mobile.modelNum}"></td>
                    <td th:text="#{mobile.companyName}"></td>
                    <td th:text="#{mobile.price}"></td>
      <td th:text="#{mobile.price}"></td>

with

 <td th:text="${mobile.modelNum}"></td>
                    <td th:text="${mobile.companyName}"></td>
                    <td th:text="${mobile.price}"></td>
      <td th:text="${mobile.price}"></td>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文