1 回答

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
希望這將是你的一個(gè)起點(diǎn)。確保將 html 文件放在 /資源/模板 下。
我更改了一些您的注冊(cè)html和結(jié)果.html如下,它們?nèi)匀徊煌昝溃ū苊馐褂脙?nèi)聯(lián)樣式并使用外部樣式表!
<!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>
結(jié)果.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>
我還創(chuàng)建了一個(gè)表單對(duì)象,如果需要,請(qǐng)?jiān)诖颂幪砑悠渌侄?/p>
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;
}
}
最后是你的控制器。我將電子郵件從注冊(cè)帖子請(qǐng)求傳遞到結(jié)果.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";
}
}
添加回答
舉報(bào)