@RequestParam(value = “username”,required = false),flase可以没有参数
/hello,是别人访问本方法的url地址
text,是视图名
Thymeleaf教程
(用于html和控制器传递参数)
Controller.java(控制器页面)
记得要写注解
@Controller
public class IndexController_zhujie {
@RequestMapping("/hello")
public String hello(Model model){
//向页面传递对象
User user=new User();
user.setUserName("李明");
user.setUserPassword("123456");
model.addAttribute(user);
//向页面传递集合
user.setUserName("李明");
user.setUserPassword("123456");
User user2=new User();
user2.setUserName("李明2");
user2.setUserPassword("654321");
List<User> userList=new ArrayList<>();
userList.add(user);
userList.add(user2);
model.addAttribute("userList",userList);
//向页面传递数组
String[]string={"李明","二公子","二少爷"};
model.addAttribute("array",string);
return "text";
}
}
html(视图页面)
顶端要写上此标签(Thymeleaf)
<body>
<h1>text页面</h1>
<h2>对象</h2>
<!--对象-->
<form method="get" action="/hellow">
<p>姓名<input type="text" th:value="${user.userName}"></p>
<p>密码:<input type="text" th:value="${user.userPassword}"></p>
<input type="submit" name="提交">
</form>
<!--集合-->
<h2>集合</h2>
<ul th:each="user:${userList}">
<li th:text="${user.userName}">名字</li>
</ul>
<!--数组-->
<h2>数组</h2>
<ul th:each="a:${array}">
<li th:text="${a}">名字</li>
</ul>
</body>