jsp代码//验证用户名是否存在 function userNum(inputname) { $.ajax({ url:"findByName.action", type:"POST", data:"userRegisterInfo.username=" + inputname, dataType:"json", success:function(data) { if(data.status == 1) { $("#u").html("<font color=‘red‘>+该用户不可用!</font>"); } else { $("#u").html("<font color=‘black‘>+该用户可用!</font>"); } }, error:function() { alert("error"); } }); }
struts.xml<package name="user_json" extends="json-default"> <action name="findByName" class="org.mln.bbs.action.FindNameAction"> <result type="json"> <!-- 这里指定将被Struts2序列化的属性,该属性在action中必须有对应的getter方法 --> <param name="root">jsonResult</param> </result> </action> </package>
action代码@Component("user1") @Scope("prototype") public class FindNameAction extends ActionSupport { private UserRegisterInfo userRegisterInfo; private UserService userService; private Map<String,Object> jsonResult; @Override public String execute() { jsonResult = new HashMap<String, Object>(); int userNum = userService.userNumByName(userRegisterInfo.getUsername()); if(userNum > 0) { jsonResult.put("valid", false); } else { jsonResult.put("valid", true); } return "success"; } public Map<String, Object> getJsonResult() { return jsonResult; } public void setJsonResult(Map<String, Object> jsonResult) { this.jsonResult = jsonResult; } public UserService getUserService() { return userService; } @Resource(name="userService") public void setUserService(UserService userService) { this.userService = userService; } public UserRegisterInfo getUserRegisterInfo() { return userRegisterInfo; } public void setUserRegisterInfo(UserRegisterInfo userRegisterInfo) { this.userRegisterInfo = userRegisterInfo; } }
时间: 2024-10-09 20:44:17