SpringMVC的数据响应方式
页面跳转
返回字符串形式
返回ModelAndView对象
编写UserController.java
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
public String save(){
System.out.println("Controller save running...");
return "success";
}
}
成功测试
设置模型数据
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","itcast");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
public String save(){
System.out.println("Controller save running...");
return "success";
}
}
编写success.jsp
<%--
Created by IntelliJ IDEA.
User: guigui
Date: 2022/4/16
Time: 18:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>success!! ${username}</h1>
</body>
</html>
注意jsp识别代码
<%@page isELIgnored="false" %>
成功测试
返回ModelAndView对象2
编写UserControlle
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick3")
public ModelAndView save3(ModelAndView modelAndView){
modelAndView.addObject("username","liuhongtao");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","taotao");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
public String save(){
System.out.println("Controller save running...");
return "success";
}
}
成功测试
返回ModelAndView对象3
编写UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick4")
public String save4( Model model){
model.addAttribute("username","刘鸿涛");
return "success";
}
@RequestMapping(value = "/quick3")
public ModelAndView save3(ModelAndView modelAndView){
modelAndView.addObject("username","liuhongtao");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","taotao");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
public String save(){
System.out.println("Controller save running...");
return "success";
}
}
成功测试
返回ModelAndView对象4
编写UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick5")
public String save5(HttpServletRequest request){
request.setAttribute("username","刘鸿涛");
return "success";
}
@RequestMapping(value = "/quick4")
public String save4( Model model){
model.addAttribute("username","刘鸿涛");
return "success";
}
@RequestMapping(value = "/quick3")
public ModelAndView save3(ModelAndView modelAndView){
modelAndView.addObject("username","liuhongtao");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick2")
public ModelAndView save2(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","taotao");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
public String save(){
System.out.println("Controller save running...");
return "success";
}
}
成功测试
回写数据
直接返回字符串
编写UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick6")
public void save6(HttpServletResponse response) throws IOException {
response.getWriter().print("hell taotao");
}
}
成功测试
直接返回字符串2
编写UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick7")
@ResponseBody
public String save7(HttpServletResponse response) throws IOException {
return "hello taotao";
}
@RequestMapping(value = "/quick6")
public void save6(HttpServletResponse response) throws IOException {
response.getWriter().print("hell taotao");
}
}
成功测试
直接返回json格式字符串
编写UserController
package com.taotao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick8")
@ResponseBody
public String save8(HttpServletResponse response) throws IOException {
return "{\"username\":\"taotao\",\"age\":18}";
}
@RequestMapping(value = "/quick7")
@ResponseBody
public String save7(HttpServletResponse response) throws IOException {
return "hello taotao";
}
@RequestMapping(value = "/quick6")
public void save6(HttpServletResponse response) throws IOException {
response.getWriter().print("hell taotao");
}
}
成功测试
直接返回json格式字符串(解耦)
编写user类
package com.taotao.domain;
@SuppressWarnings({"all"})
public class User {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", age=" + age +
'}';
}
}
导入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>
编写UserController
package com.taotao.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick9")
@ResponseBody
public String save9() throws IOException {
User user = new User();
user.setUsername("lisa");
user.setAge(20);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
return json;
}
@RequestMapping(value = "/quick8")
@ResponseBody
public String save8(HttpServletResponse response) throws IOException {
}
@RequestMapping(value = "/quick7")
@ResponseBody
public String save7(HttpServletResponse response) throws IOException {
return "hello taotao";
}
@RequestMapping(value = "/quick6")
public void save6(HttpServletResponse response) throws IOException {
response.getWriter().print("hell taotao");
}
}
成功测试
返回对象或集合
编写UserController
package com.taotao.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@SuppressWarnings({"all"})
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping(value = "/quick10")
@ResponseBody
public User save10() throws IOException {
User user = new User();
user.setUsername("lisa");
user.setAge(20);
return user;
}
@RequestMapping(value = "/quick9")
@ResponseBody
public String save9() throws IOException {
User user = new User();
user.setUsername("lisa");
user.setAge(20);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
return json;
}
@RequestMapping(value = "/quick8")
@ResponseBody
public String save8(HttpServletResponse response) throws IOException {
return "{\"username\":\"taotao\",\"age\":18}";
}
@RequestMapping(value = "/quick7")
@ResponseBody
public String save7(HttpServletResponse response) throws IOException {
return "hello taotao";
}
@RequestMapping(value = "/quick6")
public void save6(HttpServletResponse response) throws IOException {
response.getWriter().print("hell taotao");
}
}
编写Spring-mvc.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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.taotao">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<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>
</beans>
成功测试
返回对象或集合(解耦)
编写Spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--Controller的组件扫描-->
<context:component-scan base-package="com.taotao">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置内部资源视图解释器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- /jsp/success.jsp-->
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置处理器映射器-->
<!-- <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>-->
<!-- mvc的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
成功测试
|