Hi all,
I'm new to
struts and wants to validate my form field in input.jsp for the minlength. If the length is less than 5 then the control must return back to the input.jsp other wise it must go to success.jsp.
But at present what happening that if user enters less than 5 character than also the success.jsp page is being displayed instead of input.jsp.
Follwing is the struts-config file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config
PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<global-forwards>
<forward name="home" path="/home.do" />
</global-forwards>
<form-beans>
<form-bean name="lookupForm" type="ch03.LookupForm" />
<form-bean name="inputForm" type="ch03.InputForm" />
</form-beans>
<action-mappings>
<action path="/lookup" type="ch03.LookupAction" name="lookupForm" >
<forward name="success" path="/quote.jsp" />
<forward name="failure" path="/index.jsp" />
</action>
<action path="/inputSubmit" type="ch03.InputAction" name="inputForm" scope="request" validate="false" input="/input.jsp" >
<forward name="success" path="/success.jsp" />
</action>
<action path="/home" forward="/quote.jsp" />
</action-mappings>
<message-resources parameter="ApplicationResources" />
<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>
--------------------------------------------------------------------
Follwing is the input.jsp page
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<body>
<logic:messagesPresent>
There were error
<ul>
<font color='red'>
<html:messages id="error" >
<li><%=error %></li>
</html:messages>
</font>
</ul>
</logic:messagesPresent>
<html:form action="inputSubmit">
<bean:message key="inputForm.userName" />
<html:text property="userName" /> <br/>
<html:submit value="ok" />
</html:form>
</body>
</html>
---------------------------------------------------------
Follwing is InputForm.java
package ch03;
import org.apache.struts.action.ActionForm;
import org.apache.struts.validator.ValidatorForm;
public class InputForm extends ValidatorForm
{
public
String userName;
public String getUserName()
{
return userName;
}
public void setUserName(String string)
{
userName=string;
}
}
----------------------------------------------------
Following is InputAction.java
package ch03;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InputAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception
{
InputForm inputForm=(InputForm)form;
System.out.println(inputForm.getUserName());
return mapping.findForward("success");
}
}
--------------------------------------------------------------------
and follwing is snippet from validation.xml file
<form name="inputForm">
<field property="userName"
depends="minlength">
<arg0 key="inputForm.userName" />
<arg1 key="$(var:minlength)"
name="minlength"
resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>
</form>