Reputation: 5645
I am using struts1 and also using validation for username and password.see the below code
<%@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>
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;
}
}
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.
I have also attached validator-rules.xml file in the workspace.The above program is working fine without validation.I have written validation code for username and password(username required and password required),if person click login without entering username and password in the text box.Any one help me why it is not working.
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
Upvotes: 1
Views: 2961
Reputation: 1477
change your struts-config.xml as below
<!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>
and then validation.xml as below
<?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>
and run ..
output:--
Upvotes: 1