SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-006-处理表单数据(注册、显示用户资料)
一、显示注册表单
1.访问资源
1 @Test 2 public void shouldShowRegistration() throws Exception { 3 SpitterRepository mockRepository = mock(SpitterRepository.class); 4 SpitterController controller = new SpitterController(mockRepository); 5 MockMvc mockMvc = standaloneSetup(controller).build(); 6 mockMvc.perform(get("/spitter/register")) 7 .andExpect(view().name("registerForm")); 8 }
2.Controller
1 package spittr.web; 2 3 import static org.springframework.web.bind.annotation.RequestMethod.*; 4 5 import javax.validation.Valid; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.ui.Model; 10 import org.springframework.validation.Errors; 11 import org.springframework.web.bind.annotation.PathVariable; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 14 import spittr.Spitter; 15 import spittr.data.SpitterRepository; 16 17 @Controller 18 @RequestMapping("/spitter") 19 public class SpitterController { 20 21 private SpitterRepository spitterRepository; 22 23 @Autowired 24 public SpitterController(SpitterRepository spitterRepository) { 25 this.spitterRepository = spitterRepository; 26 } 27 28 @RequestMapping(value="/register", method=GET) 29 public String showRegistrationForm() { 30 return "registerForm"; 31 } 32 33 @RequestMapping(value="/register", method=POST) 34 public String processRegistration( 35 @Valid Spitter spitter, 36 Errors errors) { 37 if (errors.hasErrors()) { 38 return "registerForm"; 39 } 40 41 spitterRepository.save(spitter); 42 return "redirect:/spitter/" + spitter.getUsername(); 43 } 44 45 @RequestMapping(value="/{username}", method=GET) 46 public String showSpitterProfile(@PathVariable String username, Model model) { 47 Spitter spitter = spitterRepository.findByUsername(username); 48 model.addAttribute(spitter); 49 return "profile"; 50 } 51 52 }
3.view
1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 2 <%@ page session="false" %> 3 <html> 4 <head> 5 <title>Spitter</title> 6 <link rel="stylesheet" type="text/css" 7 href="<c:url value="/resources/style.css" />" > 8 </head> 9 <body> 10 <h1>Register</h1> 11 12 <form method="POST"> 13 First Name: <input type="text" name="firstName" /><br/> 14 Last Name: <input type="text" name="lastName" /><br/> 15 Email: <input type="email" name="email" /><br/> 16 Username: <input type="text" name="username" /><br/> 17 Password: <input type="password" name="password" /><br/> 18 <input type="submit" value="Register" /> 19 </form> 20 </body> 21 </html>
二、处理注册
1.测试处理注册
1 @Test 2 public void shouldProcessRegistration() throws Exception { 3 SpitterRepository mockRepository = mock(SpitterRepository.class); 4 Spitter unsaved = new Spitter("jbauer", "24hours", "Jack", "Bauer", "jbauer@ctu.gov"); 5 Spitter saved = new Spitter(24L, "jbauer", "24hours", "Jack", "Bauer", "jbauer@ctu.gov"); 6 when(mockRepository.save(unsaved)).thenReturn(saved); 7 8 SpitterController controller = new SpitterController(mockRepository); 9 MockMvc mockMvc = standaloneSetup(controller).build(); 10 11 mockMvc.perform(post("/spitter/register") 12 .param("firstName", "Jack") 13 .param("lastName", "Bauer") 14 .param("username", "jbauer") 15 .param("password", "24hours") 16 .param("email", "jbauer@ctu.gov")) 17 .andExpect(redirectedUrl("/spitter/jbauer")); 18 19 verify(mockRepository, atLeastOnce()).save(unsaved); 20 }
2.Controller参考上面
3.view: return "redirect:/spitter/" + spitter.getUsername();
4.Dao
1 package spittr.data; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.jdbc.core.JdbcOperations; 8 import org.springframework.jdbc.core.RowMapper; 9 import org.springframework.stereotype.Repository; 10 11 import spittr.Spitter; 12 13 @Repository 14 public class JdbcSpitterRepository implements SpitterRepository { 15 16 private JdbcOperations jdbc; 17 18 @Autowired 19 public JdbcSpitterRepository(JdbcOperations jdbc) { 20 this.jdbc = jdbc; 21 } 22 23 public Spitter save(Spitter spitter) { 24 jdbc.update( 25 "insert into Spitter (username, password, first_name, last_name, email)" + 26 " values (?, ?, ?, ?, ?)", 27 spitter.getUsername(), 28 spitter.getPassword(), 29 spitter.getFirstName(), 30 spitter.getLastName(), 31 spitter.getEmail()); 32 return spitter; // TODO: Determine value for id 33 } 34 35 public Spitter findByUsername(String username) { 36 return jdbc.queryForObject( 37 "select id, username, null, first_name, last_name, email from Spitter where username=?", 38 new SpitterRowMapper(), 39 username); 40 } 41 42 private static class SpitterRowMapper implements RowMapper<Spitter> { 43 public Spitter mapRow(ResultSet rs, int rowNum) throws SQLException { 44 return new Spitter( 45 rs.getLong("id"), 46 rs.getString("username"), 47 null, 48 rs.getString("first_name"), 49 rs.getString("last_name"), 50 rs.getString("email")); 51 } 52 } 53 54 }
二、显示某个用户的资料
1.测试:http://localhost:8080/SpringInAction4_Chapter5_SpringMVC01/spitter/eminem
2.controller写在上面
3.view
1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 2 <%@ page session="false" %> 3 <html> 4 <head> 5 <title>Spitter</title> 6 <link rel="stylesheet" type="text/css" href="<c:url value="/resources/style.css" />" > 7 </head> 8 <body> 9 <h1>Your Profile</h1> 10 <c:out value="${spitter.username}" /><br/> 11 <c:out value="${spitter.firstName}" /> <c:out value="${spitter.lastName}" /><br/> 12 <c:out value="${spitter.email}" /> 13 </body> 14 </html>
You can do anything you set your mind to, man!