1 回答
TA贡献1942条经验 获得超3个赞
希望这将是你的一个起点。确保将 html 文件放在 /资源/模板 下。
我更改了一些您的注册html和结果.html如下,它们仍然不完美(避免使用内联样式并使用外部样式表!
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>My Jmml</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body style="background-color: #2B2B2B">
<br /><br />
<h2 style="text-align:center">Contact Information</h2>
<!-- Input Form -->
<!--/*@thymesVar id="signup" type="com.mainconfig.controller1"*/-->
<form th:action="@{/signup}" th:object="${signup}" method="post">
<div align="center">
<label>Email Address</label><br /><br />
<!--/*@thymesVar id="email" type="String"*/-->
<input type="text" th:field="*{email}" placeholder="Email" />
<br />
<br />
<input class="submitbutton" type="submit" value="Submit"/>
<br />
</div>
</form>
</body>
结果.html看起来像这样
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Thank you for your submission!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Thank you for your submission!</h1>
<p th:text="'Email: ' + ${email}" />
<a href="/index">Submit another message</a>
</body>
</html>
我还创建了一个表单对象,如果需要,请在此处添加其他字段
public class SignUpForm {
//you can put some annotations here if you want for validating the email
//for e.g @NotEmpty or a @Pattern(regexp to validate the email)
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
最后是你的控制器。我将电子邮件从注册帖子请求传递到结果.html通过flash属性:
@Controller
public class Controller1 {
@RequestMapping(value = "/signup", method= RequestMethod.GET)
public String signupForm(@ModelAttribute("signup") SignUpForm form) {
return "/signup";
}
@RequestMapping(value = "/signup", method= RequestMethod.POST)
public String signupSubmit(@ModelAttribute("signup") SignUpForm form, RedirectAttributes redirectAttributes) {
//validate form first -> check bindingResult documentation
//do what you need with your form object
redirectAttributes.addFlashAttribute("email", form.getEmail());
return "redirect:/result";
}
@RequestMapping(value = "/result", method= RequestMethod.GET)
public String result() {
return "/result";
}
}
添加回答
举报