springmvc restful风格支持

    公司的一个内部系统URL是restful风格的,远比我们系统的众多***.action请求格式看起来舒服。于是花了一两天研究下Spring mvc 3.0对restful风格的支持。

    Spring 对REST的支持是构建于Spring MVC上的。

    REST不同于RPC,RPC(DUBBO,HSF,JSF,HESSIAN)是面向服务的,重点在动作和行为。而REST是面向资源的,它将资源的状态在客户端和服务端间进行转移。URL在REST中起了关键作用。同一个URL,通过配置不同的请求方法,可以对应后台的对资源的CRUD操作(详见下面的对应关系)。RESTful的URL是有层级的,从左向右读是一个从抽象到具体,从高级到低级的构成。

    path/ POST    --> 新增 (CREATE)

    path/id GET --> 查询 (READ)

    path/id PUT  --> 更新 (UPDATE)

    path/id DELETE --> 删除 (DELETE)

    GET是返回资源,将资源的状态从服务器转移到客户端。而PUT则恰恰相反,将资源的状态从客户端转移到服务端。POST和DELETE也正好相反,POST负责创建资源,而DELETE则负责删除资源。

    RESTful的URL是参数化的URL,可以使用URL的部分值作为请求参数。@PathVariable注解则让这个功能成为现实。它的作用在于将URL中的占位符的值传到所注解的变量上。

    下面我给出我实现的一个DEMO。

后台代码实现

@Log4j
@RequestMapping(value = "/word")
@Controller
public class Demo extends BaseController {

	@Autowired WriteDao	writeDao;

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public String update(Model model, @PathVariable Integer id, @RequestParam Integer lib_id, @RequestParam String word, RiskKeyWord key) {
		writeDao.update("word.update", key);
		return "word";
	}

	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public Integer add(Model model, RiskKeyWord key) {
		Integer word = null;
		try {
			word = (Integer) writeDao.insert("word.add", key);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return word;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public RiskKeyWord query(Model model, @PathVariable int id) {
		RiskKeyWord word = null;
		try {
			word = writeDao.getObject("word.query", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return word;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public int del(Model model, @PathVariable int id) {
		int word = 0;
		try {
			word = writeDao.delete("word.del", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return word;
	}

	// 页面展示
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Model model) {
		return "word";
	}
}
    各个方法的URL路径配置是一样的,区别在于method参数的配置,也就是不同的请求方法。
 

前端代码实现

    通过ajax的方式向后台发送请求,$.ajax可以灵活的配置http请求的请求方法。
function update() {
	var url = "/word/" + $("#id").val();
	$.ajax({
		url : url,

		data : $("#f").serialize(),

		type : "PUT",

		success : function(data, textStatus, jqXHR) {
			if (data == "true") {
				IM.alert("添加成功", "添加成功");
				query();
				IM.close();
			} else {
				IM.alert("添加失败", "添加失败");
			}
		},

		error : function(XMLHttpRequest, textStatus, errorThrown) {
			IM.alert("添加失败", "添加失败");
		}
	});
	
}

    这里仅给出更新操作,CRUD四个操作的js代码完全一样,除了type参数,删除对应delete,新增对应post。
 

web.xml配置

	<filter>
		<filter-name>httpPutFormFilter</filter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>httpPutFormFilter</filter-name>
		<servlet-name>appServlet</servlet-name>
	</filter-mapping>

	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
    之所以要配置HttpPutFormContentFilter,是因为spring mvc对put支持的并不好,put请求时不会解析请求体中的参数。但是配置了这个过滤器后,这个问题可以完美的解决。详细的请参见我上一篇博文 点击打开链接
 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bruce128

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值