1.参数对应
java端:
@RequestMapping(value="/test",method = RequestMethod.POST)
public String submit(String name,Model model){
//String name = request.getParameter("name");
mailClient.send("dongkun01@vcredit.com", name);
model.addAttribute("username",name);
return "account/testhome";
}
HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:social="http://spring.io/springsocial"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<head>
<title>用户登录</title>
</head>
<body>
<div id="content" layout:fragment="content">
<form action="#" method="POST" th:action="@{/app/account/test}">
<p>
登录名:<input name="name" type="text"></input>
</p>
<p>
<button type="submit">登录</button>
</p>
</form>
</div>
</body>
</html>
2.HttpServletRequest处理参数
java端:
@RequestMapping(value="/test",method = RequestMethod.POST)
public String submit(HttpServletRequest request){
String name = request.getParameter("name");
return "account/testhome";
}
HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:social="http://spring.io/springsocial"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<head>
<title>用户登录</title>
</head>
<body>
<div id="content" layout:fragment="content">
<form action="#" method="POST" th:action="@{/app/account/test}">
<p>
登录名:<input name="name" type="text"></input>
</p>
<p>
<button type="submit">登录</button>
</p>
</form>
</div>
</body>
</html>
3.thymeleaf对应
@RequestMapping(value="/register", method=RequestMethod.POST)
public String RegisterFormSubmit(@ModelAttribute CredentialUser user,Model model){
register.registerAccount(user.getUsername(),"", "");
auth.register(user.getUsername(), user.getPassword());
model.addAttribute("user", user);
return "account/home";
}
HTML:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:social="http://spring.io/springsocial"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<head>
<title>用户注册</title>
</head>
<body>
<div id="content" layout:fragment="content">
<form action="#" method="POST" th:action="@{/app/account/register}" th:object="${user}">
<p>
登录名:<input th:field="*{username}" type="text"></input>
</p>
<p>
密码:<input th:field="*{password}" type="password"></input>
</p>
<p>
</p>
<!-- <p> -->
<!-- 验证码:<input th:field="verifyCode" type="text"></input><img alt="验证码" src=""></img> -->
<!-- </p> -->
<p>
<button type="submit">注册</button>
</p>
</form>
</div>
</body>
</html>

本文介绍Spring MVC框架中如何处理HTTP请求参数,包括直接使用方法参数接收及通过HttpServletRequest对象获取参数的方式,并展示了如何利用Thymeleaf模板引擎实现表单字段与Java对象之间的自动绑定。
384

被折叠的 条评论
为什么被折叠?



