springmvc Demo
springmvc用于处理浏览器的视图,其核心处理文件DispatcherServlet贯穿整个过程。当客户在浏览器上发送请求时,DispatcherServlet截获该处理,寻找对应的处理器映射器,该映射器再寻找对应的处理器,处理完成后返回ModelAndView对象,再由视图解析器处理呈现出来。
springmvc也是在servlet基础上实现,首先配置web.xml。在其中配置DispatcherServlet,以及对应的springmvc文件,该处理器截获的路径。
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>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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路径
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再传递封装数据和页面路径,最终解析到页面上。
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.jsp
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>