SpringMVC获取请求参数
通过ServletAPI获取
通过控制器方法的形参获取请求参数
在方法中添加名称与传过来参数相同的key的形参,就可以自动接收到参数
当形参名称与key不相同时,可以添加@RequestParam(“key”)注解来进行指定对应的参数
@RequestParam
将请求参数与方法的形参绑定
这个注解还有两个属性:
required是否必需,默认为true
defaultValue默认参数值,设置后required失效
@RequsetHeader
将请求头信息与方法的形参绑定
@CookieValue
将cookie数据与方法的形参绑定
通过pojo获取参数
在控制器方法的形参中设置一个pojo,pojo里面的属性名和传过来的参数名一致,会自动获取
参数乱码问题
当获取过任意参数后,再设置httprequest的编码是没有用的
可以使用过滤器,在获取参数前就设置编码
spring中已经封装了过滤器
<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>
处理请求参数的过滤器需要配置到所有过滤器的最前边
域对象共享参数(request请求域)
在Spring中,所有共享参数的方法在最后实现的时候,spring都会把他们封装成一个ModelAndView对象
使用thymeleaf时,可以用th:text="${Attribute名称}"的方式取出数据
1、使用servletAPI
跟web阶段用的一样,在request调用setAttribute
2、使用ModelAndView
官方推荐的使用方式
在DispatcherServlet中,所有方法最终都是以ModelAndView类型封装的
控制器返回类型设置为ModelAndView,然后创建mav对象,在里面添加Object为参数,viewName为跳转到的视图路径
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
/*** ModelAndView有Model和View的功能
* * Model主要用于向请求域共享数据
* * View主要用于设置视图,实现页面跳转 */
ModelAndView mav = new ModelAndView();
//向请求域共享数据
mav.addObject("testScope", "hello,ModelAndView");
// 设置视图,实现页面跳转
mav.setViewName("success"); return mav; }
}
3、使用Model、ModelMap、Map
这三种方法,返回值还是String,用来设置view的路径,然后在形参中设置Model,用来接受放置参数的集合
这三种方法创建的都是同一种对象:org.springframework.validation.support.BindingAwareModelMap
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
域对象共享参数(session会话域和application应用域)
使用servletAPI
在方法上添加HttpSession类型的形参,然后可以用session获取ServletContext对象
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}
thymeleaf获取会话域和应用域的参数
th:text="${session.属性名}"
th:text="${application.属性名}"
SpringMVC的视图
ThymeleafView
在IOC容器中已经配置了一个id为viewrResolver的bean,但是并没有在IOC配置文件中调用,这个bean决定了普通的view的返回类型
@RequestMapping("/testHello")
public String testHello(){
return "hello";
}
thymeleafView其实也是转发视图
貌似只有这种方式转发才能解析页面中 带有 th: 的thymeleaf语法
转发视图
@RequestMapping("/testForward")
public String testForward(){
return "forward:/testHello";
}
重定向视图
@RequestMapping("/testRedirect")
public String testRedirect(){
return "redirect:/testHello";
}
重定向在SpringMVC中会自动添加上下文路径
视图控制器view-controller
<!--path:设置处理的请求地址 view-name:设置请求地址所对应的视图名称 -->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
浙公网安备 33010602011771号