接下来讲解一下 @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;
}
}

@RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法的更多相关文章

  1. @RequestMapping、@Responsebody、@RequestBody和@PathVariable详解(转)

    一.预备知识:@RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. @Requ ...

  2. 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable

    转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...

  3. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable (转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  4. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  5. @ResponseBody,@RequestBody,@PathVariable

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  6. @RequestMapping、@ResponseBody 和 @RequestBody 注解的用法与区别

    背景: 帮助同事解决文件上传的bug(文件上传成功,但是页面提示上传接口异常,数据的确是插入了),从前端layui页面找错误,然后浏览器调试,找了半天无果.layui文件上传格式code返回是数值,后 ...

  7. RequestMapping、Responsebody、RequestBody

    预备知识:@RequestMappingRequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.@RequestM ...

  8. @RequestMapping、@ResponseBody和@RequestBody的使用

    使用SSM框架进行Web开发时,经常在Controller中遇到@RequestMapping.@ResponseBody和@RequestMapping注解. 1.@RequsetMapping注解 ...

  9. SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析

    SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...

随机推荐

  1. 85. Maximal Rectangle (JAVA)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  2. Json解析报错: Error is : Unescaped control character...的解决方法

    在利用json-framework来实现json解析的过程时,会出现"-JSONValue Failed. Error is : Unescaped control character&qu ...

  3. Codeforces Round #575 (Div. 3) (A. Three Piles of Candies)(数学)

    A. Three Piles of Candies time limit per test1 second memory limit per test256 megabytes inputstanda ...

  4. vs开发之工程属性

    1.建立工程 最好使用自己创建工程 然后把代码移过去 2.不要使用别人建立的工程直接导入(差异比较大的话),后面32位 64位 配置属性 可能造成冲突 3.冲突了 还需要重新做 上面 1

  5. 常用命令之------ln

    当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在 其它的目录下用ln命令链接(link)它就可以,不必重复的 ...

  6. java课堂作业,求多参数的和

    设计思想 第一步:用户输入他想要计算的数字个数 第二步:声明字符串数组,保存用户的输入,同时每次输入后强制转换类型并求和 第三步:将声明的数组赋值给args 第四步:输出结果 程序流程图 程序源代码 ...

  7. thinkphp5杂谈--模板

    一种新型开源模板   http://www.h-ui.net/H-ui.admin.shtml 下载页面代码 除了curl以外还可以借助  仿站小工具V7.0,操作示意图

  8. eclipse修改代码后都需要clean的解决办法

    问题描述: 用STS(类似于Eclipse)正在开发一个JavaWeb项目,但不知怎么的有一天,修改完Java代码,点击运行Tomcat,发现根本没有修改.刚刚开始的时候,因为一开始没找到原因而且工期 ...

  9. IDEA 2018.1可用License服务(持续更新)

    1. http://idea.congm.in 2.http://idea.toocruel.net

  10. js 数组 splice 函数 多线程

    <script type="text/javascript"> var arr = new Array(6) arr[0] = "00" arr[1 ...