• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Form is not being validated

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<action path="/inputSubmit" type="ch03.InputAction" name="inputForm" scope="request" validate="false" input="/input.jsp" >



Change it to:

 
as chhipa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply but setting

validate="true"

is not making any difference.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The suggestion by Jaikiran is needed (set validate="true"). Other than that, it looks pretty good. Do you have validation working for other pages? I would suggest declaring userName as private in your form, but I don't see where that would make a difference.

- Brent
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Honestly, i havent used Struts a lot. But, shouldnt you be overriding the validate method in your InputForm? Something like:

 
as chhipa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have found the solution.Actually I'm using struts 1.3.5. and in this case culprit is validation.xml file.

Previously my validation.xml file was as follwing which is used in the older version

<form name="inputForm">

<field property="userName" depends="minlength">
<arg0 key="inputForm.userName" resource="true" />
<arg1 name="minlength" key="${var:minlength}" resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>

</form>


but in struts 1.3.5 it will be like this
(Pay attention to arg element and position attribute )

<form name="inputForm">

<field property="userName" depends="minlength">
<arg key="inputForm.userName" resource="true" position="0"/>
<arg name="minlength" key="${var:minlength}" resource="false"
position="1"/>
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
</field>

</form>
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic