码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC中的返回值问题之一返回void

时间:2017-12-16 23:08:09      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:d3d   int   patch   dep   ret   archetype   引入   write   ons   

 

返回值为void

此方法需要引入一个json依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>

web.xml文件

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>


<!--编码过滤器-->
<filter>
<filter-name>CharactorEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharactorEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--配置前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc06Annotion.xml</param-value>
</init-param>

<!--意思:Tomact启动,就将servlet创建好放入内存中了-->
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

技术分享图片

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<Student> list=new ArrayList<Student>();
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文件

<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
">
<!--扫描包-->
<context:component-scan base-package="cn.sjl.day03Annotion"></context:component-scan>
<!--注解驱动-->
<mvc:annotation-driven/>
</beans>

访问方式:
技术分享图片

 





 




SpringMVC中的返回值问题之一返回void

标签:d3d   int   patch   dep   ret   archetype   引入   write   ons   

原文地址:http://www.cnblogs.com/sujulin/p/7771618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!