Java Struts验证用户名和密码无效
我使用的是struts1,也使用用户名和密码验证 register.jsp RegisterAction.java validation.xmlJava Struts验证用户名和密码无效,java,validation,struts-1,Java,Validation,Struts 1,我使用的是struts1,也使用用户名和密码验证 register.jsp RegisterAction.java validation.xml 更改struts-config.xml,如下所示 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/s
更改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>
然后跑
输出:--
是否有stacktrace错误?警告:未找到资源myFile\u en\u US.properties。在config.xml中,您没有指定“validation.xml”,请更改它并尝试..删除第二个/WEB-INF/validator-rules.xml并放置/WEB-INF/validation.xml是的,我修改了它,这次验证正在工作…但问题是错误消息没有显示,即“需要用户名”和“需要密码”我没有修改struts-config.xml,但我修改了validation.xml,它正在工作。嘿@rajeev我想在看到我上面的评论后,你已经将其更改为value=“/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml”/…是的……谢谢你。在我之前的评论中,我说,我没有修改struts cofig意味着在修改/WEB-INF/validator-rules.xml、/WEB-INF/validation.xml之后,除了validation.xml之外,我没有修改其他内容。
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");
}
}
}
<?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>
<!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>
#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.
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
<!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>
<?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>