[SSM_SpringMVC]
SpringMVC简介
SpringMVC技术与Servlet技术功能等同,属于web层开发技术
MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。
SpringMVC基于Java实现MVC模型的轻量级web框架
工作流程
使用SpringMVC技术需要先导入SpringMVC坐标和Servlet坐标
创建SpringMVC控制器类
初始话SpringMVC环境,设定加载对应的bean
初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理请求
AbstractDispatcherServletInitializer类是SpringMVC提供的快速初始化类,提供三个接口方法:
工作流程分析
Bean加载控制 1.SpringMVC对应bean加载 在SpringMVC的配置类SpringMvcConfig中使用注解@ComponentScan,我们只需要将其扫描范围设置到controller即可
2.Spring对应bean加载 因功能不同,在加载Spring控制的bean时要排掉SpringMVC控制的bean
方式一:修改Spring配置类,设定SpringMVC扫描范围为精准范围
1 2 3 4 @Configuration @ComponentScan ({"service" ,"dao" })public class SpringConfig { }
方式二:修改Spring配置类,设定扫描范围,排除掉SpringMVC控制的controller包中的bean
1 2 3 4 5 6 7 8 9 10 11 12 @Configuration @ComponentScan (value="" ,excludeFilters=@ComponentScan .Filter ( type = FilterType.ANNOTATION, classes = Controller.class ) ) public class SpringConfig { }
简化开发
PostMan简介 作用:常用于进行接口测试
请求 GET发送参数 1 http:// localhost/commonParam?name=itcast&age=15
接收参数:
1 2 3 4 5 6 7 8 9 10 @Controller public class UserController {@RequestMapping("/commonParam" ) @ResponseBody public String commonParam(String name,int age){System.out .println("普通参数传递 name ==> " +name); System.out .println("普通参数传递 age ==> " +age); return "{'module':'commonParam'}" ;} }
GET请求中文乱码 如果我们传递的参数中有中文,你会发现接收到的参数会出现中文乱码问题。发送请求: http://localhost/commonParam?name=张三&age=18
出现乱码的原因:Tomcat8.5以后的版本已经处理了中文乱码的问题,但是IDEA中的Tomcat插件目前只到Tomcat7,所以需要修改pom.xml来解决GET请求中文乱码问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <build > <plugins > <plugin > <groupId > org.apache.tomcat.maven</groupId > <artifactId > tomcat7-maven-plugin</artifactId > <version > 2.1</version > <configuration > <port > 80</port > <path > /</path > <uriEncoding > UTF-8</uriEncoding > </configuration > </plugin > </plugins > </build >
POST发送参数
接收方式与get保持一致即可
POST请求中文乱码 解决方案:配置过滤器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class <?>[] getRootConfigClasses() {return new Class [0 ];} protected Class <?>[] getServletConfigClasses() {return new Class []{SpringMvcConfig .class };} protected String [] getServletMappings() {return new String []{"/" };} @Override protected Filter [] getServletFilters() {CharacterEncodingFilter filter = new CharacterEncodingFilter ();filter.setEncoding("UTF-8" ); return new Filter []{filter};} }
五种类型参数传递 普通参数 前端给的是name ,后台接收使用的是userName ,两个名称对不上的解决方法:
使用@RequestParam注解
发送请求与参数:http://localhost/commonParamDifferentName?name=张三&age=18
后台接收参数
1 2 3 4 5 6 7 @RequestMapping("/commonParamDifferentName" ) @ResponseBody public String commonParamDifferentName(@RequestParam("name" ) String userName , int age){System.out .println("普通参数传递 userName ==> " +userName); System.out .println("普通参数传递 age ==> " +age); return "{'module':'common param different name'}" ;}
POJO类型参数
此时需要使用前面准备好的POJO类,先来看下User
1 2 3 4 5 public class User {private String name;private int age;}
1 2 3 4 5 6 7 8 @RequestMapping("/pojoParam" ) @ResponseBody public String pojoParam(User user){System.out .println("pojo参数传递 user ==> " +user); return "{'module':'pojo param'}" ;}
嵌套POJO类型参数
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Address {private String province;private String city;} public class User {private String name;private int age;private Address address;}
1 2 3 4 5 6 7 @RequestMapping("/pojoParam" ) @ResponseBody public String pojoParam(User user){System.out .println("pojo参数传递 user ==> " +user); return "{'module':'pojo param'}" ;}
数组类型参数
1 2 3 4 5 6 7 @RequestMapping("/arrayParam" ) @ResponseBody public String array Param(String[] likes ) { System . out.println("数组参数传递 likes ==> " + Arrays .to String(likes ) );return "{'module':'array param'}" ; }
集合类型参数
1 2 3 4 5 6 7 8 9 @RequestMapping ("/listParam" )@ResponseBody public String listParam (@RequestParam List<String > likes ){System .out .println ("集合参数传递 likes ==> " + likes);return "{'module':'list param'}" ;}
JSON数据传递参数
1 2 3 4 5 <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.9.0</version > </dependency >
1 2 3 4 5 6 @Configuration @ComponentScan ("controller" )@EnableWebMvc public class SpringMvcConfig { }
JSON普通数组
1 2 3 4 5 6 7 @RequestMapping ("/listParamForJson" )@ResponseBody public String listParamForJson (@RequestBody List<String > likes ){System .out .println ("list common(json)参数传递 list ==> " +likes);return "{'module':'list common for json param'}" ;}
JSON对象数据
1 2 3 4 5 6 7 8 { "name" : "itcast" , "age" : 15 , "address" : { "province" : "beijing" , "city" : "beijing" } }
1 2 3 4 5 6 @RequestMapping("/pojoParamForJson" ) @ResponseBody public String pojoParamForJson(@RequestBody User user){System.out .println("pojo(json)参数传递 user ==> " +user); return "{'module':'pojo for json param'}" ;}
JSON对象数组
1 2 3 4 [ {"name" :"itcast" ,"age" :15 }, {"name" :"itheima" ,"age" :12 } ]
1 2 3 4 5 6 @RequestMapping("/listPojoParamForJson" ) @ResponseBody public String listPojoParamForJson(@RequestBody List<User> list){System.out .println("list pojo(json)参数传递 list ==> " +list); return "{'module':'list pojo for json param'}" ;}
日期类型参数传递
1 2 3 4 5 6 7 8 9 10 11 12 @RequestMapping("/dataParam" ) @ResponseBody public String dataParam(Date date,@DateTimeFormat(pattern="yyyy-MM-dd" ) Date date1,@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss" ) Datedate2) System.out .println("参数传递 date ==> " +date); System.out .println("参数传递 date1(yyyy-MM-dd) ==> " +date1); System.out .println("参数传递 date2(yyyy/MM/dd HH:mm:ss) ==> " +date2); return "{'module':'data param'}" ;}
响应
Rest风格
(REST风格而不是规范)RESTful
1 @RestController `可替代` @Controller `+` @ResponseBody
SSM整合