一、页面跳转
二、回写数据
下面会详细学习这数据响应的两种方式
返回字符串可以直接返回 返回对象(下面会讲)或集合(不作演示)需要先转为json格式才能返回,也就是我们返回的其实就是json数据
直接返回字符串: 此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转
在UserController类中写入如下:
"/quick") (
public String save() {
return "success";
}
在spring-mvc.xml写入如下:
xxxxxxxxxx
<!--配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<!--上面那行是配置地址前缀,相当于拼接地址,然后,我们在UserController类里面写跳转地址的时候,就不用每次都写全地址-->
<property name="suffix" value=".jsp"></property>
<!--上面那行是配置地址后缀,相当于拼接地址,然后,我们在UserController类里面写跳转地址的时候,就不用每次都写全地址-->
</bean>
上面拼接之后,转发资源地址就是为: /jsp/success.jsp
返回带有前缀的字符串: 转发: forward:/jsp/success.jsp 重定向: redirect:/success.jsp
就是我们上节课结尾学的组件配置的资源解析器,就实现了这节课的'返回字符串形式'的页面跳转
value="/quick2") (
public ModelAndView save2() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","我是huanf,无参,手动new");//username的值需要去success.jsp页面接收一下
modelAndView.setViewName("success"); //这里的success指的是我们会跳转到success.jsp页面
return modelAndView;
}
value="/quick3") (
public ModelAndView save3(ModelAndView modelAndView) {
modelAndView.addObject("username","我是huanf,带参,自动new");//username的值需要去success.jsp页面接收一下
modelAndView.setViewName("success"); //这里的success指的是我们会跳转到success.jsp页面
return modelAndView;
}
value="/quick4") (
public String save4(Model model) {
model.addAttribute("username","我是huanf,直接返回要跳转的页面");
return "success"; //这里的success指的是我们会跳转到success.jsp页面
}
value="/quick5") (
public String save5(HttpServletRequest request) {
request.setAttribute("username","原生的Tomcat产生的对象,这个不是mvc框架提供的,所以不常用");
return "success"; //这里的success指的是我们会跳转到success.jsp页面
}
在Web基础阶段,客户端访问服务器端,如果想直接返回字符串作为响应体返回的话,只需要使用response.getWriter().print("hello world")即可, 那么在Controller中想直接返回字符串,该怎么写,有两种情况的写法,如下
1、通过SpringMVC框架注入的response对象,使用response.getWriter().print("hello world")返回数据,此时不需要视图跳转,方法返回值为void
value="/quick6") (
public void save6(HttpServletResponse response) throws IOException {
//解决中文乱码
response.setContentType("text/html;charset=utf-8");
//response直接返回字符串
response.getWriter().print("我是response回写的字符串");
}
//访问:http:localhost:8080/user/quick6
2、将需要返回的字符串直接返回,而不是返回视图。也就是我们需要通过@ResponseBody注解告诉SpringMVC框架,我给你的这个字符串需要给 我直接在http响应体中返回,而不是帮我拼接地址去找页面。简单说就是别让SpringMVC去找页面,我只是要返回一个字符串在新页面中显示
xxxxxxxxxx
注解的最大作用是不进行页面跳转,直接在本页面展示数据
value="/quick7") (
public String save7() throws IOException {
return "hello world";
}
//访问:http:localhost:8080/user/quick7
value="/quick8") (
public String save8() throws IOException {
User user = new User();
user.setUsername("Tom");
user.setAge(19);
//上面三行拿到的是对象,我们需要使用json转换工具将对象转换成json格式的字符串,再返回。如何使用json转换工具,我们需要在pom.xml文件里面导入坐标
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);//把我们的user对象转换成JSON格式的字符串
return json; //把json格式的字符串显示在页面
//上面那行会在页面输出{"username":"zhangsan","age":18}54
}
//访问:http:localhost:8080/user/quick8
在pom.xml写入如下:
<!--导入json转换工具的包,我们才能使用json转换工具-->
<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>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
xxxxxxxxxx
value="/quick9") (
//在spring-mvc.xml配置文件里面导入json处理器映射器之后,SpringMVC就会自动把User转换成json格式的字符串
public User save9() throws IOException {
User user = new User();
user.setUsername("这个不乱码");
user.setAge(17);
return user;
}
//我们只需要写好一个User类,然后在spring-mvc.xml导处理器映射器的包,就直接能在这里写json数据啦,页面展示的也是标准的json数据
//访问:http:localhost:8080/user/quick9
在spring-mvc.xml配置文件写入如下:
xxxxxxxxxx
<!--配置json处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
我们前面的演示都离不开@ResponseBody注解,它的作用是返回json格式的字符串,但是缺点是配置比较麻烦,我们为了使用这一个注解,需要 在
我们可以额外使用mvc的另一个注解驱动,代替麻烦的配置。如下
xxxxxxxxxx
<mvc:annotation-driven/>
该注解驱动的作用: 在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器称为SpringMVC的三大组件,使用该注解驱动可以自动 加载处理器映射器、处理器适配器,使用注解驱动我们只需要在spring-xml配置文件中写入
<mvc:annotation-driven/>
并添加mvc对应的命名空间
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
即可