返回值为void
此方法需要引入一个json依赖
com.alibaba
fastjson
1.2.28
web.xml文件
/p>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
Archetype Created Web Application
CharactorEncoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
CharactorEncoding
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc06Annotion.xml
1
springmvc
/
public class Student {
private String name;
private Integer age;
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
@Controller
public class ThridAnnotion {
/**
* 返回void json
* @param response
* @throws IOException
*/
@RequestMapping("/json")
public void Jack(HttpServletResponse response) throws IOException {
List list=new ArrayList();
Student student=new Student();
student.setName("李四");
student.setAge(20);
Student student1=new Student();
student1.setName("张三");
student1.setAge(22);
list.add(student);
list.add(student1);
String json = JSON.toJSONString(list);
//将json转发到浏览器
response.getWriter().write(json);
}
}
springmvc06Annotion.xml文件
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
">
访问方式:
关于找一找教程网
本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[SpringMVC中的返回值问题之一返回void]http://www.zyiz.net/tech/detail-131302.html