spring3 MVC下结合jquery ajax简单使用--超级简单

今天需要写一个简单的应用:用户注册时检查用户名是否可以使用。

 

之前在struts2下貌似对ajax的支持是不错的(其实有点烦),而且网上有很多教程。不过这次是换成springmvc了,而且是spring3,网上东西不多。

 

不过还是在springsource上发现了一篇文章

 

在客户端使用了formValidtor插件,这个插件真的不错,呵呵。

 

$("#loginName").formValidator({onshow:"请输入用户名",onfocus:"用户名至少6个字符,最多15个字符",oncorrect:"该用户名可以注册"}).inputValidator({min:6,max:15,onerror:"你输入的用户名非法,请确认"}).regexValidator({regexp:"username",datatype:"enum",onerror:"用户名格式不正确"}).ajaxValidator({
	    type : "get",
		url : "${ctx}/ajax/checkLoginName",
		datatype : "json",
		success : function(data){	
            if( data == "1" )
			{
                return true;
			}
            else
			{
                return false;
			}
		},
		buttons: $("#button"),
		error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
		onerror : "该用户名不可用,请更换用户名",
		onwait : "正在对用户名进行合法性校验,请稍候..."
	});

 这个是formValidator自带的ajax方式验证,很明了。

 

在老外的那片文章上发现可以有更简单的调用ajax方式:

 

 

jQuery.getJSON( url, [ data ], [ callback(data, textStatus) ] )

 

从jquey网站上可知:

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

 

对应于服务端可以说实在太cool了,简直就是一个标准的spring control!!!!

 

@Controller
@RequestMapping("/ajax")
public class CommonServControl {
	
	@RequestMapping(value = "/checkLoginName", method = RequestMethod.GET)
	public @ResponseBody boolean checkLoginName(@RequestParam String loginName) {
		boolean b = userServ.checkLoginName(loginName);
		return b;
	}
	
	@Autowired
	private UserServ userServ;
	
}

 

 这段代码相信实在是够“POJO”了吧。

当然注意!!!这样还不够!

 

 写道
Underneath the covers, Spring MVC delegates to a HttpMessageConverter to perform the serialization. In this case, Spring MVC invokes a MappingJacksonHttpMessageConverter built on the Jackson JSON processor. This implementation is enabled automatically when you use the mvc:annotation-driven configuration element with Jackson present in your classpath.

 这段话的意思是,spring使用“HttpMessageConverter”来进行工作,因此我们还需要做一件事:

在springMVC的XXX-servlet.xml文件中加入这么一行:

 

 

<mvc:annotation-driven />


命名空间为:
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	default-autowire="byName"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 

OK 完事!

这个是post方式,也差不多,呵呵

1. $("#account").submit(function() {
2. var account = $(this).serializeObject();
3. $.postJSON("account", account, function(data) {
4. $("#assignedId").val(data.id);
5. showPopup();
6. });
7. return false;
8. });

On the server-side, the Controller is more standard Java with Spring MVC annotations:

01. @RequestMapping(method=RequestMethod.POST)
02. public @ResponseBody Map<String, ? extends Object> create(@RequestBody Account account, HttpServletResponse response) {
03. Set<ConstraintViolation<Account>> failures = validator.validate(account);
04. if (!failures.isEmpty()) {
05. response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值