@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;
}
}
@RequestMapping 和@ResponseBody 和 @RequestBody和@PathVariable 注解 注解用法的更多相关文章
- @RequestMapping、@Responsebody、@RequestBody和@PathVariable详解(转)
一.预备知识:@RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. @Requ ...
- 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable
转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable (转)
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...
- @ResponseBody,@RequestBody,@PathVariable
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...
- @RequestMapping、@ResponseBody 和 @RequestBody 注解的用法与区别
背景: 帮助同事解决文件上传的bug(文件上传成功,但是页面提示上传接口异常,数据的确是插入了),从前端layui页面找错误,然后浏览器调试,找了半天无果.layui文件上传格式code返回是数值,后 ...
- RequestMapping、Responsebody、RequestBody
预备知识:@RequestMappingRequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.@RequestM ...
- @RequestMapping、@ResponseBody和@RequestBody的使用
使用SSM框架进行Web开发时,经常在Controller中遇到@RequestMapping.@ResponseBody和@RequestMapping注解. 1.@RequsetMapping注解 ...
- SpringMVC注解@RequestMapping @RequestParam @ResponseBody 和 @RequestBody 解析
SpringMVC Controller层获取参数及返回数据的方式: @RequestMapping @RequestMapping(“url”),这里的 url写的是请求路径的一部分,一般作用在 C ...
随机推荐
- 12.AutoMapper 之Null替换(NullSubstitution)
https://www.jianshu.com/p/3f86f237d1db Null 替换(Null Substitution) Null 替换允许当源类型成员在成员链任何位置为Null时给目标成员 ...
- RabbitMQ从安装到使用
一.在Linux中安装RabbitMQ 通过Docker安装: 获取镜像(选用management是带有管理界面的) docker pull rabbitmq:-management 查看下载好的镜像 ...
- 缓存机制总结(JVM内置缓存机制,MyBatis和Hibernate缓存机制,Redis缓存)
一.JVM内置缓存(值存放在JVM缓存中) 我们可以先了解一下Cookie,Session,和Cache Cookie:当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cooki ...
- 从分析攻击方式来谈如何防御DDoS攻击
DDoS攻击的定义: DDoS攻击全称——分布式拒绝服务攻击,是网络攻击中非常常见的攻击方式.在进行攻击的时候,这种方式可以对不同地点的大量计算机进行攻击,进行攻击的时候主要是对攻击的目标发送超过其处 ...
- Delphi 对象观察器
- zabbix 数据库问题
Too many connections ::052844.247 Cannot connect to the database. Exiting... :: started [trapper #] ...
- Linux日常之数据重定向
Linux在启动后,会默认打开3个文件操作符,分别是 (1)标准输入0,standard input (2)正确输出1,standard output (3)错误输出2,standard error ...
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- yang学习
Ø Yang 是对NETCONF数据模型.操作进行建模,覆盖NETCONF协议的操作层和内容层 Ø 模块和子模块 模块是yang中定义的基本单位. 模块定义单个数据模型,它可以定义完整的聚合模型, ...
- Gym-100923A-Por Costel and Azerah(DP)
链接: https://vjudge.net/problem/Gym-100923A 题意: Por Costel the Pig has received a royal invitation to ...