Springmvc中ajax与jason应用


Springmvc中ajax与jason应用

相关依赖包

json数据转换的jar包

jackson-annotations-2.5.4
jackson-core-2.5.4
jackson-databind-2.5.4

spring以及spring的依赖包

bean context aop context core web webmvc expression

未涉及json的ajax请求

简单的表单验证

Controller层java代码

@Controller
public class AjaxCtrl {
	@RequestMapping("/toAjax.do")
	public String toAjax() {
		return "ajax";
	}
	@RequestMapping("/ajax.do")
	@ResponseBody
	public void ajaxJson(@RequestParam("name")String name,HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		if(name.equals("hm"))
			out.print("该用户已注册");
		else
			out.print("该用户可使用");
	}

注意点:

  • 在手动调用getWriter()方法时不可以再添加返回值,否则tomcat会报错(getWriter()方法已存在)。因为在返回值处容器会自动调用getWriter()方法来输出内容,而检测到前面getWriter()已经被调用过,产生冲突。
  • 解决:1. 像上面的代码不给出返回值;2. resp.reset(); resp.setContentType("text/html; charset=utf-8"); out.flush; out.close();
  • @ResponseBody会将返回的数据自动转换为json格式的数据

相关jsp代码:ajax.jsp




	姓名:
	

web.xml

<?xml version="1.0" encoding="UTF-8"?>

	springmvc_ajax
	
		characterencoding
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		characterencoding
		*.do
	
	
		ajax
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:mvc.xml
		
		1
	
	
		ajax
		*.do
	

涉及json格式数据的ajax请求

Controller层java代码

@RequestMapping("/toAjaxJson.do")
	public String toAjaxJson() {
		return "ajaxJson";
	}
	@RequestMapping("/ajaxJson.do")
	@ResponseBody
	public List ajaxJson() {
		List list = new ArrayList();
		list.add(new User(1, "xb", "123"));
		list.add(new User(2, "xc", "234"));
		list.add(new User(3, "xd", "345"));
		list.add(new User(4, "xa", "456"));
		return list;
	}

相关jsp代码:ajaxJson.jsp


	
编号 姓名 密码

springmvc的核心配置文件:mvc.xml

<?xml version="1.0" encoding="UTF-8"?>

	
	
	
	
	
	
		
		
	
	
		
			
				text/plain;charset=UTF-8
			
		
	
	
	
		
			
				
				
			
		
		

相关类的介绍

StringHttpMessageConverter

官方文档:
An HttpMessageConverter implementation that can read and write Strings from the HTTP request and response. By default, this converter supports all text media types (text/*), and writes with a Content-Type of text/plain.

译文:
一种HttpMessageConverter可以从HTTP请求和响应中读取和写入字符串的实现。默认情况下,此转换器支持所有文字媒体类型(text/*),并用Content-Type中的text/plain类型。

MappingJackson2HttpMessageConverter

官方文档:
An HttpMessageConverter implementation that can read and write JSON using Jackson’s ObjectMapper. JSON mapping can be customized as needed through the use of Jackson’s provided annotations. When further control is needed, a custom ObjectMapper can be injected through the ObjectMapper property for cases where custom JSON serializers/deserializers need to be provided for specific types. By default this converter supports ( application/json).

译文:
一个HttpMessageConverter可以使用Jackson库提供的ObjectMapper类来读取和写入json格式的数据。可以根据需要通过使用Jackson库提供的注解来自定义XML映射。当需要进一步控制时,XmlMapper 可以通过ObjectMapper属性注入自定义,以用于需要为特定类型提供自定义XML序列化器/反序列化器的情况。默认情况下,此转换器支持(application/xml)。

ObjectMapper是Jackson库的主要类,他提供的一些功能用于将Java对象转换为符合jason结构的字符串。

AnnotationMethodHandlerAdapter

注入了AnnotationMethodHandlerAdapter作用是对有RequestMapping注解的控制器进行HTTP路径、HTTP方法和请求参数解析.

项目结构

ajaxJson

项目源码已上传至 github ajaxJson