0

即使在 struts-config.xml 中清楚地描述了我的 register.jsp 的操作,它也没有执行。我还有一个运行良好的 login.jsp 页面。register.jsp 没有抛出任何错误,但它没有执行。请看一下我的代码。谢谢

注册.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>Register</h1>
        <html:form action="register">
             <bean:message key="label.username" />
             <html:text property="userName"></html:text>
             <html:errors property="userName" />
             <br/>
             <bean:message key="label.name"/>
            <html:text property="name"></html:text>
             <html:errors property="name"/>
             <br/>
              <bean:message key="label.mobile"/>
            <html:text property="phone_number"></html:text>
             <html:errors property="phone_number"/>
             <br/>
            <html:submit/>
            <html:reset/>
        </html:form>

          <%
    if(request.getAttribute("msg1")!=null){
    %>
    <%= request.getAttribute("msg1") %>
    <%} request.setAttribute("msg1","null"); %>
      <%
    if(request.getAttribute("msg2")!=null){
    %>
    <%= request.getAttribute("msg2") %>
    <%} request.setAttribute("msg2","null"); %>
      <%
    if(request.getAttribute("msg3")!=null){
    %>
    <%= request.getAttribute("msg3") %>
    <%} request.setAttribute("msg3","null"); %>
    </body>
    </html>

**RegisterAction**
public class RegisterAction extends Action {
    @SuppressWarnings({ "rawtypes", "unused" })
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Session session = HibernateUtil.getSessionFactory().openSession();
         Transaction trans = null;
            LoginForm loginForm = (LoginForm) form;
            String email = loginForm.getUserName();
            String name = loginForm.getName();
            int mobile = loginForm.getPhone_number();

            boolean new_user = true;
            LoginForm lform = null;
            String result = "success";
            try{
                trans = session.beginTransaction();

                List ulogin = session.createQuery("from LoginForm").list();
                for (Iterator iterator = ulogin.iterator(); iterator.hasNext();){

                    lform = (LoginForm) iterator.next();
                    if (lform.getUserName().equals(email) && lform.getPhone_number()==mobile){
                        result = "failure";
                        break;
                    }
                    if(lform.getUserName().equals(email)){
                        result = "failure1";
                        break;
                    }
                    if(lform.getPhone_number() == mobile){
                        result = "failure2";
                        break;
                    }
                }
                if(result.equals("failure")){
                    request.setAttribute("msg3", "Email and mobile number already exist");
                }
                if(result.equals("failure1")){
                    request.setAttribute("msg1", "Email already exist");
                }
                if(result.equals("failure2")){
                    request.setAttribute("msg2", "mobile number already exist");
                }
                if(result.equals("success")){
                    String password = Long.toHexString(Double.doubleToLongBits(Math.random()));
                    lform = new LoginForm();
                    lform.setName(name);
                    lform.setPhone_number(mobile);
                    lform.setUserName(email);
                    lform.setPassword(password);
                    lform.setNew_user(new_user);
                    session.save(lform);

                    SendEmail sendEmail = new SendEmail();
                    request.setAttribute("msg4", "Registered Successfully");
            }
                }
            catch (HibernateException e){
                if (trans!=null)
                    trans.rollback();
            e.printStackTrace(); }
             return mapping.findForward(result);
}
    }

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_3.dtd">

<struts-config>
    <form-beans>
        <form-bean name="LoginForm"
            type="com.company.strutsuser.LoginForm" />
    </form-beans>


    <global-exceptions>
    </global-exceptions>
    <global-forwards></global-forwards>

    <action-mappings>
        <action path="/login" name="LoginForm" validate="true" input="/login.jsp"
            type="com.company.strutsuser.LoginAction">
            <forward name="success" path="/success.jsp" />
            <forward name="failure" path="/login.jsp" />
            <forward name="failure1" path="/ChangePassword.jsp" />
            <forward name="failure2" path="/login.jsp" />
              </action>
              <action path ="/register" name="LoginForm" validate="true" input="/register.jsp"
            type="com.company.strutsuser.RegisterAction">
            <forward name="success" path="/login.jsp" />
            <forward name="failure" path="/register.jsp" />
            <forward name="failure1" path="/register.jsp" />
            <forward name="failure2" path="/register.jsp" />
            </action>
    </action-mappings>


    <message-resources parameter="resource.MessageResource"></message-resources>

</struts-config>

LoginForm(我的 bean 类)

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

@SuppressWarnings("serial")
public class LoginForm extends ActionForm{
    private String userName;
    private String password;

    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ActionErrors actionErrors = new ActionErrors();

        if(userName == null || userName.trim().equals("")) {
            actionErrors.add("userName", new ActionMessage("error.username"));
        }
        try {
        if(password == null || password.trim().equals("")) {
            actionErrors.add("password", new ActionMessage("error.password"));
        }
        if(name == null || name.trim().equals("")) {
            actionErrors.add("name", new ActionMessage("error.name"));
        }
        if(phone_number == 0) {
            actionErrors.add("phone_number", new ActionMessage("error.mobile"));
        }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return actionErrors ;
    }

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPhone_number() {
        return phone_number;
    }


    private String name;
    private int phone_number;
    public void setPhone_number(int phone_number) {
        this.phone_number = phone_number;
    }

    public boolean isNew_user() {
        return new_user;
    }

    public void setNew_user(boolean new_user) {
        this.new_user = new_user;
    }
    boolean new_user;
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

安慰

Aug 07, 2013 3:38:58 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;.
Aug 07, 2013 3:38:58 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:StrutsUser' did not find a matching property.
Aug 07, 2013 3:38:58 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Aug 07, 2013 3:38:58 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 801 ms
Aug 07, 2013 3:38:58 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Aug 07, 2013 3:38:58 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.37
Aug 07, 2013 3:38:59 PM org.apache.struts.action.ActionServlet initChain
INFO: Loading chain catalog from jar:file:/C:/Users/Edith/Documents/Java/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/StrutsUser/WEB-INF/lib/struts-core-1.3.10.jar!/org/apache/struts/chain/chain-config.xml
Aug 07, 2013 3:39:00 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Aug 07, 2013 3:39:00 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Aug 07, 2013 3:39:00 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/52  config=null
Aug 07, 2013 3:39:00 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1982 ms
Aug 07, 2013 3:39:01 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource resource/MessageResource_en_US.properties Not Found.
Aug 07, 2013 3:39:01 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource resource/MessageResource_en.properties Not Found.
Aug 07, 2013 3:39:07 PM org.apache.struts.chain.ComposableRequestProcessor init
INFO: Initializing composable request processor for module prefix ''
4

0 回答 0