SpringMVC从Controller中获取json数据
2015-03-27 17:53
435 查看
web.xml配置文件:
springMVC配置文件:
实体类User:
方式一:
方式二:需要加入jackson-all-1.6.4.jar
jsp页面(方式一):
jsp页面(方式二
):
项目截图:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/hello-servlet.xml</param-value> </init-param> <!-- 设置启动优先级 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- filter用来设置编码 --> <filter> <filter-name>CharacterFilter</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> </filter> <filter-mapping> <filter-name>CharacterFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springMVC配置文件:
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 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-3.1.xsd"> <context:component-scan base-package="com.spring.json.controller"></context:component-scan> <!-- 开启注解模式 --> <mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
实体类User:
package com.spring.json.entity; public class User { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }Controller控制器:
方式一:
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; import com.spring.json.entity.User; @Controller public class UserController { @RequestMapping(value="/user.do") public String toUser(){ System.out.println("ok"); return "userList"; } //方式一 @RequestMapping(value="/getUserList.do") public String getUserList(HttpServletResponse response){ response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); List<User> userList=getUsers(); Gson gson = new Gson(); String json=gson.toJson(userList); System.out.println("json------"+json); PrintWriter out=null; try { out=response.getWriter(); out.write(json); out.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ if(out!=null){ out.close(); } } return "userList"; } private List<User> getUsers(){ List<User> users=new ArrayList<User>(); User u1=new User(); u1.setUserName("张三"); u1.setPassword("123456"); User u2=new User(); u2.setUserName("李四"); u2.setPassword("ls123"); User u3=new User(); u3.setUserName("王五"); u3.setPassword("ww888"); users.add(u1); users.add(u2); users.add(u3); return users; } }
方式二:需要加入jackson-all-1.6.4.jar
package com.spring.json.controller; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; import com.spring.json.entity.User; @Controller public class UserController { @RequestMapping(value="/user.do") public String toUser(){ System.out.println("ok"); return "userList"; } //方式二 /** * @responsebody表示该方法的返回结果直接写入HTTP response body中 一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径, 加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。 比如异步获取json数据,加上@responsebody后,会直接返回json数据 */ @RequestMapping("/getUserList.do") @ResponseBody public Map<String, Object> getUserList(){ Map<String, Object> result=new HashMap<String, Object>(); List<User> userlist=getUsers(); result.put("users",userlist); return result; } private List<User> getUsers(){ List<User> users=new ArrayList<User>(); User u1=new User(); u1.setUserName("张三"); u1.setPassword("123456"); User u2=new User(); u2.setUserName("李四"); u2.setPassword("ls123"); User u3=new User(); u3.setUserName("王五"); u3.setPassword("ww888"); users.add(u1); users.add(u2); users.add(u3); return users; } }
jsp页面(方式一):
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> <script type="text/javascript" src="js/jquery-1.7.2.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $.ajax({ type:'post', url:'${pageContext.request.contextPath}/getUserList.do', success:function(data){ //返回json数据 alert("data:"+data); $.each(data,function(i,user){ $("#result").append(user.userName+"----"+user.password+"<br>"); }) }, error:function(e){ alert("error:"+e); } }); }); }); </script> </head> <body> <input type="button" id="btn" value="获取用户信息"/><br/> 用户信息:<br/> <div id="result"></div> </body> </html>
jsp页面(方式二
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> <script type="text/javascript" src="js/jquery-1.7.2.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $.ajax({ type:'post', url:'${pageContext.request.contextPath}/getUserList.do', success:function(data){ //返回json数据 alert("data:"+data); $.each(data.users,function(i,user){ $("#result").append(user.userName+"----"+user.password+"<br>"); }) }, error:function(e){ alert("error:"+e); } }); }); }); </script> </head> <body> <input type="button" id="btn" value="获取用户信息"/><br/> 用户信息:<br/> <div id="result"></div> </body> </html>
):
项目截图:
相关文章推荐
- SpringMVC之Controller传递JSON数据到页面
- 解决springmvc 从后台获取json数据传入前台jsp中文显示???的方法
- SpringMVC作为Controller向前端传递JSON数据,前端用Extjs接收
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- SpringMVC中controller接收json数据的方法
- SpringMVC之Controller传递JSON数据到页面
- 使用原生JavaScriptAjax以及jQuery的Ajax结合SpringMVC发送和获取json数据
- SpringMVC作为Controller向前端传递JSON数据,前端用Extjs接收
- 使用fastjson与SpringMVC实现自定义HttpMessageConverter接收和获取JSON格式的数据
- springMVC架构后台获取不到json数据的问题
- SpringMVC之Controller传递JSON数据到页面
- SpringMVC常用配置-Controller返回格式化数据如JSON、XML的配置方式和机制
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- SpringMVC中controller接收Json数据
- SpringMVC从Controller中响应json数据
- springmvc之json数据交互controller方法返回值为简单类型
- js获取springmvc的controller返回的数据,可以是对象,也可以处理成字符串返回给js
- springmvc之json数据交互controller方法返回值为简单类型
- springmvc的controller层如何获取到请求的数据
- SpringMVC中controller返回json数据的两种方法