方志朋 2020-04-25
将数据传输给页面
新建一个工程moudle,创建一个demo测试一下环境是否正常
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--contextConfigLocation:指定SpringMVC配置文件位置 classpath:路径下的文件地址 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符编码过滤器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--encoding:指定解决POST请求乱码--> <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> </web-app>
springmvc.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.jiang"></context:component-scan> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
com.jiang.controller.HelloController
package com.jiang.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * @Title: * @author: JiangPeng */ @Controller public class HelloController { @RequestMapping("/hello") public String hello(){ System.out.println("Hello"); return "hello"; } }
剩下的就是web-inf/pages/hello.jsp......
SpringMVC可以在参数上写原生API
@RequestMapping("/handle03") public String handle03(HttpSession session, HttpServletRequest request){ request.setAttribute("reqParam","我是请求域中的"); session.setAttribute("sessionParam","我是session域中的"); return "hello"; }
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 请求:${requestScope.reqParam} session:${sessionScope.sessionParam} </body> </html>
在方法处传入Map、Model、或者ModelMap给这些参数
保存的所有数据都会放在域中
@RequestMapping("/handle01") public String handle01(Map<String,Object> map){ map.put("msg","hello你好"); return "hello"; }
我们测试在hello页面中能获取到哪些数据
pageContext:${pageScope.msg}<br> request:${requestScope.msg}<br> session:${sessionScope.msg}<br> application:${applicationScope.msg}<br>
最终我们可以看到只有request域中获取到了数据:
pageContext:
request:hello你好
session:
application:
@RequestMapping("/handle02") public String handle02(Model model){ model.addAttribute("msg","model你好"); return "hello"; }
通过Model对象 来传递参数
在网页中仍然只能request域获取到
pageContext:
request:model你好
session:
application:
@RequestMapping("/handle03") public String handle03(ModelMap modelMap){ modelMap.addAttribute("msg","modelmap你好"); return "hello"; }
仍然是只能request域获取到
pageContext:
request:modelmap你好
session:
application:
方法的返回值可以变为ModelAndView类型:
既可以包含视图信息(页面地址)也包含模型数据(给页面带的数据)
而且数据是放在请求域中的。
@RequestMapping("/handle04") public ModelAndView handle04(){ // 之前我们return的叫viewName视图名,视图解析器会帮我们拼串得到最后的真实地址 // 现在我们是通过new ModelAndVie(”viewName“) 在viewName中写入我们之前return的字符串,即视图名 ModelAndView mv = new ModelAndView("hello"); mv.addObject("msg","hellomodelandview"); return mv; //或者你也可以不通过有参构造传入viewName而是无参构造 ModelAndView mv = new ModelAndView(); mv.setViewName("hello"); mv.addObject("msg","hellomodelandview"); return mv; }
运行后还是在request域中有数据
给session域中保存数据
@SessionAttributes(value = {"msg","msg1"},types={String.class}) @Controller public class OutputController { @RequestMapping("/handle04") public ModelAndView handle04(){ ModelAndView mv = new ModelAndView("hello"); mv.addObject("msg","hellomodelandview"); return mv; } }
本来只有request中有数据,我们在控制类中加上@SessionAttributes注解以后就可以给session域中获取数据
@SessionAttributes(value = "msg")
但是@SessionAttributes不推荐使用,可能会有异常。
如果想给session保存数据还是推荐原生API
在以前的hibernate使用,现在已经用mybatis不用这个了,不做笔记了
@RequestMapping("/hello") public String hello(Model model){ model.addAttribute("msg","helloView"); return "hello"; }
我们在hello请求中 最后的页面会自动拼上前缀和后缀 /WEB-INF/pages/hello.jsp
那么假设我此时需要访问我项目根路径下的hello.jsp怎么访问?
就是通过../返回上一级,那么因为WEB-INF/pages是两个目录 所以需要 ../../hello
@RequestMapping("/hello") public String hello(Model model){ model.addAttribute("msg","helloView"); return "forward:/hello"; }
@RequestMapping("/hello") public String hello(Model model){ model.addAttribute("msg","helloView"); return "redirect:/hello.jsp"; }
导入jackson的包
@ResponseBody @RequestMapping("/getJson") public Collection<Employee> ajaxGetAll(){ Collection<Employee> all = employeeDao.getAll(); return all; }
返回的json数据中,如果有的数据我不想让他返回就是用该注解
@JsonIgnore private Department department;
那么我们在返回的Json数据中,就不会有该属性的json数据@
在Date上加该注解,自动帮我们转换成我们可以理解的日期格式
@JsonFormat(pattern="yyyy-MM-dd") private Date birth = new Date();