今日内容
- SpringMVC的数据响应
- 页面跳转
- 会写数据
- SpringMVC获得请求数据
- 获得普通数据
- 获得文件上传
1. SpringMVC的数据响应
1.1 SpringMVC的数据响应方式
-
页面跳转
- 直接返回字符串 return
- 通过ModelAndView对象返回
-
回写数据
- 直接返回字符串 return
- 返回对象或集合
1.2 页面跳转
1. 返回字符串形式
直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。
视图解析器
<bean id="viewRsolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
如返回的是:return index; 怎会拼接上prefix的值为前缀,suffix为后缀/jsp/index.jsp
2. 返回ModelAndView对象
//重定向
@RequestMapping("/quick2")
public ModelAndView quickMethod2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("redirect:index.jsp");
return modelAndView;
}
//转发
@RequestMapping("/quick3")
public ModelAndView quickMethod3(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("forward:/WEB-INF/views/index.jsp");
return modelAndView;
}
3. 向request域存储数据
在进行转发时,往往要向request域中存储数据,在jsp页面中显示,那么Controller中怎样向request域中存储数据呢?
- 通过SpringMVC框架注入的request对象setAttribute()方法设置
- 通过ModelAndView的addObject()方法设置
通过SpringMVC框架注入的request对象setAttribute()方法设置
//本质上是应用了spring会自动帮你创建形参部分的对象
@RequestMapping("/quick")
public ModelAndView quickMethod(HttpServletRequest request){
request.setAttribute("name","zhangsan");
return "index";
}
通过ModelAndView的addObject()方法设置
//存储数据到视图处理对象 取值方法同上 ${username}
@RequestMapping("/quick3")
public ModelAndView quickMethod3(){
//Model 模型 :封装数据
//View 视图 :展示作用数据
ModelAndView modelAndView =new ModelAndView();
//设置视图名称
modelAndView.setViewName("nihao");
//设置模型数据
modelAndView.addObject("username","itcast");
return modelAndView;
}
1.3 回写数据
1. 回写字符串
Web基础阶段,客户端访问服务器端,如果想直接回写字符串作为响应体返回的话,只需要使用
response.getWriter().print(“hello world”) 即可,那么在Controller中想直接回写字符串该怎样呢?
- Spring框架帮你创建response对象
- 通过·@ResponseBody直接返回字符串
1. 通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”) 回写数据,此时不需要视图跳转,业务方法返回值为void。
//本质上是应用了spring会自动帮你创建形参部分的对象
@RequestMapping(value = "/quick5")
public void save5(HttpServletResponse response) throws IOException {
response.getWriter().println("Hellow itcast");
}
2. 将需要回写的字符串直接返回,但此时需要通过@ResponseBody注解告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回。
@RequestMapping(value = "/quick6")
@ResponseBody()//告诉springMVC框架不要进行视图跳转 直接进行数据响应
public String save6() throws IOException {
return "nihao";
}
2. 回写json格式字符串,也可以说是回写对象
- 手动拼接
- ObjectMapper对象进行转换
- SpringMVC中转直接返回对象即可自动转化为json对象
1.客户端与服务器端往往要进行json格式字符串交互,此时我们可以手动拼接json字符串返回。
@RequestMapping(value = "/quick7")
@ResponseBody()//告诉springMVC框架不要进行视图跳转 直接进行数据响应
public String save6() throws IOException {
return "{\"name\":\"zhangsan\",\"age\":18}";
}
2. 使用web阶段学习过的json转换工具jackson进行转换,导入jackson坐标。
<!--jackson的jar包导入-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<