采用struts2.0+jquery+json写的一个登录功能,
action内容如下:
public class UserAction extends BaseAction implements Preparable { private static final long serialVersionUID = 8992700772962868363L; private User user; private String result; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public void prepare() throws Exception { setUser(null); } public void login() throws JDOMException, IOException { this.getResponse().setContentType("application/json; charset=utf-8"); // this.getResponse().setCharacterEncoding("UTF-8"); PrintWriter pw = this.getResponse().getWriter(); AnalyseXML axml = new AnalyseXML(); User tmp = axml.resolve(user);// userService.getUser(user);查询用户是否存在 // if (tmp == null) { // tmp = userService.getUser(user); // } String code = this.getRequest().getParameter("code"); String auth = Util.getString(this.getSession().getAttribute("auth"));// 验证码 if (tmp != null && !tmp.isLocked()) {// isLocked()为false时用户是启用状态 if (auth.equals(code)) { result = "{\"Msg\": \"登录成功!\",\"IsSuccess\":true}"; } else { result = "{\"Msg\": \"验证码错误,登录失败!\",\"IsSuccess\":false}"; } } else { result = "{\"Msg\": \"用户名或密码错误,登录失败!\",\"IsSuccess\":false}"; } // System.out.println("result=" + result); pw.print(result); } public void logout() { } }
login.jsp页面内容:
<script src="/js/jquery/jquery-1.3.2.js" type="text/javascript"></script> <script src="/js/jquery/jquery.validate.js" type="text/javascript"></script> <script src="/js/jquery/jquery.form.js" type="text/javascript"></script><!-- 此js如果缺失,登录后页面将接受不到从struts返回的信息,而是提示用户下载.do文件 --> <form id="Lform" action="/user/login.do" method="post"> <div id="div_un"><input type="text" name="user.username" id="input_un" class="required" /></div> <div id="div_pw"><input type="password" name="user.passwd" id="input_pw" class="required" /></div> <div id="div_vc"><input type="text" name="code" id="input_vc" class="required" /></div> <div id="vc"><img id="auth" src="/authImag.st"></div> <div id="div_login"><input type="submit" id="btn_login" /></div> </form> <script language="javascript"> $(document).ready(function() { //定义ajaxform的调用参数 var options = { beforeSubmit: function() { return true; }, dataType: "json", success: function(data) { alert(data.Msg); if (data.IsSuccess) { location.href="/back.jsp"; //CloseModelWindow(null,true); }else{ alert("登录失败,请您重新登录!"); document.getElementById("auth").src = "/authImag.st?tmp="+Math.random()*10000; } } }; $("#Lform").validate({ submitHandler: function(form) { $("#Lform").ajaxSubmit(options); }, errorElement: "div", errorClass: "cusErrorPanel", errorPlacement: function(error, element) { showerror1(error, element); } }); function showerror1(error, target) { var pos = target.position(); var height = target.height(); var newpos = { left: pos.left, top: pos.top + height + 2 } var form = $("#Lform"); error.appendTo(form).css(newpos); } }); </script>
struts.xml内容如下:
<package name="user" extends="default" namespace="/user"> <action name="login" class="UserAction" method="login" > </action> <action name="logout" class="UserAction" method="logout" > </action> </package>