簡體   English   中英

Struts驗證用戶名和密碼不起作用

[英]Struts validation username and password not working

我正在使用struts1並使用用戶名和密碼進行驗證。請參見以下代碼

register.jsp

    <%@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"%>

<bean:message key="my.title" />

<html:form action="register" method="GET">

    <bean:message key="my.un" />                    
    <html:text property="userName"></html:text>
    <html:errors property="userName" />

    <br>

    <bean:message key="my.pwd" />
    <html:text property="password"></html:text>
    <html:errors property="password" />

    <br>

    <html:submit>
        <bean:message key="my.btn.cap" />
    </html:submit>

</html:form>

<logic:notEmpty name="msg" scope="request" >
    <bean:write name="msg" scope="request" />
</logic:notEmpty>

RegisterForm.java

package com.rajeev.form;
import org.apache.struts.validator.ValidatorForm;

public class RegisterForm extends ValidatorForm {
    String userName,password;

    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;
    }
}

RegisterAction.java

package com.rajeev.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.rajeev.form.RegisterForm;

public class RegisterAction extends Action {

    public RegisterAction() {
        System.out.println("RegisterAction const");
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println("in execute method");
        RegisterForm registerForm = (RegisterForm) form;

        String userName = registerForm.getUserName();
        String password = registerForm.getPassword();

        if (userName.equals("Rajeev") && password.equals("java")) {
            request.setAttribute("msg", "valid");
            return mapping.findForward("result");
        } else {
            request.setAttribute("msg", "invalid");
            return mapping.findForward("result");
        }

    }
}

validation.xml中

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>

    <formset>

        <form name="rf">
            <field property="userName" depends="required">
                <arg key="my.un" position="0" />
            </field>
            <field property="password" depends="required">
                <arg key="my.pwd" position="0" />
            </field>
        </form>
    </formset>

</form-validation>

struts-config.xml中

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <form-beans>
        <form-bean name="rf" type="com.rajeev.form.RegisterForm"></form-bean>
    </form-beans>
    <action-mappings>
        <action path="/register" name="rf" scope="request"
            type="com.rajeev.action.RegisterAction" input="/register.jsp">
            <forward name="result" path="/register.jsp"></forward>
        </action>
    </action-mappings>

    <message-resources parameter="myFile"></message-resources>

    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validator-rules.xml" />
    </plug-in>
</struts-config>

myFile.properties

#presentation labels
my.un=UserName
my.pwd=Password

my.btn.cap=Login
my.title=<h1>login page</h1>

#default error message
errors.userName.required={0}is required.

我還附加了工作區中的validateator-rules.xml文件。上面的程序無需驗證即可正常工作。我編寫了用戶名和密碼(要求用戶名和密碼)的驗證碼,如果有人單擊登錄而未在其中輸入用戶名和密碼文本框。有人幫我為什么它不起作用。

在此處輸入圖片說明

安慰

Mar 06, 2014 2:08:51 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\jdk1.7.0_17\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jdk1.7.0_17/bin/../jre/bin/server;C:/Program Files/Java/jdk1.7.0_17/bin/../jre/bin;C:/Program Files/Java/jdk1.7.0_17/bin/../jre/lib/amd64;C:\Windows\SYSTEM32\WBEM;C:\Program Files\Java\jdk1.7.0_17\bin;C:\ORACLEXE\APP\ORACLE\PRODUCT\10.2.0\SERVER\BIN;.;.;C:\PROGRAM FILES\MYSQL\MYSQL SERVER 5.5\BIN;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;;C:\Program Files\Dell\DW WLAN Card;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\MinGW\bin;.;E:\eclipse;;.
Mar 06, 2014 2:08:51 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Validations' did not find a matching property.
Mar 06, 2014 2:08:51 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ValidationWith_XML_Validation' did not find a matching property.
Mar 06, 2014 2:08:51 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-9001"]
Mar 06, 2014 2:08:51 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8010"]
Mar 06, 2014 2:08:51 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 918 ms
Mar 06, 2014 2:08:52 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 06, 2014 2:08:52 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.33
Mar 06, 2014 2:08:53 PM org.apache.struts.action.ActionServlet initChain
INFO: Loading chain catalog from jar:file:/E:/javahyd/eclipse_struts/.metadata/.plugins/org.eclipse.wst.server.core/tmp4/wtpwebapps/Validations/WEB-INF/lib/struts-core-1.3.10.jar!/org/apache/struts/chain/chain-config.xml
Mar 06, 2014 2:08:54 PM org.apache.struts.action.ActionServlet initChain
INFO: Loading chain catalog from jar:file:/E:/javahyd/eclipse_struts/.metadata/.plugins/org.eclipse.wst.server.core/tmp4/wtpwebapps/ValidationWith_XML_Validation/WEB-INF/lib/struts-core-1.3.10.jar!/org/apache/struts/chain/chain-config.xml
Mar 06, 2014 2:08:54 PM org.apache.struts.validator.ValidatorPlugIn initResources
INFO: Loading validation rules file from '/WEB-INF/validator-rules.xml'
Mar 06, 2014 2:08:54 PM org.apache.struts.validator.ValidatorPlugIn initResources
INFO: Loading validation rules file from '/WEB-INF/validator-rules.xml'
Mar 06, 2014 2:08:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9001"]
Mar 06, 2014 2:08:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8010"]
Mar 06, 2014 2:08:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2960 ms
Mar 06, 2014 2:08:55 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource myFile_en_US.properties Not Found.
Mar 06, 2014 2:08:55 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING:   Resource myFile_en.properties Not Found.
Mar 06, 2014 2:15:39 PM org.apache.struts.chain.ComposableRequestProcessor init
INFO: Initializing composable request processor for module prefix ''
Mar 06, 2014 2:15:40 PM org.apache.commons.validator.ValidatorResources getForm
WARNING: Form 'rf' not found for locale 'en_US'
Mar 06, 2014 2:15:40 PM org.apache.struts.chain.commands.servlet.CreateAction createAction
INFO: Initialize action of type: com.rajeev.action.RegisterAction
RegisterAction const
in execute method

如下更改您的struts-config.xml

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <form-beans>
        <form-bean
            name="rf"
            type="com.rajeev.form.RegisterForm"
        ></form-bean>
    </form-beans>
    <action-mappings>
        <action
            path="/register"
            name="rf"
            scope="request"
            type="com.rajeev.action.RegisterAction"
            input="/register.jsp"
        >
            <forward
                name="result"
                path="/register.jsp"
            ></forward>
        </action>
    </action-mappings>
    <message-resources parameter="myFile"></message-resources>
     <plug-in className="org.apache.struts.validator.ValidatorPlugIn" >
    <set-property property="pathnames"
    value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/>

   </plug-in>
</struts-config>

然后是validation.xml,如下所示

   <?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>

    <formset>

        <form name="rf">
            <field property="userName" depends="required">
                <arg key="my.un" position="0" />
                <msg name="required" key="errors.userName.required"/>
            </field>
            <field property="password" depends="required">
                <arg key="my.pwd" position="0" />
                 <msg name="required" key="errors.userName.required"/>
            </field>
        </form>
    </formset>

</form-validation>

並運行..

輸出 :-

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM