【问题标题】:Display datas in Thymeleaf在 Thymeleaf 中显示数据
【发布时间】:2013-07-07 21:23:02
【问题描述】:

我正在使用 Spring MVC 3 和 Thymeleaf。我想在表格中显示字符串,但没有结果(空白表格单元格)。我究竟做错了什么?我的控制器类:

@Controller
@RequestMapping(value="table", method = RequestMethod.GET)
public class TableController {
@Autowired
DaneEndpoint daneEndpoint;
public String tabela(Model model){
model.addAttribute("tytul", daneEndpoint.getAll().get(0));
model.addAttribute(model);
 return "table";
    } 
 }


和html页面(table.html):

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <h3 align="center">table</h3> 
    <div align="left">
    <form action="table.html"  th:object="${tytul}" method="get">
      <fieldset> 
        <div align="center">
        <table border="1">
          <thead>
             <tr>
               <th th:text="">tytul</th>
             </tr>
          </thead>
          <tbody>
             <tr>
                <td th:text=""><span th:text="#{table.tytul}"/></td>
             </tr>
          </tbody>
        </table> 
        </div>
      </fieldset>    
    </form> 
    </div> 
</body>
</html>


感谢您的帮助。

【问题讨论】:

    标签: spring spring-mvc thymeleaf


    【解决方案1】:

    &lt;span th:text="#{table.tytul}"/&gt;

    ` 从消息属性中获取值,所以如果 table.html 直接在浏览器中打开,它将是空白的

    如果这个页面是通过应用程序访问的,那么,

    &lt;th th:text=""&gt;

    is not valid thymeleaf expression syntax(因为空字符串),会出现解析错误..和

    &lt;td th:text=""&gt;&lt;span th:text="#{table.tytul}"/&gt;&lt;/td&gt;

    包含 span 作为 td 的子级,因此 th:text 不需要包含在 td 中,因为 td 中的 th:text 最终将替换..

    进一步的元标记没有关闭..

    试试这个

    > <!DOCTYPE html>  <html xmlns:th="http://www.thymeleaf.org">  <head>
    >     <title></title>
    >     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    > </head> <body>
    >     <h3 align="center">table</h3> 
    >     <div align="left">
    >     <form action="table.html"  th:object="${tytul}" method="get">
    >       <fieldset> 
    >         <div align="center">
    >         <table border="1">
    >           <thead>
    >              <tr>
    >                <th >tytul</th>
    >              </tr>
    >           </thead>
    >           <tbody>
    >              <tr>
    >                 <td><span th:text="#{table.tytul}"/></td>
    >              </tr>
    >           </tbody>
    >         </table> 
    >         </div>
    >       </fieldset>    
    >     </form> 
    >     </div>  </body> </html>
    

    如果在浏览器中直接打开时需要显示表格上的值以进行原型设计,试试这个

    > <!DOCTYPE html>  <html xmlns:th="http://www.thymeleaf.org">  <head>
    >     <title></title>
    >     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body>
    >     <h3 align="center">table</h3> 
    >     <div align="left">
    >         <form action="table.html"  th:object="${tytul}" method="get">
    >             <fieldset> 
    >                 <div align="center">
    >                     <table border="1">
    >                         <thead>
    >                             <tr>
    >                                 <th >tytul</th>
    >                             </tr>
    >                         </thead>
    >                         <tbody>
    >                             <tr>
    >                                 <td><span th:text="#{table.tytul}"/> <span th:remove="all">content</span></td>
    > 
    >                             </tr>
    >                         </tbody>
    >                     </table> 
    >                 </div>
    >             </fieldset>    
    >         </form> 
    >     </div>  </body> </html>
    

    【讨论】:

      【解决方案2】:

      好像您缺少结尾 &lt;/tr&gt; 和结尾 &lt;/div&gt;。尝试添加。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 2019-11-11
        • 1970-01-01
        • 2022-09-23
        • 2020-11-17
        • 1970-01-01
        相关资源
        最近更新 更多