首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

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

2012-10-08 
spring3 MVC下结合jquery ajax简单使用--超级简单今天需要写一个简单的应用:用户注册时检查用户名是否可以

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;}@Autowiredprivate UserServ userServ;}

?

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

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

?

<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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://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:

view sourceprint?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);

热点排行