springMvc Rest风格Url

       REST是设计风格而不是标准目的只是让url看起来更简洁实用,是资源状态的一种表达,资源是由URI来指定,对资源的操作包括获取、创建、修改和删除资源这些操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法。通过操作资源的表现形式来操作资源。

常用操作:

GET  获取

POST  提交

PUT  更新

Delete  删除


表单只支持get和post提交  要支持put和delete需要在web.xml中配置以下内容

   <!-- 请求method支持put和delete必须添加 过滤器 -->
  <filter>
	<filter-name>myFilter</filter-name>
	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
	<filter-name>myFilter</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>




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

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestController {
	@Autowired
	JdbcTemplate jdbc;   //localhost:8080/s/user/2   @PathVariable(value="id")和value("/user/{id}")的参数相但与参数列表中的参数可以不同

	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	public String index(@PathVariable(value="id") String uid,HttpServletResponse response) throws IOException{
		String sql="select * from str where id="+uid;
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		return "/index.jsp";
	}
	//根据表单的动作进入不同的方法
	@RequestMapping(value="/user",method=RequestMethod.POST)
	public String addUser(String name,HttpServletResponse response) throws IOException{
		response.getWriter().println(name);
		return null;
	}
	
	@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
	public String update(@PathVariable String id,HttpServletResponse response) throws IOException{
		response.getWriter().println(id+"----update success");
		return null;
	}
	
	@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
	public String delete(@PathVariable String id,HttpServletResponse response) throws IOException{
		response.getWriter().println(id+"----delete success");
		return null;
	}
}

表单

<form action="${pageContext.request.contextPath}/user/12" method="post">
    												<!-- put   delete -->
    	<input type='hidden' name="_method" value="delete">
    	<input type="text" name="name"/>
    	<input type="submit" value="提交"/>
    </form>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值