接下来讲解一下 @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. centos7配置mysql8.0主从复制

    注意:1.主库:10.1.131.75,从库:10.1.131.762.server-id必须是纯数字,并且主从两个server-id在局域网内要唯一. [主节点]vi /etc/my.cnf[mys ...

  2. vue打包时,assets目录 和static目录下文件的处理区别(nodeModule中插件源码修改后,打包后的文件应放在static目录)

    为了回答这个问题,我们首先需要了解Webpack如何处理静态资产.在 *.vue 组件中,所有模板和CSS都会被 vue-html-loader 及 css-loader 解析,并查找资源URL.例如 ...

  3. kbd_mode - 显示或者设置键盘模式

    总览 (SYNOPSIS) kbd_mode [ -auks ] 描述 (DESCRIPTION) 如果 没有 参数 kbd_mode 会 显示 当前 键盘 的 模式, 如果 有 参数, 它会把 键盘 ...

  4. Ubuntu16.04下安装httpd+svn+viewVC

    一.安装httpd 1.下载httpd 网址:http://httpd.apache.org/download.cgi#apache24 下载这一条---Source: httpd-2.4.39.ta ...

  5. Ubuntu伪破解Navicat12方法

    一.去官网下载navicat112_premium_cs_x64 for linux版本二.用tar解压安装包三.navicat解压即可用,直接进入解压后的目录,然后用‘./’运行start_navi ...

  6. (转) Java中的负数及基本类型的转型详解

    (转) https://my.oschina.net/joymufeng/blog/139952 面这行代码的输出是什么? 下面两行代码的输出相同吗? 请尝试在Eclipse中运行上面的两个代码片段, ...

  7. [POJ1664]放苹果(动态规划)

    [POJ1664]放苹果 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第 ...

  8. etl-p

    java excel 导入数据库 上传文件包  解压导入excel包 导入mysql

  9. C#基础知识之图解TCP IP》读书笔记

    一.网络基础知识 1. 计算机使用模式的演变 2.协议 协议就是计算机与计算机之间通过网络实现通信事先达成的一种“约定”.这种“约定”使那些由不同厂商的设备.不同的CPU以及不同的操作系统组成的计算机 ...

  10. 如何通过Samba共享Linux文件夹

    https://blog.csdn.net/stu059074244/article/details/77766155   Samba(SMB是其缩写) 是一个网络服务器,用于Linux和Window ...