1. 程式人生 > >構建Spring web 應用程序 (三)

構建Spring web 應用程序 (三)

auto BE lib www itl last pub oos bean

處理表單

一、首先寫一個表單

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"
> <title>Insert title here</title> </head> <body> <h1>Register</h1> <form method="post"> First Name: <input type="text" name="firstName"/><br/> last Name: <input type="text" name="lastName"/><br/> UserName: <input type="text" name="userName"
/> Password: <input type="text" name="password"/> <input type="submit" value="register"/> </form> </body> </html>

註意這裏的 form 標簽並沒有設置 action 屬性,這種情況下當表單提交時他會提交到與展現時相同的url路徑上,即它會提交到 /spitter/register 上。 所以我們在該Controller中添加一個處理POST請求的 register 方法

二、然後編寫對應的控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import data.Repository; import entity.Spittle; @Controller @RequestMapping("/spitter") public class SpitterController { private Repository repository; @Autowired public SpitterController(Repository repository) { this.repository = repository; } @RequestMapping(value="/register", method=RequestMethod.GET) public String showRegistrationForm() { return "registerForm"; } @RequestMapping(value="/register", method=RequestMethod.POST) public String processRegistration(Spittle spittle) { repository.save(spittle); return "redirect:/spitter/" + spittle.getUserName(); } @RequestMapping(value="/{userName}", method=RequestMethod.GET) public String redirectPage(@PathVariable String userName, Model model) { System.out.println("6666666666666"); Spittle spittle = repository.findByUserName(userName); model.addAttribute(spittle); return "profile"; } }

profile.jsp 頁面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Your Profile</h1>
<c:out value="${spitter.username}"/><br/>
<c:out value="${spitter.username}"/><br/>
<c:out value="${spitter.username}"/><br/>
</body>
</html>

校驗表單

使用JSR-303 Validation 來校驗

(1)首先改造實體類

....
@NotNull @Size(min
=5, max=16) //非空,5到16個字符 private String userName; @NotNull @Size(min=5, max=16) //非空,5到16個字符 private String firstName; @NotNull @Size(min=5, max=16) //非空,5到16個字符 private String lastName; @NotNull @Size(min=5, max=25) //非空,5到25個字符 private String password; ....

(2)然後改造控制器中的方法

    .....
@RequestMapping(value="/register", method=RequestMethod.POST) public String processRegistration(@Valid Spittle spittle, Errors errors) { System.out.println(errors.hasErrors()+""); if (errors.hasErrors()) { return "registerForm"; } repository.save(spittle); return "redirect:/spitter/" + spittle.getUserName(); }
......

需要的jar包有 classmate-1.0.0、hibernate-jpa-2.1-api-1.0.2.Final、hibernate-validator-5.1.0.Final、jboss-logging-3.3.2.Final、joda-time-2.1、jsoup-1.7.1、paranamer-2.5.5、validation-api-2.0.1.Final

當參數校驗不通過的時候會返回到registerForm頁面

構建Spring web 應用程序 (三)