springMVC JSON的使用

1.springMVC中要的.jar文件

jackson-annotations-2.4.0.jar

jackson-core-2.4.2.jar

jackson-databind-2.4.2.jar

2.html文件中配置json

2.1 contentType:"application/json;charset=utf-8", 必须是json

2.2 data:'{"name":"测试商品","year":"22","sex":"男"}', 中属性必须是bean中有的

<%@ 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 'json.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
	<script type="text/javascript">
		
		function buttonClick(){
			
			//请求json响应json   
			// contentType:"application/json;charset=utf-8",    必须是json
			$.ajax({
				type:"post",
				url:"${pageContext.request.contextPath }/json/updateJson.action",
				contentType:"application/json;charset=utf-8",
				data:'{"name":"测试商品","year":"22","sex":"男"}',
				success:function(data){
					alert(data);
				}
			});
		}
	</script>

  </head>
  
  <body>
    	<button id = 'button' οnclick="buttonClick()">aa</button>
  </body>
</html>


3. controller中接受数据

@Controller
//窄化请求映射:为防止你和你的队友在conroller方法起名的时候重名,所以相当于在url中多加了一层目录,防止重名
//例如:当前list的访问路径   localhost:8081/ssm0523-1/items/list.action 
@RequestMapping("/json")
public class UpdateJsonController {

	/**
	 * springMVCjson上传
	 * @ResponseBody  springMVC 自己帮助我们json格式发送
	 * @RequestBody   SC 帮助我们请求json
	 */
	@RequestMapping("/updateJson")
	@ResponseBody
	public ClassBean updateJson(@RequestBody ClassBean classBean){
		
		System.out.println(classBean.getName());
		return classBean;
	}
	
	@RequestMapping("/json")
	public String json(){
		
		return "json";
	}
	
}

4.bean中数据

package pojo;

public class ClassBean {

	public String name;
	public String year;
	public String sex;
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getYear() {
		return year;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getSex() {
		return sex;
	}
	
}

5.在springMVC中配置

5.1  配置了注解驱动就可以直接用了

  <!-- 注解驱动:
    		替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>


6 普通的springMVC.xml文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
 <context:component-scan base-package="controller"></context:component-scan>
 
	 <!--  如果没有配置处理器映射器和处理器适配器,那么springMVC每次请求就会在默认dispatchServlet.properlist文件中扫描,这样会影响性能 -->
	 <!--  注解形式的处理器映射器 -->
	 <!-- 
	 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	  -->
	 <!-- 注解形式的处理器适配器 
	 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
	 -->
	 
	 <!--  上面的配置,在springMVC3.2 以后就废弃了 -->
	 <!-- 配置最新的处理器映射器 
	 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
	 <!-- 配置最新的处理器适配器
	 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>  -->
	 
	 <!--  注解驱动,因为公司里面不可能,因为spring升级,而改配置文件,所以用注解驱动 
	 作用:替我们自动配置最新的注解的处理器映射器和注解的处理器适配器
	 -->
	 <mvc:annotation-driven></mvc:annotation-driven>
	 
	 <!-- 配置视图解析器  /WEB-INF/jsp/itemList.jsp  以后不要写完整的路径,可以写直接的名字 -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<!-- 前缀 -->
	 	<property name="prefix" value=" /WEB-INF/jsp/"></property>
	 	<!--  后缀 -->
	 	<property name="suffix" value=".jsp"></property>
	 </bean>
	 
 </beans>       

7.配置注解驱动后的springMVC,xml文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- @Controller注解扫描 -->
    <context:component-scan base-package="controller"></context:component-scan>
    
    <!-- 注解驱动:
    		替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    
   <!-- 配置自定义转换器 
	注意: 一定要将自定义的转换器配置到注解驱动上
	-->
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<set>
				<!-- 指定自定义转换器的全路径名称  这样数据转化就可以了 而且可以配置多个 -->
				<bean class="tools.ConversionsServiceStringToDate"/>
			</set>
		</property>
	</bean>
	
    <!-- 配置视图解析器 
	作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
	-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 真正的页面路径 =  前缀 + 去掉后缀名的页面名称 + 后缀 -->
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<!-- 后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设置上传文件的最大尺寸为5MB -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean>
	
</beans>


























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值