前面演示的方法都是返回的字符串,用于视图解析器拼接响应视图的路径,
controller中方法的返回类型如下:
controller 方法返回字符串也称为逻辑视图名,通过视图解析器解析为物理视图地址。
物理视图地址 = 视图解析器前缀 + 逻辑视图名 + 视图解析器后缀
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
bean>
@RequestMapping("/saveAccount")
public String saveAccount(User user){
System.out.println(user);
return "success";
}
最终返回的物理视图地址:/WEB-INF/pages/ + success + .jsp = /WEB-INF/pages/success.jsp
很少用,默认会查找请求路径结尾的jsp资料
@RequestMapping("/void")
public void returnVoid(){
System.out.println("返回值void...");
}
ModelAndView 是 SpringMVC 为我们提供的一个对象,该对象也可以用作控制器方法的返回值。
该对象中有两个方法:
(String attributeName, @Nullable Object attributeValue)
保存键值对数据到ModelMap中,可以传递给前端页面展示。
setViewName(@Nullable String viewName)
设置逻辑视图名称,等同于return “success”;
/**
* Add an attribute to the model.
* @param attributeName name of the object to add to the model (never {@code null})
* @param attributeValue object to add to the model (can be {@code null})
* @see ModelMap#addAttribute(String, Object)
* @see #getModelMap()
*/
public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
getModelMap().addAttribute(attributeName, attributeValue);
return this;
}
/**
* Set a view name for this ModelAndView, to be resolved by the
* DispatcherServlet via a ViewResolver. Will override any
* pre-existing view name or View.
*/
public void setViewName(@Nullable String viewName) {
this.view = viewName;
}
@RequestMapping(value = "/model/view",method = RequestMethod.GET)
public ModelAndView modelAndView(ModelAndView modelAndView){
System.out.println("测试 modelAndView...");
User user = new User();
user.setName("tom");
user.setAge(22);
//保存数据
modelAndView.addObject("clazz","大数据");
modelAndView.addObject("user",user);
//设置视图名称
modelAndView.setViewName("success");
//返回ModelAndView对象
return modelAndView;
}
<body>
<h2>sccessh2>
class: ${clazz}
username: ${user.name}
age: ${user.age}
body>
使用@ResponseBody注解
该注解用于将 Controller 的方法返回的对象,通过 HttpMessageConverter 接口转换为指定格式的
数据如:json、xml 等,默认转为json格式,再通过 Response 响应给客户端 。
使用json格式的数据进行交互,也是目前前后端分离开发最主要的方式。
Springmvc 默认用 MappingJacksonHttpMessageConverter 对 json 数据进行转换,需要加入
jackson 的包(如果已经添加过,不要重复添加)
<jackson-version>2.11.0jackson-version>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>${jackson-version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>${jackson-version}version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>${jackson-version}version>
dependency>
将pojo对象转为json格式响应给客户端
@RequestMapping(value = "/testResponseBody",method = RequestMethod.POST)
public @ResponseBody User testResponseBody(){
System.out.println("使用 @ResponseBody响应json数据 ...");
User user = new User();
user.setId(1001);
user.setMoney(100.1F);
user.setName("tom");
user.setAge(22);
return user;
}