问题是这样的:
regist.jsp页面:(应用到了validator客户端验证的功能)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html> <head> <title><bean:message key="lable.text.userlist" /> </title> <html:javascript formName="/regist" /> <script type="text/javascript" src='<c:url value="/scripts/common/jquery.js" />' type="text/javascript"> </script> <script type="text/javascript"> function checkUserName() { var userNameInput = document.getElementById("userName"); var userName = userNameInput.value; if (jQuery.trim(userName) == "") { document.getElementById("userNameSpan").innerHTML=""; return false; } var isNameValid = true; jQuery.ajax( type:"POST", url:'<c:url value="/users.do?method=checkUser" />', data:"userName=" + userName, dataType:"json", cache:false, async:false, success:function(data,textStatus){ isNameValid = data['isNameValid']; if(isNameValid){ document.getElementById("userNameSpan").innerHTML="该用户名可以使用"; } else{ document.getElementById("userNameSpan").innerHTML="该用户名已经被占用"; userNameInput.focus(); } } ); return isNameValid; } </script> </head> <body> <html:form action="/regist.do?method=regist" method="POST" onsubmit="javascript:if(validateUserForm(this)){ return checkUserName(this);}else{return false;}"> <bean:message key="lable.text.userName" /> <bean:message key="lable.text.colon" /> <html:text property="userName" styleId="userName" onblur="checkUserName()"></html:text> <span id="userNameSpan"></span> <br /> <bean:message key="lable.text.password" /> <bean:message key="lable.text.colon" /> <html:password property="password"></html:password> <br /> <bean:message key="lable.text.email" /> <bean:message key="lable.text.colon" /> <html:text property="email"></html:text> <html:submit> <bean:message key="lable.text.regist" /> </html:submit> </html:form> </body> </html:html>UserAction.java源码如下:
public class UserAction extends DispatchAction { private static IUserService userService = new UserServiceImpl(); // Logger logger = Logger.getLogger("UserAction"); public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UserForm loginForm = (UserForm) form; String userName = loginForm.getUserName(); String password = loginForm.getPassword(); Users user = null; try { user = userService.getUserByNameAndPassword(userName, password); } catch (Exception e) { e.printStackTrace(); } if (user == null) { return mapping.findForward(mapping.getInput()); } request.setAttribute("user", user); return mapping.findForward("success"); } public ActionForward toRegist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { saveToken(request); return mapping.findForward("regist"); } public ActionForward regist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionMessages messages = new ActionMessages(); if (!isTokenValid(request)) { messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "errors.invalid.token")); saveErrors(request, messages); saveToken(request); return mapping.findForward("regist"); } resetToken(request); UserForm loginForm = (UserForm) form; String userName = loginForm.getUserName(); String password = loginForm.getPassword(); String email = loginForm.getEmail(); Users user = new Users(); user.setName(userName); user.setPassword(password); user.setEmail(email); int result = 0; try { result = userService.addUser(user); } catch (RuntimeException e) { e.printStackTrace(); } if (result < 1) { request.setAttribute("fail", "failure,please re-regist!"); return mapping.findForward("regist"); } request.setAttribute("user", user); return mapping.findForward("success"); } public ActionForward checkUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String userName = request.getParameter("userName"); boolean isNameValid = userService.isNameValid(userName); response.getWriter().write("{\"isNameValid\":" + isNameValid + "}"); return null; } public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { List<Users> userList = null; try { userList = userService.findAll(); } catch (RuntimeException e) { e.printStackTrace(); } request.setAttribute("userList", userList); return mapping.findForward("userlist"); } }
if (!isTokenValid(request)) { messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "errors.invalid.token")); saveErrors(request, messages); saveToken(request); return mapping.findForward("regist"); }
整不明白了,哪位大侠指点一下啊。。。
附加问题:::
是否注意到这段代码:
// Logger logger = Logger.getLogger(UserAction.class);不知为什么会出错???有一本书是这样写的,难道是书写错了,而应该改写成
// Logger logger = Logger.getLogger("UserAction");