1.Spring MVC概述:
Spring MVC是Spring提供的一个强大而灵活的web框架。借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单。这些控制器一般不直接处理请求,而是将其委托给Spring上下文中的其他bean,通过Spring的依赖注入功能,这些bean被注入到控制器中。
Spring MVC主要由DispatcherServlet、处理器映射、处理器(控制器)、视图解析器、视图组成。他的两个核心是两个核心:
处理器映射:选择使用哪个控制器来处理请求
视图解析器:选择结果应该如何渲染
通过以上两点,Spring MVC保证了如何选择控制处理请求和如何选择视图展现输出之间的松耦合。
2.spring MVC配置及使用
(1)需要使用的包(spring的lib包下找)
(2)配置:
在src下: 建立 Spring Bean Configuration File文件(xml文件)名字没有特殊要求
需要勾选的几个项 beans context mvc
beans.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!--自动扫描 --> <context:component-scan base-package="com.*" /> <!--开启Spring MVC 的注解驱动--> <mvc:annotation-driven/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/page/"></property> <!--请求的前缀--> <property name="suffix" value=".jsp"></property> <!-- 请求的后缀 --> </bean> </beans>
我们还需要在web.xml中配置,使项目启动时,加载上面的配置文件才能执行
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 中文过滤器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 同struts一样,也是需要拦截请求 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载时,需要装载内容--> <init-param> <param-name>contextConfigLocation</param-name><!-- 这个名字是固定的 --> <param-value>classpath:beans.xml</param-value> </init-param> <!-- 1,启动时加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern><!-- 拦截所有以.do结尾的请求 --> </servlet-mapping> </web-app>
以上SpringMVC基本就配置完成了
测试一下:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <a href="test.do">测试链接</a> </body> </html>
建立一个类,加入注解
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller//注解 public class PersonControler { //页面跳转 @RequestMapping("test")//加入注解当页面访问对应的请求时(test.do),会自动找到这个类 private String test(){ return "success";//根据前缀跟后缀跳转到那个页面 } }
跳转成功
传输字符串和数字参数
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <a href="test1.do?str=aaaa&num=123">测试链接</a> </body> </html>
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller//注解 public class PersonControler { //传值(字符串、数字) @RequestMapping("test1") private String test1(@RequestParam("str")String str,@RequestParam("num")int num){ System.out.println(str+" "+num); return "success"; } }
传日期型参数
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <a href="test2.do?date=20170414">测试链接</a> </body> </html>
package com.controller; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller//注解 public class PersonControler { //传日期型参数 @RequestMapping("test2") public String test2(@RequestParam("date")@DateTimeFormat(pattern="yyyyMMdd")Date date){ System.out.println(date); return "error"; } }
传实体类
先写一个实体类
package com.model; import java.util.Date; public class Person { private Integer id; private String name; private Date brithday; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", brithday=" + brithday + "]"; } public Date getBrithday() { return brithday; } public void setBrithday(Date brithday) { this.brithday = brithday; } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 传递实体类 只需要传成员即可 例如实体类中有成员 id name直接写id就行--> <form action="test3.do"> <input type="text" name="id"> <input type="text" name="name"> <input type="submit" value="提交(实体类)"> </form> </body> </html>
package com.controller; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.model.Person; @Controller//注解 public class PersonControler { //传实体类对象 @RequestMapping("test3") public String test3(Person p){ //传实体类,参数不用加注解@RequestParam System.out.println(p); return "error"; } }
传递数组
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="test4.do"> <input type="text" name="strs[]"> <input type="text" name="strs[]"> <input type="submit" value="提交(数组)"> </form> </body> </html>
package com.controller; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.model.Person; @Controller//注解 public class PersonControler { //传递数组 @RequestMapping("test4") public String test5(@RequestParam("strs[]")String[] strs) { for(String s : strs) { System.out.println(s); } return "success"; } }
传递带有日期型参数的实体类
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="test5.do"> <input type="text" name="id"> <input type="text" name="name"> <input type="text" name="brithday"> <input type="submit" value="提交(带日期行实体类)"> </form> </body> </html>
package com.controller; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.model.Person; @Controller//注解 public class PersonControler { //带日期型参数的实体类 @RequestMapping("test5") public String test6(Person p) { System.out.println(p); return "success"; } }
还需在实体类加一个注解@DateTimeFormat(pattern="yyyyMMdd")
必须有这个包
package com.model; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; public class Person { private Integer id; private String name; @DateTimeFormat(pattern="yyyyMMdd") private Date brithday; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", brithday=" + brithday + "]"; } public Date getBrithday() { return brithday; } public void setBrithday(Date brithday) { this.brithday = brithday; } }