SpringMVC入门(一)--e.注解@ResponseBody、@RequestBody使用

@ResponseBody、@RequestBody两个注解主要满足开发人员使用SpringMVC框架,

在前后台传输和接收json格式的数据的使用

1.创建Web工程准备好环境(导入jar包 配置Springmcv.xml文件)

2.编写login.html页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>


<script type="text/javascript">
function chuanJson(){
	// json对象
	var jsonObj = {username:"张三",age:"12",phone:"123456789",address:"天津市"};
	var user = JSON.stringify({username:"张三",age:"12",phone:"123456789",address:"天津市"});
	user.toJSONString();
	alert(user);
	$.ajax({
		type:'post',
		url:'${pageContext.request.contextPath}/rest/user/aa/Requestparam.do',
		contentType: "application/json", 
		data:user,
		dataType:'json',
		success:function(mm){
			alert(mm);
		}
	});
}
</script>
<body>
<!-- 传递json串  -->
	<input type="button" onclick="chuanJson();" value="提交">
<hr >
</body>
</html>

注意:使用ajax请求需要引入jquery的js包

3.编写springmvc配置文件

3.1 手动添加json数据支持的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
		
		<!-- 配置扫描 -->
		<context:component-scan base-package="com.zit.controller"></context:component-scan>
		
		<!-- <mvc:annotation-driven/> -->
		
		
		<!-- 配置注解处理器映射器 
			功能:寻找执行类Controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
		
		<!-- 配置注解处理器适配器 
			功能:调用controller方法,执行controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		
			<!-- 此处添加json数据格式的数据支持, -->
			<property name="messageConverters">
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
				</bean>
			</property>
		</bean>
		
		
		<!-- 配置sprigmvc视图解析器:解析逻辑试图 
			 后台返回逻辑试图:index
			视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
		-->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>		
		</bean>
		</beans>

3.2 使用<mvc:annotation-driven/>代替 对处理器映射器处理器适配器 的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
		
		<!-- 配置扫描 -->
		<context:component-scan base-package="com.zit.controller"></context:component-scan>
		
		<mvc:annotation-driven/>
		
		
		<!-- 配置注解处理器映射器 
			功能:寻找执行类Controller
		-->
		<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
		
		<!-- 配置注解处理器适配器 
			功能:调用controller方法,执行controller
		-->
		<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		
			此处添加json数据格式的数据支持,
			<property name="messageConverters">
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
				</bean>
			</property>
		</bean> -->
		
		
		<!-- 配置sprigmvc视图解析器:解析逻辑试图 
			 后台返回逻辑试图:index
			视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
		-->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>		
		</bean>
		</beans>

4. 编写执行类

package com.zit.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zit.entity.User;
import com.zit.entity.WrapObject;

@Controller
@RequestMapping("/user")
public class UserController {
/**
	 * 接受json格式数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/aa/Requestparam")
	
	public @ResponseBody User jsonRequest(@RequestBody User user){
		System.out.println("121212");
		System.out.println(user.getAddress());
		//重定向
		return user;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37306041/article/details/80096604