整合struts2 jQuery validate 插件的远程Ajax验证

整合struts2jQueryvalidate插件的远程Ajax验证

法:-----对用户名进行验证

<tr>

<td>

<labelfor="user.username">

用户名(<fontcolor=red>*</font>)

</label>

</td>

<td>

<inputname="user.username"id="input1"/>

</td>

</tr>

js:

$("#info").validate(

{

rules:{

"user.username":{

required:true,

minlength:5,

maxlength:15,

remote:"/ssh2/test/remotetestAction.action"

},

........

}});

struts.xml:

<actionname="remotetestAction"

class="com.small.action.RegAction"method="remoteTest">

<resulttype="stream">

<paramname="user.username">inputStream</param><!--默认就为inputStream-->

</result>

</action>

action做处理:

publicStringremoteTest(){

booleanb=false;

if(null==user)

b=true;

else{

logger.info("AJAX验证用户:"+user.getUsername());

Useruserinfo=this.userDaoImpl.getUser(user);

if(null!=userinfo){

logger.info("AJAX验证用户:"+user.getUsername()+"---->已经存在....");

b=true;

}else

logger.info("AJAX验证用户:"+user.getUsername()+"---->验证通过....");

}

returnrenderText(b?"false":"true");

}

protectedStringrender(Stringtext,StringcontentType){

try{

HttpServletResponseresponse=ServletActionContext.getResponse();

response.setContentType(contentType);

response.getWriter().write(text);

}catch(IOExceptione){

}

returnnull;

}

/**

*直接输出字符串.

*/

protectedStringrenderText(Stringtext){

returnrender(text,"text/plain;charset=UTF-8");

}

相关推荐