我的SSH项目之旅(2.用户注册–前台)

2008年07月18日 JAVA 暂无评论

user表的结构如下

加入struts

采用DispatchAction


Struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
  <form-bean name="userForm" type="dj.fantlam.myssh.struts.form.UserForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
  <action
    attribute="userForm"
    input="/jsp/errors.jsp"
    name="userForm"
    parameter="status"
    path="/jsp/user"
    scope="request"
    type="dj.fantlam.myssh.struts.action.UserAction" />
  <forward name="regsuc" path="/jsp/index.jsp" />
  <forward name="regfail" path="/jsp/register.jsp"></forward>

  </action-mappings>

  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
  <message-resources
    parameter="dj.fantlam.myssh.struts.ApplicationResources" />
  <plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
      value="/WEB-INF/classes/applicationContext.xml" />
  </plug-in>
</struts-config>

errors.jsp

<body>
<center>
<jsp:include flush="true" page
="inc/template.html"/>
<h1>程序出现了以下错误</h1>
<html:errors/>
<p2><<a href="#" onclick="history.back()">返回上一页</a></p2>
</center>

</body>







register.jsp
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
  <title>MLDN</title>

  <META NAME="Generator" CONTENT="Struts + Spring + Hibernate + MySQL + Tomcat + CP">

  <META NAME="Author" CONTENT="fantlam">
  <META NAME="Keywords" CONTENT="SSH,tomcat,mysql">
  <META NAME="Description" CONTENT=" http://fantlam.blogbus.com">
</head>
<body>
<center>
<jsp:include flush="true" page="inc/template.html"/>
<h1>用户注册</h1>
<html:form action="" method="post">
用户ID<html:text property="userid"></html:text>
用户密码:<html:password property="userpwd"></html:password>
确认密码:<html:password property="confirmpwd"></html:password>
丢失密码提示问题:<html:text property="userques"></html:text>
丢失密码问题答案:<html:text property="userans"></html:text>
验证码:<html:text property="checkcode"></html:text>
<img src="image.jsp"><br>
对于property的属性如”checkcode”对应于Userform.javacheckcode属性,并要生成settergetter方法,如果userform没有呢页面就呈现不出
<input type="hidden" name="status" value="register">对应UserAction.java
public ActionForward register()
<input type="hidden" name="type" value="1">
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
</center>
</body>

</html:html>


参考struts的API
public class ActionErrors
extends ActionMessages
implements java.io.Serializable

Action
saveErrors(javax.servlet.http.HttpSession session, ActionMessages errors)
          Save the specified error messages keys into the appropriate session attribute for use by the <html:messages> tag (if messages="false") or <html:errors>, if any error messages are required.






Userform.java

加多三个属性(注意都要生成settergetter方法)

private String confirmpwd;
private String checkcode;  
private int type;

  // 1:表示注册功能
  // 2:表示登陆
  // 3:表示忘记密码,确认用户是否存在

  // 4:表示修改密码


public ActionErrors validate(ActionMapping mapping,
      HttpServletRequest request) {
   
//Struts前台表单的验证,根据type做不同的验证
    ActionErrors errors = new ActionErrors();
    if (type == 1) {
      if (this.userid == null || "".equals(this.userid)) {

        errors.add("userid", new ActionMessage("user.userid.null"));
      }
      if (this.userpwd == null || "".equals(this.userpwd)) {
        errors.add("userpwd", new ActionMessage("user.userpwd.null"));

      } else {
        if (!(this.userpwd.equals(this.confirmpwd))) {
            errors.add("confpwd", new ActionMessage(
                "user.confpwd.error"));
        }
      }
      if (this.userques == null || "".equals(this.userques

)) {

        errors.add("userques", new ActionMessage("user.userques.null"));

      }
      if (this.userans == null || "".equals(this.userans)) {
        errors.add("userans", new ActionMessage("user.userans.null"));

      }
      if(this.checkcode==null||"".equals(this.checkcode)){
          errors.add("checkcode", new ActionMessage("user.checkcode.null"));
       
      }
    }
    return errors;

  }



UserAction.java

/*

* Generated by MyEclipse Struts

* Template path: templates/java/JavaClass.vtl

*/

package dj.fantlam.myssh.struts.action;



import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

import org.apache.struts.actions.DispatchAction;



import dj.fantlam.myssh.struts.form.UserForm;



/**

* MyEclipse Struts

* Creation date: 07-15-2008

*

* XDoclet definition:

* @struts.action path="/user" name="userForm" input="error.jsp" parameter="status" scope="request" validate="true"



*/

UserAction是继承自DispatchAction的,根据传来的status的值判断交给那个ActionForward处理

public class UserAction extends DispatchAction {

  /*

  * Generated Methods

  */



  /**

  * Method execute

  * @param mapping

  * @param form

  * @param request

  * @param response

  * @return ActionForward

  */

  public ActionForward register(ActionMapping mapping, ActionForm form,

      HttpServletRequest request, HttpServletResponse response) {

    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub

   

    /* ActionErrorsActionMessages的关系如下

    * public class ActionErrors extends ActionMessages implements

    * java.io.Serializable

    *

    *

    * Action

    * saveErrors(javax.servlet.http.HttpSession session,

  * ActionMessages errors) Save the specified error messages keys into

  * the appropriate session attribute for use by the <html:messages>tag

        * (if messages="false") or <html:errors>, if any error messages are required.

    */

//验证输入的验证码是否正确

  String ccode=(String)request.getSession().getAttribute("ccode");

    String checkcode=userForm.getCheckcode();

    if(!(ccode.equals(checkcode))){

      ActionMessages errors=new ActionMessages();

      errors.add("ccode", new ActionMessage("checkcode.error"));

      super.saveErrors(request, errors);

      return mapping.getInputForward();

    }

   

    return null

;

  }

 

  public ActionForward execute(ActionMapping mapping, ActionForm form,

      HttpServletRequest request, HttpServletResponse response) {

    UserForm userForm = (UserForm) form;// TODO Auto-generated method stub

    return null;

  }

}

给我留言