邻水县规划和建设局 网站,网页版传奇世界什么组合最好,微信小程序编辑,wordpress怎么禁止评论1、Spring MVC简介#xff1a;
MVC是一种软件架构的思想#xff0c;将软件按照模型、视图、控制器来划分
M#xff1a;Model#xff0c;模型层#xff0c;指工程中的JavaBean#xff0c;作用是处理数据
JavaBean分为两类#xff1a; 一类称为实体类Bean#xff1a;专…1、Spring MVC简介
MVC是一种软件架构的思想将软件按照模型、视图、控制器来划分
MModel模型层指工程中的JavaBean作用是处理数据
JavaBean分为两类 一类称为实体类Bean专门存储业务数据的如 Student、User 等 一类称为业务处理 Bean指 Service 或 Dao 对象专门用于处理业务逻辑和数据访问。
VView视图层指工程中的html或jsp等页面作用是与用户进行交互展示数据
CController控制层指工程中的servlet作用是接收请求和响应浏览器 MVC的工作流程 用户通过视图层发送请求到服务器在服务器中请求被Controller接收Controller调用相应的Model层处理请求处理完毕将结果返回到ControllerController再根据请求处理的结果找到相应的View视图渲染数据后最终响应给浏览器 实际上Spring MVC 对原生的Servlet进行封装然后成为一个功能更强大的前端控制器。
特点 Spring 家族原生产品与 IOC 容器等基础设施无缝对接 基于原生的Servlet通过了功能强大的前端控制器DispatcherServlet对请求和响应进行统一处理 表述层各细分领域需要解决的问题全方位覆盖提供全面解决方案 代码清新简洁大幅度提升开发效率 内部组件化程度高可插拔式组件即插即用想要什么功能配置相应组件即可 性能卓著尤其适合现代大型、超大型互联网项目要求
2、Spring MVC配置:
1默认方式配置 此配置作用下SpringMVC的配置文件默认位于WEB-INF下默认名称为servlet-name-servlet.xml例如以下配置所对应SpringMVC的配置文件位于WEB-INF下文件名为springMVC-servlet.xml。
!-- 配置SpringMVC的前端控制器对浏览器发送的请求统一进行处理 --
servletservlet-namespringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
/servlet
servlet-mappingservlet-namespringMVC/servlet-name!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求/* 可以匹配所有的请求的路径方式--url-pattern//url-pattern
/servlet-mapping 2扩展配置方式 可通过init-param标签设置SpringMVC配置文件的位置和名称通过load-on-startup标签设置SpringMVC前端控制器DispatcherServlet的初始化时间。
?xml version1.0 encodingUTF-8?
web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0!--配置SpringMVC的前端控制器 --servletservlet-nameSpringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class!-- 使用这个标签可以配置SpringMVC配置文件的位置和名称--init-paramparam-namecontextConfigLocation/param-name!--类路径--param-valueclasspath:springMVC.xml/param-value/init-param!--将前端控制器DispatcherServlet的初始化时间提前到服务器启动时这样是为了服务器初始化的时间--load-on-startup1/load-on-startup/servletservlet-mappingservlet-nameSpringMVC/servlet-name!--设置springMVC的核心控制器所能处理的请求的请求路径/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是/不能匹配.jsp请求路径的请求--url-pattern//url-pattern/servlet-mapping
/web-app
url-pattern标签中使用 / 和 /* 的区别 / 所匹配的请求可以是/login或.html或.js或.css方式的请求路径但是 / 不能匹配.jsp请求路径的请求因此就可以避免在访问jsp页面时该请求被DispatcherServlet处理从而找不到相应的页面。 /* 则能够匹配所有请求例如在使用过滤器时若需要对所有请求进行过滤就需要使用 /* 的写法。
3配置视图解析器
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!-- 扫描组件--context:component-scan base-packagecom.songzhishu.MVC/!-- 配置Thymeleaf视图解析器 可以配置多个 然后根据优先级 --bean idviewResolver classorg.thymeleaf.spring5.view.ThymeleafViewResolver!--设置视图解析器的优先器--property nameorder value1/!--编码方式--property namecharacterEncoding valueUTF-8/property nametemplateEngine!--内部bean 为templateEngine属性赋值--bean classorg.thymeleaf.spring5.SpringTemplateEngineproperty nametemplateResolver!--内部bean 为templateResolver属性赋值--bean classorg.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver!-- 视图前缀 --property nameprefix value/WEB-INF/templates//!-- 视图后缀 --property namesuffix value.html/property nametemplateMode valueHTML5/property namecharacterEncoding valueUTF-8 //bean/property/bean/property/bean
/beans
3、RequestMapping注解 从注解名称上我们可以看到RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来建立映射关系。SpringMVC 接收到指定的请求就会来找到在映射关系中对应的控制器方法来处理这个请求。
1存在的位置
RequestMapping标识一个类设置映射请求的请求路径的初始信息
RequestMapping标识一个方法设置映射请求请求路径的具体信息
Controller
RequestMapping(/test)
public class RequestMappingController {//此时请求映射所映射的请求的请求路径为/test/testRequestMappingRequestMapping(/testRequestMapping)public String testRequestMapping(){return success;}
}
多个控制器RequestMapppering匹配到的地址是唯一的
2RequestMapping注解的value属性
RequestMapping注解的value属性通过请求的请求地址匹配请求映射
RequestMapping注解的value属性是一个字符串类型的数组表示该请求映射能够匹配多个请求地址所对应的请求
RequestMapping注解的value属性必须设置至少通过请求地址匹配请求映射
controller控制器
Controller
public class RequestMappingController {RequestMapping(value {/testRequestMapping,/test})public String success(){return success;}
}
这的意思就是多个映射匹配的目标的是同一个请求地址。
3RequestMapping注解的method属性只满足一个就可以
RequestMapping注解的method属性通过请求的请求方式get或post匹配请求映射
RequestMapping注解的method属性是一个RequestMethod类型的数组表示该请求映射能够匹配多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性但是请求方式不满足method属性则浏览器报错405Request method POST not supported
Controller
public class RequestMappingController {RequestMapping(value {/testRequestMapping,/test},method {RequestMethod.POST,RequestMethod.GET})public String success(){return success;}
}
这的意思就是多个映射匹配的目标的是同一个请求地址同时请求的方式是post和get两种 1、对于处理指定请求方式的控制器方法SpringMVC中提供了RequestMapping的派生注解 处理get请求的映射--GetMapping 处理post请求的映射--PostMapping 处理put请求的映射--PutMapping 处理delete请求的映射--DeleteMapping 相同的请求地址的情况下可以使用注解表示不同的请求方式 4RequestMapping注解的params属性必须同时满足
RequestMapping注解的params属性通过请求的请求参数匹配请求映射
RequestMapping注解的params属性是一个字符串类型的数组可以通过四种表达式设置请求参数和请求映射的匹配关系
param要求请求映射所匹配的请求必须携带param请求参数
!param要求请求映射所匹配的请求必须不能携带param请求参数
paramvalue要求请求映射所匹配的请求必须携带param请求参数且paramvalue
param!value要求请求映射所匹配的请求必须携带param请求参数但是param!value
RequestMapping(value {/testParam},method {RequestMethod.GET, RequestMethod.POST},params {username,password!123456})public String testParam(){return success;} 使用param的话要所有都满足才可以完成跳转大概意思就是必须携带这username的参数然后携带password参数并且携带的值不能是123456。
5RequestMapping注解的headers属性同时满足
RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射
RequestMapping注解的headers属性是一个字符串类型的数组可以通过四种表达式设置请求头信息和请求映射的匹配关系
header要求请求映射所匹配的请求必须携带header请求头信息
!header要求请求映射所匹配的请求必须不能携带header请求头信息
headervalue要求请求映射所匹配的请求必须携带header请求头信息且headervalue
header!value要求请求映射所匹配的请求必须携带header请求头信息且header!value
若当前请求满足RequestMapping注解的value和method属性但是不满足headers属性此时页面显示404错误即资源未找到
6SpringMVC支持ant风格的路径(模糊匹配)
表示任意的单个字符
RequestMapping(value /a?c/testPut ,method RequestMethod.PUT)
表示可以匹配 aac abc 等等
*表示任意的0个或多个字符
RequestMapping(value /a*c/testPut ,method RequestMethod.PUT) 表示可以匹配 ac aaaaaac abvvvvvdfwerc 等等
**表示任意的一层或多层目录
RequestMapping(value /**/testPut ,method RequestMethod.PUT) 表示可以匹配 ac a/a/aa/a/a/c abv/v/v/v/vd/f/w/er/c 等等
注意在使用**时只能使用/**/xxx的方式
7SpringMVC支持路径中的占位符重点
原始方式/deleteUser?id1
rest方式/deleteUser/1 SpringMVC路径中的占位符常用于RESTful风格中当请求路径中将某些数据通过路径的方式传输到服务器中就可以在相应的RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据在通过PathVariable注解将占位符所表示的数据赋值给控制器方法的形参。 使用占位符必须传参不传会报错
4、SpringMVC获取请求参数
1通过ServletAPI获取
RequestMapping(/testParam)
public String testParam(HttpServletRequest request){String username request.getParameter(username);String password request.getParameter(password);System.out.println(username:username,password:password);return success;
} 2通过控制器方法的形参获取请求参数 在控制器方法的形参位置设置和请求参数同名的形参当浏览器发送请求匹配到请求映射时在DispatcherServlet中就会将请求参数赋值给相应的形参。直接请求的数据作为匹配时传递的形参。
RequestMapping(/testParam)public String testParam(String username, String password){System.out.println(username:username,password:password);return success;} 注 若请求所传输的请求参数中有多个同名的请求参数此时可以在控制器方法的形参中设置字符串数组或者字符串类型的形参接收此请求参数 若使用字符串数组类型的形参此参数的数组中包含了每一个数据 若使用字符串类型的形参此参数的值为每个数据中间使用逗号拼接的结果 form th:action{/paramtest/testParam} methodpost用户名: input typetext nameusername br密码: input typepassword namepassword br爱好: input typecheckbox namehobby valuesing唱input typecheckbox namehobby valuedance跳input typecheckbox namehobby valueraprapinput typecheckbox namehobby valueplay打篮球brinput typesubmit value测试多个参数的形式
/form RequestMapping(value /**/testParam,method RequestMethod.POST)public String testParam(String username, String password ,String[] hobby){System.out.println(username:username,password:password,hobby:hobby.toString());return success;} 3RequestParam
RequestParam是将请求参数和控制器方法的形参创建映射关系
RequestParam注解一共有三个属性
value指定为形参赋值的请求参数的参数名required设置是否必须传输此请求参数默认值为true若设置为true时则当前请求必须传输value所指定的请求参数若没有传输该请求参数且没有设置defaultValue属性则页面报错400Required String parameter xxx is not present若设置为false则当前请求不是必须传输value所指定的请求参数若没有传输则注解所标识的形参的值为nulldefaultValue不管required属性值为true或false当value所指定的请求参数没有传输或传输的值为时则使用默认值为形参赋值
这个可以应对请求参数和形参不一致的时候可以使用
//RequestParamRequestMapping(value /**/testParam,method RequestMethod.POST)public String testParam(RequestParam(value user_name) String username,String password,RequestParam(value hobby,defaultValue rap) String[] hobby){System.out.println(username:username,password:password,hobby: Arrays.toString(hobby));return success;}
4RequestHeader
RequestHeader是将请求头信息和控制器方法的形参创建映射关系
RequestHeader注解一共有三个属性value、required、defaultValue用法同RequestParam
5CookieValue
CookieValue是将cookie数据和控制器方法的形参创建映射关系
CookieValue注解一共有三个属性value、required、defaultValue用法同RequestParam
//RequestParamRequestMapping(value /**/testParam, method RequestMethod.POST)public String testParam(RequestParam(value user_name) String username,String password,RequestParam(value hobby, defaultValue rap) String[] hobby,RequestHeader(value kk,required true ,defaultValue haha) String host,CookieValue(value JSESSIONID) String JSESSIONID) {System.out.println(username: username ,password: password ,hobby: Arrays.toString(hobby),host:host);System.out.println(JSESSIONID:JSESSIONID);return success;}
6通过POJO获取请求参数 可以在控制器方法的形参位置设置一个实体类类型的形参此时若浏览器传输的请求参数的参数名和实体类中的属性名一致那么请求参数就会为此属性赋值。
form th:action{/paramtest/testpojo} methodpost用户名input typetext nameusernamebr密码input typepassword namepasswordbr性别input typeradio namesex valueman男input typeradio namesex valuewoman女br年龄input typetext nameagebr邮箱input typetext nameemailbrinput typesubmit
/form RequestMapping(/**/testpojo)public String testPOJO(User user){System.out.println(user);return success;}
7解决获取请求参数的乱码问题 关于乱码的问题都是编码的方式和解码的方式不一致导致的对于get请求参数中的中文乱
码的问题Tomcat8开始都已经解决啦就是只要是get请求不需要额外的指定编码。但是post没有
解决方式可以使用过滤器然后经行编码的转变。
!--配置springMVC的编码过滤器--
filterfilter-nameCharacterEncodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-paraminit-paramparam-nameforceResponseEncoding/param-nameparam-valuetrue/param-value/init-param
/filter
filter-mappingfilter-nameCharacterEncodingFilter/filter-nameurl-pattern/*/url-pattern
/filter-mapping
5、域对象共享数据
1使用ServletAPI向request域对象共享数据
//使用ServletAPI来向request对象共享数据RequestMapping(value (/**/testRequestByServletAPI))public String testRequestByServletAPI(HttpServletRequest request) {//获取共享数据 request.getAttribute()//删除共享数据 request.removeAttribute();//设置共项数据 key valuerequest.setAttribute(name, 张胜男);return success;}
2使用ModelAndView向request域对象共享数据 //通过ModelAndView向Request域对象共性数据RequestMapping(/**/testModelAndView)public ModelAndView testModelAndView() {ModelAndView mav new ModelAndView();//处理模型数据 共享数据mav.addObject(username, 李四);//设置视图名称 跳转页面mav.setViewName(success);return mav;}
3使用Model向request域对象共享数据
//这种方式的话,使用model来向Request域中共享数据,但是不进行视图解析RequestMapping(/**/testModel)public String testModel(Model model) {model.addAttribute(username, 王五);return success;}
4使用map向request域对象共享数据
//使用map向request域对象共享数据RequestMapping(/**/testMap)public String testMap(MapString,Object map) {map.put(username,孙六);return success;}
5使用ModelMap向request域对象共享数据
//使用ModelMap向request域对象共享数据RequestMapping(/**/testModelMap)public String testModelMap(ModelMap modelMap) {modelMap.addAttribute(username,赵七);return success;}
6向session域共享数据
RequestMapping(/testSession)public String testSession(HttpSession session){session.setAttribute(username, 小明);return success;}
7向application域共享数据
RequestMapping(/testApplication)
public String testApplication(HttpSession session){
ServletContext application session.getServletContext();
application.setAttribute(password, hello);
return success;
}
前端测试页面:
a th:href{/scope/testRequestByServletAPI}通过ServletAPI向Request共享数据/abr
a th:href{/scope/testModelAndView}通过ModelAndView向Request域对象共性数据/abr
a th:href{/scope/testModel}使用model来向Request域中共享数据,但是不进行视图解析/abr
a th:href{/scope/testMap}使用map来向Request域中共享数据/abr
a th:href{/scope/testModelMap}使用ModelMap向request域对象共享数据/abr
a th:href{/scope/testSession}通过ServletAPI向session域对象共享数据/abr
a th:href{/scope/testApplication}向application域共享数据/abr