目录
页面跳转
1.返回字符串形式
直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转。
@RequestMapping(value="/quick",method = RequestMethod.GET)
public String save(){
System.out.println("Controller save running....");
return "success";
}
<bean id="InternalResourceView class=" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
转发: forward: /wEB-INE/views/ index.jsp
重定向: redirect : / index.jsp
2.ModeAndVIew形式
方式1:
@RequestMapping(value="/quick2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
//设置模型数据
modelAndView.addObject("username","itPig");
//设置视图名称
modelAndView.setViewName("success");
return modelAndView;
}
方式2:
@RequestMapping(value="/quick3")
public ModelAndView save3(ModelAndView modelAndView){
//设置模型数据
modelAndView.addObject("username","itPig1");
//设置视图名称
modelAndView.setViewName("success");
return modelAndView;
}
方式3:
@RequestMapping(value="/quick4")
public String save4(Model model){
//设置模型数据
model.addAttribute("username","真好玩");
return "success";
}
回写数据
1.直接返回字符串
web阶段需要用到response.getWriter0.print,而在mvc会更简单
通过SpringMVC框架注入的response对象,使用response.getWriter(.print(“hello world”)回写数据,此时不需要视图跳转,业务方法返回值为void。
@RequestMapping(value="/quick5")
public void save5(HttpServletResponse response) throws IOException {
response.getWriter().println("你真牛");
}
@RequestMapping(value="/quick6")
@ResponseBody //告诉方法不进行跳转,直接进行数据响应
public String save6() throws IOException {
return "hello hhh";
}
2.json返回(返回对象)
方式一:使用js转换工具将对象转换为json格式字符串
pom.xml导入
<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>
@RequestMapping(value="/quick7")
@ResponseBody //告诉方法不进行跳转,直接进行数据响应
public String save7() throws IOException {
UserName userName=new UserName(19,"zwq");
//使用js转换工具将对象转换为json格式字符串
ObjectMapper objectMapper=new ObjectMapper();
String json = objectMapper.writeValueAsString(userName);
return json;
}
方式2:
使用mvc配置处理器映射器来进行返回
配置
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
@RequestMapping(value="/quick8")
@ResponseBody //告诉方法不进行跳转,直接进行数据响应
public UserName save8() throws IOException {
UserName userName=new UserName();
userName.setAge(19);
userName.setUsername("aaa");
//使用js转换工具将对象转换为json格式字符串
return userName;
}
方式3:
通过mvc的注解将配置给简略化
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
@RequestMapping(value="/quick8")
@ResponseBody //告诉方法不进行跳转,直接进行数据响应
public UserName save8() throws IOException {
UserName userName=new UserName();
userName.setAge(19);
userName.setUsername("aaa");
//使用js转换工具将对象转换为json格式字符串
return userName;
}
若有收获,就点个赞吧