springmvc用於處理瀏覽器的視圖,其核心處理文件DispatcherServlet貫穿整個過程。當客戶在瀏覽器上發送請求時,DispatcherServlet截獲該處理,尋找對應的處理器映射器,該映射器再尋找對應的處理器,處理完成後返回ModelAndView對象,再由視圖解析器處理呈現出來。html
springmvc也是在servlet基礎上實現,首先配置web.xml。在其中配置DispatcherServlet,以及對應的springmvc文件,該處理器截獲的路徑。java
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <servlet> 9 <servlet-name>smart</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 <servlet-mapping> 14 <servlet-name>smart</servlet-name> 15 <url-pattern>*.html</url-pattern> 16 </servlet-mapping> 17 18 <context-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:applicationContext.xml</param-value> 21 </context-param> 22 23 <listener> 24 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 25 </listener> 26 27 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:c="http://www.springframework.org/schema/c" 6 xmlns:cache="http://www.springframework.org/schema/cache" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 9 xmlns:jee="http://www.springframework.org/schema/jee" 10 xmlns:lang="http://www.springframework.org/schema/lang" 11 xmlns:mvc="http://www.springframework.org/schema/mvc" 12 xmlns:p="http://www.springframework.org/schema/p" 13 xmlns:task="http://www.springframework.org/schema/task" 14 xmlns:tx="http://www.springframework.org/schema/tx" 15 xmlns:util="http://www.springframework.org/schema/util" 16 xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd 17 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 18 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 20 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd 21 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd 22 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd 23 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd 24 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 25 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd 26 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> 27 28 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 29 p:prefix="/" 30 p:suffix=".jsp" 31 /> 32 33 <context:component-scan base-package="controller"></context:component-scan> 34 35 36 </beans>
接下來建立頁面,此頁面內實現用戶註冊功能,提交到/user.html路徑web
1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 2 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>用戶註冊</title> 8 </head> 9 <body> 10 11 <form method="post" action="<c:url value="/user.html" />"> 12 <table align = "center" border="1" style="border-collapse: collapse;"> 13 <tr> 14 <td colspan="2">用戶註冊</td> 15 </tr> 16 <tr> 17 <td>用戶名:</td> 18 <td><input type="text" name="username" /></td> 19 </tr> 20 <tr> 21 <td>密碼:</td> 22 <td><input type="password" name="username" /></td> 23 </tr> 24 <tr> 25 <td class="tdstyle" colspan="2"> 26 <input type="submit" value="註冊" /> 27 </td> 28 </tr> 29 </table> 30 </form> 31 32 </body> 33 </html>
DispatcherServlet尋找對應的處理器UserController.java。spring自動把用戶輸入的數據封裝到user對象中,在傳給ModelAndView,ModelAndView再傳遞封裝數據和頁面路徑,最終解析到頁面上。spring
1 package controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.servlet.ModelAndView; 7 8 import dao.User; 9 10 @Controller 11 @RequestMapping("/user") 12 public class UserController { 13 14 @RequestMapping(method = RequestMethod.POST) 15 public ModelAndView createUser(User user) { 16 17 ModelAndView view = new ModelAndView(); 18 view.setViewName("userlist"); 19 view.addObject("user", user); 20 return view; 21 } 22 23 }
User爲與用戶輸入的數據對應的對象瀏覽器
1 package dao; 2 3 public class User { 4 5 private String username; 6 private String password; 7 public String getUsername() { 8 return username; 9 } 10 public void setUsername(String username) { 11 this.username = username; 12 } 13 public String getPassword() { 14 return password; 15 } 16 public void setPassword(String password) { 17 this.password = password; 18 } 19 20 21 22 }
信息展現頁面userlist.jspspring-mvc
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>用戶列表</title> 8 </head> 9 <body> 10 11 12 用戶${user.username }建立成功 13 14 </body> 15 </html>