ajax validate



<td>
<label for="user.username">
用戶名(<font color=red>*</font>)
</label>
</td>
<td>
<input name="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:

<action name="remotetestAction"
class="com.small.action.RegAction" method="remoteTest">
<result type="stream">
<param name="user.username">inputStream</param><!-- 默認就爲inputStream -->
</result>
</action>

action做處理:

public String remoteTest() {
boolean b = false;

if (null == user)
b = true;
else {
logger.info("AJAX驗證用戶:" + user.getUsername());
User userinfo = this.userDaoImpl.getUser(user);
if (null != userinfo) {
logger.info("AJAX驗證用戶:" + user.getUsername() + "---->已經存在....");
b = true;
} else
logger.info("AJAX驗證用戶:" + user.getUsername() + "---->驗證通過....");
}
return renderText(b ? "false" : "true");
}

protected String render(String text, String contentType) {
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(contentType);
response.getWriter().write(text);
} catch (IOException e) {
}
return null;
}

/**
* 直接輸出字符串.
*/
protected String renderText(String text) {
return render(text, "text/plain;charset=UTF-8");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章