Spring MVC:在表格行中显示数据? [英] Spring MVC: Show data in a table row?

查看:207
本文介绍了Spring MVC:在表格行中显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在表格行中显示 data 。但是,它只是显示 var name 作为输出,例如:

 名称|年龄
User1 | {age}

我使用 Spring-MVC 以及 Spring Boot 。我创建了其他映射类,它们的数据在 HTML< p>中正确显示。标签

我的控制台:
$ b

  Controller 
public class MyController {

@RequestMapping(/ age)
public String age(@RequestParam(value =age,required = false,defaultValue =5 )字符串年龄,模型模型){
model.addAttribute(age,age);
返回年龄;


age.html :(使用ThymeLeaf)

 <!DOCTYPE HTML> 
< html xmlns:th =http://www.thymeleaf.org>
< head>
< title>年龄表< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< / head>
< body>
< table border =1>
< tr>
< td>名称< / td>
< td>年龄< / td>
< / tr>
< tr>
< td> User1< / td>
< td> $ {age}< / td>
< / tr>
< / table>
< / body>
< / html>


解决方案

试试这个:

 < table> 
< tr>
< th>名称< / th>
< th>年龄< / th>
< / tr>
< tr>
< td>使用者1< / td>
< td th:text =$ {age}>年龄< / td>
< / tr>
< / table>

如果您有这样的年龄列表:

 列表<整数> listOfAge = new ArrayList<>(Arrays.asList(23,45,45,23,54,23,43)); 
model.addAttribute(listOfAge,listOfAge);

然后试试这个:

 < table> 
< tr>
Sr。否LT; /第>
< th>名称< / th>
< th>年龄< / th>
< / tr>
< tr th:each =age,iterationstatus:$ {listOfAge}>
< td th:text =$ {iterationstatus.count}> 1< / td>
< td>使用者1< / td>
< td th:text =$ {age}>年龄< / td>
< / tr>
< / table>


I am trying to display data in a table row. However it is just showing the var name as output, e.g:

Name | age
User1| {age}

I am using Spring-MVC along with Spring Boot. I have created other mapping classes and their data is showing correctly in HTML <p> tags.

My Controller:

@Controller
public class MyController {

    @RequestMapping("/age")
    public String age(@RequestParam(value="age", required=false, defaultValue="5") String age, Model model) {
        model.addAttribute("age", age);
        return "age";
    }
}

age.html: (uses ThymeLeaf)

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Age Table</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1">
    <tr>
        <td>Name</td>
        <td>Age</td>
    </tr>
    <tr>
        <td>User1</td>
        <td>${age}</td>
    </tr>
</table>
</body>
</html>

解决方案

Try this :

    <table>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr>
            <td>User 1</td>
            <td th:text="${age}"> Age </td>
        </tr>
    </table>

If you have a list of age like this:

    List<Integer> listOfAge = new ArrayList<>(Arrays.asList(23,45,45,23,54,23,43));
    model.addAttribute("listOfAge", listOfAge);

then try this:

    <table>
        <tr>
            <th>Sr. No</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr th:each="age,iterationstatus :${listOfAge}">
            <td th:text="${iterationstatus.count}">1</td>
            <td>User 1</td>
            <td th:text="${age}"> Age </td>
        </tr>
    </table>

这篇关于Spring MVC:在表格行中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆