这篇文章在前边项目的基础上来整合json,最新项目资料见:http://download.csdn.net/detail/u012116457/8436187
首先需要的jar包:jackson-core-asl-1.7.2.jar jackson-mapper-asl-1.7.2.jar
然后是配置文件json-servlet.xml,该文件得在web.xml中配置使其在tomcat启动时调用:
<?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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 配置对Json的支持 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <!-- 为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 --> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </beans>
控制类如下:
package module.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import module.entity.User; import module.service.LoginService; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/login") public class LoginController { private static String LOGINSUCCESS="module/jsp/index"; //@Autowired() @Qualifier("loginServiceImpl2") @Resource(name="loginServiceImpl2") private LoginService loginService; @RequestMapping(value = "/login.do", method = RequestMethod.POST) @ResponseBody //此注解表明返回值跳过视图处理部分,直接写入 http response body中 public User login(User user) { System.out.println("用户:"+user.getName()+" 请求登陆"); User u = loginService.findByName(user.getName()); if(u==null){ u=new User(); u.setName(""); } return u; } @RequestMapping("/loginSuccess.do") public String start(HttpServletRequest request){ return LOGINSUCCESS; } }
在spring中,如果使用了@RequestMapping注解,会根据该方法的返回值映射到实际的url,如果同时还使用了@ResponseBody注解,则会跳过视图处理部分,将返回值直接写入http response body中,将其返回到客户端页面。如上边的代码,会将User格式的数据自动转换为json格式返回到客户端。
简单来说,如果需要返回json数据,则用@ResponseBody来注解该方法,返回类型为实体类即可。
下面是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 > <html> <head> <title>登陆</title> <script type="text/javascript" src="<%=path %>/js/common/jquery.min.js"></script> <script type="text/javascript" charset="utf-8"> function login(){ $.ajax({ url:'login/login.do', data:{ name:document.getElementById("name").value, password:document.getElementById("password").value }, type:'POST', dataType:'json', success:function(data){ alert(data.name); if(data.name!="") window.location.href ='http://localhost:8080/springMVC/login/loginSuccess.do'; else alert("用户名或密码错误"); }, error:function(){ alert("error"); } }); } </script> </head> <body> <center> <form> 姓名:<input type="text" name="name" id="name"><br/> 密码:<input type="password" name="password" id="password"></br> <button type="button" onclick="login()">登陆</button> </form> </center> </body> </html>
时间: 2024-10-12 20:20:44