@RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法

接下来讲解一下 @RequestMapping  和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法

@RequestMapping 为url映射路径

@ResponseBody  将数据以json数据返回ajax的回掉方法

@RequestBody  将url的参数以实体形式传入action

@PathVariable 注解  分解resultful的风格

jsp源码

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-1.5.js"></script>
<script type="text/javascript">

function findStudentInfo() {
debugger
$.ajax({
type:"get",
url:"${pageContext.request.contextPath}/getemps",
dataType:"json",
success : function (data) {
debugger
$("#showMessageDiv").empty();
$("#showMessageDiv").append("<table id='table1'></table>");
$("#table1").append("<tr><td>员工ID</td><td>姓名</td><td>性别</td><td>邮箱地址</td></tr>");
$.each(data,function (i,result) {
var sex="女"
if (result.gender==1){sex="男"}
var item="<tr><td>"+result.id+"</td><td>"+result.lastName+"</td><td>"+sex+"</td><td>"+result.email+"</td>";
$("#table1").append(item);
});

},
error:function(){
alert("错误");
}
});
}

function findStudentInfo1() {
debugger
var datas ='{"id":" 1","email":"123@11.com "}';
$.ajax({
type : 'POST',
contentType : 'application/json',
url : "${pageContext.request.contextPath}/getemps1",
dataType : 'json',
data : datas,
success : function(data){
$("#showMessageDiv").empty();
$("#showMessageDiv").append("<table id='table1'></table>");
$("#table1").append("<tr><td>员工ID</td><td>姓名</td><td>性别</td><td>邮箱地址</td></tr>");
var sex="女"
if (data.gender==1){sex="男"}
var item="<tr><td>"+data.id+"</td><td>"+data.lastName+"</td><td>"+sex+"</td><td>"+data.email+"</td>";
$("#table1").append(item);
},
error : function()
{
alert('Sorry, it is wrong!');
}
});
}
function findStudentInfo2() {
debugger
var datas ='{"id":" 1","email":"jerry1@atguigu.com"}';
$.ajax({
contentType : 'application/json',
type : 'POST',
url:"${pageContext.request.contextPath}/getemps2",
dataType:"json",
data : datas,
success : function (data) {
debugger
$("#showMessageDiv").empty();
$("#showMessageDiv").append("<table id='table1'></table>");
$("#table1").append("<tr><td>员工ID</td><td>姓名</td><td>性别</td><td>邮箱地址</td></tr>");
$.each(data,function (i,result) {
var sex="女"
if (result.gender==1){sex="男"}
var item="<tr><td>"+result.id+"</td><td>"+result.lastName+"</td><td>"+sex+"</td><td>"+result.email+"</td>";
$("#table1").append(item);
});

},
error:function(){
alert("错误");
}
});
}
function findStudentInfo3() {
debugger
$.ajax({
type : 'POST',
contentType : 'application/json',
url : "${pageContext.request.contextPath}/getemps3?id=1",
dataType : 'json',
success : function(data){
$("#showMessageDiv").empty();
$("#showMessageDiv").append("<table id='table1'></table>");
$("#table1").append("<tr><td>员工ID</td><td>姓名</td><td>性别</td><td>邮箱地址</td></tr>");
var sex="女"
if (data.gender==1){sex="男"}
var item="<tr><td>"+data.id+"</td><td>"+data.lastName+"</td><td>"+sex+"</td><td>"+data.email+"</td>";
$("#table1").append(item);
},
error : function()
{
alert('Sorry, it is wrong!');
}
});
}
function findStudentInfo4() {
debugger
$.ajax({
type : 'POST',
contentType : 'application/json',
url : "${pageContext.request.contextPath}/testPathVariable/4",
dataType : 'json',
success : function(data){
$("#showMessageDiv").empty();
$("#showMessageDiv").append("<table id='table1'></table>");
$("#table1").append("<tr><td>员工ID</td><td>姓名</td><td>性别</td><td>邮箱地址</td></tr>");
var sex="女"
if (data.gender==1){sex="男"}
var item="<tr><td>"+data.id+"</td><td>"+data.lastName+"</td><td>"+sex+"</td><td>"+data.email+"</td>";
$("#table1").append(item);
},
error : function()
{
alert('Sorry, it is wrong!');
}
});
}
</script>
<body>

<div id="showMessageDiv">
</div>
<div id="data">
<input type="button" value="返回list" onclick="findStudentInfo()"/>
<input type="button" value="返回实体以RequestBody接受参数" onclick="findStudentInfo1()"/>
<input type="button" value="返回list以RequestBody接受参数" onclick="findStudentInfo2()"/>
<input type="button" value="返回实体以Requestparam接受参数" onclick="findStudentInfo3()"/>
<input type="button" value="返回实体以testPathVariable接受参数" onclick="findStudentInfo4()"/>
</div>
</body>
</html>

2 action源码

package com.atguigu.mybatis.controller;

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.service.EmployeeService;

@Controller
public class EmployeeController {

@Autowired
EmployeeService employeeService;

@RequestMapping("/getemps")
@ResponseBody
public String empsList() throws JsonGenerationException, JsonMappingException, IOException{
List<Employee> emps = employeeService.getEmps();
ObjectMapper mapper= new ObjectMapper();
String jsonStr = mapper.writeValueAsString(emps );
return jsonStr;
}
@RequestMapping("/getemps1")
@ResponseBody
public Employee emps(@RequestBody Employee e) throws JsonGenerationException, JsonMappingException, IOException{
Employee e1=employeeService.getEmpById(e.getId());
return e1;
}

@RequestMapping("/getemps2")
@ResponseBody
public String empsListbyemail(@RequestBody Employee e) throws JsonGenerationException, JsonMappingException, IOException{
List<Employee> emps = employeeService.getEmpByemail(e.getEmail());
ObjectMapper mapper= new ObjectMapper();
String jsonStr = mapper.writeValueAsString(emps );
return jsonStr;
}

@RequestMapping("/getemps3")
@ResponseBody
public Employee emps(@RequestParam(value = "id") Integer id) throws JsonGenerationException, JsonMappingException, IOException{
Employee e1=employeeService.getEmpById(id);
return e1;
}
@RequestMapping("/testPathVariable/{id}")
@ResponseBody
public Employee testPathVariableemps(@PathVariable("id") Integer id) throws JsonGenerationException, JsonMappingException, IOException{
Employee e1=employeeService.getEmpById(id);
return e1;
}
}

原文地址:https://www.cnblogs.com/zhangzhiqin/p/8491428.html