1 package cn.itcast.converter; 2 3 import org.springframework.core.convert.converter.Converter; 4 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 public class DateConverter implements Converter<String, Date> { 10 @Override 11 public Date convert(String source) { 12 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 13 Date date = null; 14 try { 15 date = sdf.parse(source); 16 } catch (ParseException e) { 17 e.printStackTrace(); 18 } 19 return date; 20 } 21 }
1 package cn.itcast.domain; 2 3 public class User { 4 private String name; 5 private int age; 6 7 public User(){} 8 9 public String getName() { 10 return name; 11 } 12 13 public void setName(String name) { 14 this.name = name; 15 } 16 17 public int getAge() { 18 return age; 19 } 20 21 public void setAge(int age) { 22 this.age = age; 23 } 24 25 @Override 26 public String toString() { 27 return "User{" + 28 "name='" + name + '\'' + 29 ", age=" + age + 30 '}'; 31 } 32 }
1 package cn.itcast.vo; 2 3 import cn.itcast.domain.User; 4 5 public class VO { 6 private User[] userList; 7 8 public VO(){} 9 10 public User[] getUserList() { 11 return userList; 12 } 13 14 public void setUserList(User[] userList) { 15 this.userList = userList; 16 } 17 }
1 package cn.itcast.web.controller; 2 3 import cn.itcast.domain.User; 4 import cn.itcast.vo.VO; 5 import org.springframework.stereotype.Component; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.validation.support.BindingAwareModelMap; 9 import org.springframework.web.bind.annotation.RequestBody; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestParam; 12 import org.springframework.web.bind.annotation.ResponseBody; 13 import org.springframework.web.servlet.ModelAndView; 14 15 import java.util.ArrayList; 16 import java.util.Arrays; 17 import java.util.Date; 18 import java.util.List; 19 20 @Controller 21 //@Component 22 public class UserController { 23 /* 24 響應處理,跳轉 25 */ 26 @RequestMapping("/quick1") 27 public String quick1(){ 28 //視圖字符串,可使用絕對路徑和相對路徑 29 return "/mvc.jsp"; 30 } 31 32 @RequestMapping("/quick2") 33 public ModelAndView quick2(){ 34 ModelAndView modelAndView = new ModelAndView(); 35 36 //ViewName即視圖字符串 37 modelAndView.setViewName("/mvc.jsp"); 38 39 //SpringMVC會自動將ModelAndView中存入的數據,存入request域 40 modelAndView.addObject("name", "劉備"); 41 42 return modelAndView; 43 } 44 45 @RequestMapping("/quick3") 46 //由SpringMVC建立ModelAndView 47 public ModelAndView quick3(ModelAndView modelAndView){ 48 //ViewName即視圖字符串 49 modelAndView.setViewName("/mvc.jsp"); 50 51 //SpringMVC會自動將ModelAndView中存入的數據,存入request域 52 modelAndView.addObject("name", "孫權"); 53 54 return modelAndView; 55 } 56 57 //使用Model+視圖字符串,則Model必須由SpringMVC建立。 58 @RequestMapping("/quick4") 59 public String quick4(Model model){ 60 //SpringMVC會自動將ModelAndView中存入的數據,存入request域 61 model.addAttribute("name", "曹操"); 62 return "/mvc.jsp"; 63 } 64 65 //錯誤示例: 返回視圖字符串,本身建立Model的話沒法交給SpringMVC。 66 @RequestMapping("/quick5") 67 public String quick5(){ 68 Model model = new BindingAwareModelMap(); 69 //SpringMVC會自動將ModelAndView中存入的數據,存入request域 70 model.addAttribute("name", "小喬"); 71 return "/mvc.jsp"; 72 } 73 74 /* 75 直接響應數據 76 */ 77 @RequestMapping("/quick6") 78 @ResponseBody 79 public String quick6(){ 80 return "你好 mvc";//?? mvc 81 } 82 83 @RequestMapping("/quick7") 84 @ResponseBody 85 public User quick7(){ 86 User user = new User(); 87 user.setName("劉備"); 88 user.setAge(20); 89 90 return user; 91 } 92 93 @RequestMapping("/quick8") 94 @ResponseBody 95 public List<User> quick8(){ 96 User user = new User(); 97 user.setName("劉備"); 98 user.setAge(20); 99 100 User user2 = new User(); 101 user2.setName("孫權"); 102 user2.setAge(20); 103 104 List<User> userList = new ArrayList<>(); 105 userList.add(user); 106 userList.add(user2); 107 108 return userList; 109 } 110 111 @RequestMapping("/quick9") 112 public String quick9(){ 113 return "mvc"; 114 } 115 116 /* 117 請求處理 118 */ 119 @RequestMapping("/quick10") 120 @ResponseBody 121 public void quick10(String name, Integer age){ 122 System.out.println(name + ":" + age); 123 } 124 125 @RequestMapping("/quick11") 126 @ResponseBody 127 public void quick11(User user){ 128 System.out.println(user); 129 } 130 131 @RequestMapping("/quick12") 132 @ResponseBody 133 public void quick12(String[] strs){ 134 System.out.println(Arrays.toString(strs)); 135 } 136 137 @RequestMapping("/quick13") 138 @ResponseBody 139 public void quick13(VO vo){ 140 System.out.println(Arrays.toString(vo.getUserList())); 141 } 142 143 /* @RequestMapping("/quick14") 144 @ResponseBody 145 public void quick14(@RequestBody List<User> userList){ 146 System.out.println(userList); 147 }*/ 148 149 @RequestMapping("/quick14") 150 @ResponseBody 151 public void quick14(@RequestBody User[] users){ 152 System.out.println(Arrays.toString(users)); 153 } 154 155 @RequestMapping("/quick15") 156 @ResponseBody 157 public void quick15(@RequestParam("name") String username){ 158 System.out.println(username); 159 } 160 161 @RequestMapping("/quick16") 162 @ResponseBody 163 public void quick16(Date date){ 164 System.out.println(date); 165 } 166 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd 13 "> 14 15 <!--組件掃描--> 16 <context:component-scan base-package="cn.itcast.web.controller"></context:component-scan> 17 18 <!--啓用jackson--> 19 <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 20 <property name="messageConverters"> 21 <list> 22 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> 23 </list> 24 </property> 25 </bean>--> 26 27 <!--設置視圖字符串先後綴--> 28 <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 29 <property name="prefix" value="/"></property> 30 <property name="suffix" value=".jsp"></property> 31 </bean>--> 32 33 <!--當SpringMVC沒有匹配成功使用的Servlet處理器--> 34 <!--<mvc:default-servlet-handler></mvc:default-servlet-handler>--> 35 36 <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> 37 38 <!--注入自定義類型轉換器--> 39 <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> 40 <property name="converters"> 41 <list> 42 <bean class="cn.itcast.converter.DateConverter"></bean> 43 </list> 44 </property> 45 </bean> 46 47 <!--mvc註解驅動,默認啓用jackson,使用類型轉換服務--> 48 <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> 49 50 </beans>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>cn.itcast</groupId> 8 <artifactId>spring_mvc_day2</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <!--springmvc--> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-webmvc</artifactId> 16 <version>5.0.5.RELEASE</version> 17 </dependency> 18 19 <!--jackson--> 20 <dependency> 21 <groupId>com.fasterxml.jackson.core</groupId> 22 <artifactId>jackson-databind</artifactId> 23 <version>2.9.8</version> 24 </dependency> 25 </dependencies> 26 27 </project>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 7 <!--SpringMVC核心控制器--> 8 <servlet> 9 <servlet-name>DispatcherServlet</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:spring-mvc.xml</param-value> 14 </init-param> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>DispatcherServlet</servlet-name> 19 <url-pattern>/</url-pattern> 20 </servlet-mapping> 21 22 <!--編碼過濾器--> 23 <filter> 24 <filter-name>CharacterEncodingFilter</filter-name> 25 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 26 <init-param> 27 <param-name>encoding</param-name> 28 <param-value>UTF-8</param-value> 29 </init-param> 30 </filter> 31 <filter-mapping> 32 <filter-name>CharacterEncodingFilter</filter-name> 33 <url-pattern>/*</url-pattern> 34 </filter-mapping> 35 36 </web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>你好 mvc ${name}</h1> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> <script src="/js/jquery-3.3.1.js"></script> <script> $(function () { var users = new Array(); users.push({"name": "劉備"}); users.push({"name": "孫權", age: 20}); //發送異步請求,攜帶對象數據 $.ajax({ type: "POST", url: "/quick14", data: JSON.stringify(users), contentType: "application/json;charset=utf-8" }); }); </script> </head> <body> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/quick13" method="post"> <input type="text" name="userList[0].name"> <input type="text" name="userList[0].age"> <input type="text" name="userList[1].name"> <input type="text" name="userList[1].age"> <input type="submit" value="提交"> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/quick15" method="post"> <input type="text" name="name"> <input type="submit" name="提交"> </form> </body> </html>