您当前的位置: 首页 >  spring

Charge8

暂无认证

  • 1浏览

    0关注

    447博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC RESTfull风格请求传参

Charge8 发布时间:2018-09-23 23:24:01 ,浏览量:1

一、 在HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。

它们分别对应四种基本操作:    

1、GET ======获取资源
2、POST ===== 新建资源
3、PUT======= 更新资源
4、DELETE==== 删除资源
二、REST:即 Representational State Transfer。(资源)表现层状态转化。

是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用。

我们可以通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,实现结果:

/某路径/1 HTTP GET : 得到 id = 1 的 一条数据
/某路径/1 HTTP DELETE: 删除 id = 1的 一条数据
/某路径/1   HTTP PUT: 更新id = 1的 一条数据
/某路径 HTTP POST: 新增一条数据

 

实现方式(REST风格四种请求方式的调用):

我们通过 @RequestMapping映射请求中的 method属性实现四种请求方式的调用:

@Controller
@RequestMapping("/login")
public class LoginAction {

	//http://localhost:8080/springmvcdemo/login/
	@RequestMapping(value="/")
	public String login() {
		return "login";
	}
	
	@RequestMapping(value="/restGet", method=RequestMethod.GET)
	public String restGet() {
		System.out.println("restGet");
		return "success";
	}
	@RequestMapping(value="/restPost", method=RequestMethod.POST)
	public String restPost() {
		System.out.println("restPost");
		return "success";
	}
	@RequestMapping(value="/restPut", method=RequestMethod.PUT)
	public String restPut() {
		System.out.println("restPut");
		return "redirect:/login/";
	}
	@RequestMapping(value="/restDelete", method=RequestMethod.DELETE)
	public String restDelete() {
		System.out.println("restDelete");
		return "redirect:/login/";
	}
	
}

----
restPut
restDelete
restGet
restPost
三、将POST请求转化为put请求和delele请求

1.在 web.xml 文件中配置 HiddenHttpMethodFilter 过滤器:

	
	
		hiddenHttpMethodFilter
		org.springframework.web.filter.HiddenHttpMethodFilter
	
	
		hiddenHttpMethodFilter
		/*
	

2.在表单域中需写一个隐藏表单: 来访问 REST的put或者delete请求,正常get/post请求可写可不写。name 值为 _method ,value 值为put或者delete

        登录页面
	 --%>
	 --%>
	 --%>
	
		
			
		 
		 
		
	

使用springmvc 的from 标签: 

jsp页面可省略那个隐藏表单, 但是查看页面源码


  

   




修改用户



	修改用户信息
	
		
		
		用户编号: 
		用户名:  
		用户密码:  
		email:  
		用户所属部门:  
		 
	
	

   

 

ends~

关注
打赏
1664721914
查看更多评论
立即登录/注册

微信扫码登录

0.0390s