Thymeleaf基础

一.显示变量

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<title>info</title>
	</head>
	<body>
		学号:<span th:text="${person.id+1}">0000</span><br />
		姓名:<span th:text="${person.name}">0000</span><br />
		性别:<span th:text="${person.sex?'男':'女'}">0000</span><br />
	</body>
</html>

要求Person类里面属性是public的,或者有Getter方法。 

@Controller
public class TestController
{
	@RequestMapping("info.do")
	public String myTest(Model model)
	{
		Person p=new Person(100,"lisong",true);
		model.addAttribute("person",p);
		return "info";//全路径  /template/info.html,相关配置在spring-mvc.xml里
	}
}

二.显示Map

@Controller
public class TestController
{
	@RequestMapping("info.do")
	public String myTest(Model model)
	{
		Map<String, Object> person=new HashMap<String,Object>();
		person.put("id", 100);
		person.put("name", "lisong");
		person.put("sex", true);
		
		model.addAttribute("person",person);
		return "info";//全路径  /template/info.html,相关配置在spring-mvc.xml里
	}
}

后端使用同上

三.显示List

@Controller
public class TestController
{
	@RequestMapping("info.do")
	public String myTest(Model model)
	{
		List<Person> p=new ArrayList<Person>();
		p.add(new Person(100,"lihua",true));
		p.add(new Person(101,"liuming",true));
		p.add(new Person(102,"xupan",false));
		
		model.addAttribute("personList",p);
		return "info";//全路径  /template/info.html,相关配置在spring-mvc.xml里
	}
}
		<table>
			<tr th:each="row,start : ${personList}">
				<td th:text="${start.index+1}">000</td>
				<td th:text="${row.id}">000</td>
				<td th:text="${row.name}">000</td>
				<td th:text="${row.sex?'男':'女'}">000</td>
			</tr>
		</table>

   row : 表示这一行

   stat : 表示上下文,比如 stat.index 表示当前遍历索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值