关于jquery的ajax的用户名唯一性总结
光标移动,触发ajax时间,然后到action里面判断是否存在,然后返回提示文字。
<td align="center">注册名</td><td><input type="text" name="user.userName" id="zhuceming"style="width: 160px;" onblur="checkOnlyUser(this.value);"></td>
<tr align="center"><span style="color: red"> ${onlyUserWord}</span>???????</td>??????</tr>
?
function checkOnlyUser(username){if(username == ""){ymPrompt.alert("不能为空",null,null,'用户信息提示框')return false;}$.ajax({type : "post",url : "doOnlyUser",beforeSend: function(XMLHttpRequest){ },data : {username : username ,random : Math.random()},success : function(data) {$(".wrong_msg span").html(data);$(".wrong_msg").show();}});}
?
进入struts.xml
<action name="doOnlyUser" method="onlyUser"><result name="success">/popedom/userManage.jsp</result><result name="nonono">/popedom/userManage.jsp</result></action>
?
?
进入action方法
/** * 验证唯一 */public String onlyUser() {try {HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();user = this.userService.getOnlyUser(username);if (user != null) {onlyUserWord = "用户名重复";} else {onlyUserWord = "恭喜你,可以注册";}out.println(onlyUserWord);out.flush();out.close();return "success";} catch (IOException e) {e.printStackTrace();return "nonono";}}
?
public User getOnlyUser(String userName) {Map<String, Object> paramMap = new HashMap<String, Object>();paramMap.put("userName", userName);return this.userMapper.getOnlyUser(paramMap);}
?
<select id="getOnlyUser" resultMap="userResultMap" parameterType="java.util.HashMap"> select u.* from ts_user_info u where u.USER_NAME=#{userName} </select>
?
总结:然后又回到页面,显示出来那个字符串关键字,就这么简单。