You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Filippov, Andrey" <an...@t-systems.ru> on 2008/03/18 08:55:02 UTC

Custom validation with Struts2

Hello everybody,

 

I need to validate only 1 field on the jsp. It value should be between 1 and 999999. Initially I started to implement through int validation but it seemed to go wrong - it works with some limitation - maximum value should not exceed 1000 or something like that. I am trying to implement validation through overriding validate() method. I use tiles together with Struts 2.0.11. I could not tune it to work correctly. Could someone help, please?

 

Here are my snippets:

 

 

Struts.xml:

 

            <action name="EmployeeCard"

                  class="com.tsystems.tintra4.actions.EmployeeCard">

                  <interceptor-ref name="i18n" />

                  <interceptor-ref name="roles">

                        <param name="allowedRoles">WTT_CARDS_EDITOR</param>

                  </interceptor-ref>

                  <!-- interceptor-ref name="workflow"/-->

                  <interceptor-ref name="workflow"/>

                  <result type="tiles" name="SUCCESS">def_page_employee_card</result>

                  <result type="tiles" name="input">

                        def_page_employee_card

                  </result>

            </action>

 

 

Jsp:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

 

<!--Reference to the Employee profile-->

 

<div>

<s:url id="url_fio" action="GetEmployee" includeParams="none">

      <s:param name="employeeId"><s:property value="%{EmpNo}"/></s:param>

</s:url>

<s:a href="%{url_fio}">

      <s:property value="%{fio}"/>

</s:a>

</div>

<br/>

 

<!--Adding new card-->

 

<s:if test="addCard == true">

<s:text name="card_adding_proccess"></s:text>

<s:fielderror/>

<s:form method="GET" validate="true">

      <s:textfield name="cardNo" label="%{getText('card_add_edit_field')}"></s:textfield>

      <s:textfield name="EmpNo" value="%{EmpNo}"></s:textfield>

      <s:hidden name="editCard" value="%{true}"></s:hidden>

      <s:hidden name="addCardFinal" value="%{true}"></s:hidden>

      <s:submit value="%{getText('button_submit')}"/>

</s:form>

</s:if>

 

<!--Editing new card-->

 

<s:if test="editCard == true">

<s:text name="card_editing_proccess"></s:text>

<s:fielderror/>

<s:form method="GET" validate="true">

      <s:textfield name="cardNo" label="%{getText('card_add_edit_field')}"></s:textfield>

      <s:hidden name="EmpNo" value="%{EmpNo}"></s:hidden>

      <s:hidden name="editCard" value="%{true}"></s:hidden>

      <s:hidden name="editCardFinal" value="%{true}"></s:hidden>

      <s:submit value="%{getText('button_submit')}"/>

</s:form>

</s:if>

 

JAVA Action:

 

public void validate() {

            request = ServletActionContext.getRequest();

            String tmp = "";

            try {

                  tmp = request.getParameter("cardNo");

                  Integer i = this.getCardNo();

                  if (tmp != null && tmp.length() > 0) {

                        int tmpInt = Integer.parseInt(tmp);

                        this.setCardNo(tmpInt);

                        System.out.println("Card no (validation) = " + cardNo);

                        if (cardNo < 1 || cardNo > 999999) {

 

                             addFieldError("cardNo", "Must be int");

                             

                             

                             System.out.println("INPUT...");

                        }

                  }

            } catch (Exception e) {

 

                  e.printStackTrace();

            }

 

      }

 

Thanx a lot.

 

Best regards, Filippov Andrey


Re: Custom validation with Struts2

Posted by Lukasz Lenart <lu...@googlemail.com>.
>              request = ServletActionContext.getRequest();
>              String tmp = "";
>              try {
>                    tmp = request.getParameter("cardNo");
>                    Integer i = this.getCardNo();
>                    if (tmp != null && tmp.length() > 0) {
>                          int tmpInt = Integer.parseInt(tmp);

Remove this part above

>                          this.setCardNo(tmpInt);

You don't have to do this, it will be done by Struts2

>                          System.out.println("Card no (validation) = " + cardNo);
>                          if (cardNo < 1 || cardNo > 999999) {
>                               addFieldError("cardNo", "Must be int");
>                               System.out.println("INPUT...");
>                          }

That's all you have to have in your validate() method (if cardNo has
getter and setter defined in action class)
Remove also validate=true from your form, because you don't use client
side validation.


Regards
-- 
Lukasz

http://www.linkedin.com/in/lukaszlenart

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Custom validation with Struts2

Posted by Dave Newton <ne...@yahoo.com>.
The OP isn't really using the built-in intrange validator, but doing it manually (for some reason).

The problem, however, lies in the OP's configuration fragment:

<action name="EmployeeCard" 
        class="com.tsystems.tintra4.actions.EmployeeCard">
  <interceptor-ref name="i18n" />
  <interceptor-ref name="roles">
    WTT_CARDS_EDITOR
  </interceptor-ref>
  <interceptor-ref name="workflow"/>
  <result type="tiles" name="SUCCESS">def_page_employee_card</result>
  <result type="tiles" name="input">def_page_employee_card</result>
</action>

As soon as you start defining an action's interceptors you are defining *all* the action's interceptors. 

In other words, this action will *only* be using the "i18n", "roles", and "workflow" interceptors. This bypasses the default stack, which includes the "validation" interceptor--the interceptor that calls an action's validate() method.

In order to call the validate() method we must include the "validation" interceptor or stack that includes it.

I'm also not sure if result names are case-sensitive, but if they are, the above result named "SUCCESS" will not be found by an action returning "success" or ActionSupport.SUCCESS. As to why the OP is bypassing the bundled int range validator, not sure, but unless there's a good reason for it, I wouldn't bother re-writing existing functionality.

Dave

--- On Wed, 9/10/08, Priyanka.dandekar wrote:
> If Integer Range Validator does not work for you then you
> shoud consider regular expression validation for same, its 
> can be done by restricting number of digits and allowing 
> only numeric characters.
> 
> Here is an example of similar problem 
> http://struts-2-developers.blogspot.com/2008/08/struts-2-integer-validation-example.html
> Struts 2 Integer Validation  
> 
> 
> fil78 wrote:
> > 
> > Hello everybody,
> > 
> >  
> > 
> > I need to validate only 1 field on the jsp. It value
> should be between 1
> > and 999999. Initially I started to implement through
> int validation but it
> > seemed to go wrong - it works with some limitation -
> maximum value should
> > not exceed 1000 or something like that. I am trying to
> implement
> > validation through overriding validate() method. I use
> tiles together with
> > Struts 2.0.11. I could not tune it to work correctly.
> Could someone help,
> > please?
> > 
> >  
> > 
> > Here are my snippets:
> > 
> >  
> > 
> >  
> > 
> > Struts.xml:
> > 
> >  
> > 
> >             <action
> name="EmployeeCard"
> > 
> >                  
> class="com.tsystems.tintra4.actions.EmployeeCard">
> > 
> >                   <interceptor-ref
> name="i18n" />
> > 
> >                   <interceptor-ref
> name="roles">
> > 
> >                        
> WTT_CARDS_EDITOR
> > 
> >                  
> </interceptor-ref>
> > 
> >                   <!--
> interceptor-ref name="workflow"/-->
> > 
> >                   <interceptor-ref
> name="workflow"/>
> > 
> >                   <result
> type="tiles"
> >
> name="SUCCESS">def_page_employee_card</result>
> > 
> >                   <result
> type="tiles" name="input">
> > 
> >                        
> def_page_employee_card
> > 
> >                   </result>
> > 
> >             </action>
> > 
> >  
> > 
> >  
> > 
> > Jsp:
> > 
> >  
> > 
> > <%@ page language="java"
> contentType="text/html; charset=UTF-8"
> > 
> >     pageEncoding="UTF-8"%>
> > 
> > <%@ taglib prefix="s"
> uri="/struts-tags" %>
> > 
> >  
> > 
> > <!--Reference to the Employee profile-->
> > 
> >  
> > 
> > <div>
> > 
> > <s:url id="url_fio"
> action="GetEmployee"
> includeParams="none">
> > 
> >       <s:param
> name="employeeId"><s:property
> value="%{EmpNo}"/></s:param>
> > 
> > </s:url>
> > 
> > <s:a href="%{url_fio}">
> > 
> >       <s:property
> value="%{fio}"/>
> > 
> > </s:a>
> > 
> > </div>
> > 
> > <br/>
> > 
> >  
> > 
> > <!--Adding new card-->
> > 
> >  
> > 
> > <s:if test="addCard == true">
> > 
> > <s:text
> name="card_adding_proccess"></s:text>
> > 
> > <s:fielderror/>
> > 
> > <s:form method="GET"
> validate="true">
> > 
> >       <s:textfield name="cardNo"
> >
> label="%{getText('card_add_edit_field')}"></s:textfield>
> > 
> >       <s:textfield name="EmpNo"
> value="%{EmpNo}"></s:textfield>
> > 
> >       <s:hidden name="editCard"
> value="%{true}"></s:hidden>
> > 
> >       <s:hidden name="addCardFinal"
> value="%{true}"></s:hidden>
> > 
> >       <s:submit
> value="%{getText('button_submit')}"/>
> > 
> > </s:form>
> > 
> > </s:if>
> > 
> >  
> > 
> > <!--Editing new card-->
> > 
> >  
> > 
> > <s:if test="editCard == true">
> > 
> > <s:text
> name="card_editing_proccess"></s:text>
> > 
> > <s:fielderror/>
> > 
> > <s:form method="GET"
> validate="true">
> > 
> >       <s:textfield name="cardNo"
> >
> label="%{getText('card_add_edit_field')}"></s:textfield>
> > 
> >       <s:hidden name="EmpNo"
> value="%{EmpNo}"></s:hidden>
> > 
> >       <s:hidden name="editCard"
> value="%{true}"></s:hidden>
> > 
> >       <s:hidden name="editCardFinal"
> value="%{true}"></s:hidden>
> > 
> >       <s:submit
> value="%{getText('button_submit')}"/>
> > 
> > </s:form>
> > 
> > </s:if>
> > 
> >  
> > 
> > JAVA Action:
> > 
> >  
> > 
> > public void validate() {
> > 
> >             request =
> ServletActionContext.getRequest();
> > 
> >             String tmp = "";
> > 
> >             try {
> > 
> >                   tmp =
> request.getParameter("cardNo");
> > 
> >                   Integer i =
> this.getCardNo();
> > 
> >                   if (tmp != null
> && tmp.length() > 0) {
> > 
> >                         int
> tmpInt = Integer.parseInt(tmp);
> > 
> >                        
> this.setCardNo(tmpInt);
> > 
> >                        
> System.out.println("Card no (validation) = " +
> > cardNo);
> > 
> >                         if
> (cardNo < 1 || cardNo > 999999) {
> > 
> >  
> > 
> >
>                             
> addFieldError("cardNo", "Must be int");
> > 
> >
>                              
> > 
> >
>                              
> > 
> >
>                             
> System.out.println("INPUT...");
> > 
> >                         }
> > 
> >                   }
> > 
> >             } catch (Exception e) {
> > 
> >  
> > 
> >                  
> e.printStackTrace();
> > 
> >             }
> > 
> >  
> > 
> >       }
> > 
> >  
> > 
> > Thanx a lot.
> > 
> >  
> > 
> > Best regards, Filippov Andrey
> > 
> > 
> > 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Custom-validation-with-Struts2-tp16115973p19425117.html
> Sent from the Struts - User mailing list archive at
> Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Custom validation with Struts2

Posted by "Priyanka.dandekar" <pr...@gmail.com>.
If Integer Range Validator does not work for you then you shoud consider
regular expression validation for same, its can be done by restricting
number of digits and allowing only numeric characters.

Here is an example of similar problem 
http://struts-2-developers.blogspot.com/2008/08/struts-2-integer-validation-example.html
Struts 2 Integer Validation  


fil78 wrote:
> 
> Hello everybody,
> 
>  
> 
> I need to validate only 1 field on the jsp. It value should be between 1
> and 999999. Initially I started to implement through int validation but it
> seemed to go wrong - it works with some limitation - maximum value should
> not exceed 1000 or something like that. I am trying to implement
> validation through overriding validate() method. I use tiles together with
> Struts 2.0.11. I could not tune it to work correctly. Could someone help,
> please?
> 
>  
> 
> Here are my snippets:
> 
>  
> 
>  
> 
> Struts.xml:
> 
>  
> 
>             <action name="EmployeeCard"
> 
>                   class="com.tsystems.tintra4.actions.EmployeeCard">
> 
>                   <interceptor-ref name="i18n" />
> 
>                   <interceptor-ref name="roles">
> 
>                         WTT_CARDS_EDITOR
> 
>                   </interceptor-ref>
> 
>                   <!-- interceptor-ref name="workflow"/-->
> 
>                   <interceptor-ref name="workflow"/>
> 
>                   <result type="tiles"
> name="SUCCESS">def_page_employee_card</result>
> 
>                   <result type="tiles" name="input">
> 
>                         def_page_employee_card
> 
>                   </result>
> 
>             </action>
> 
>  
> 
>  
> 
> Jsp:
> 
>  
> 
> <%@ page language="java" contentType="text/html; charset=UTF-8"
> 
>     pageEncoding="UTF-8"%>
> 
> <%@ taglib prefix="s" uri="/struts-tags" %>
> 
>  
> 
> <!--Reference to the Employee profile-->
> 
>  
> 
> <div>
> 
> <s:url id="url_fio" action="GetEmployee" includeParams="none">
> 
>       <s:param name="employeeId"><s:property value="%{EmpNo}"/></s:param>
> 
> </s:url>
> 
> <s:a href="%{url_fio}">
> 
>       <s:property value="%{fio}"/>
> 
> </s:a>
> 
> </div>
> 
> <br/>
> 
>  
> 
> <!--Adding new card-->
> 
>  
> 
> <s:if test="addCard == true">
> 
> <s:text name="card_adding_proccess"></s:text>
> 
> <s:fielderror/>
> 
> <s:form method="GET" validate="true">
> 
>       <s:textfield name="cardNo"
> label="%{getText('card_add_edit_field')}"></s:textfield>
> 
>       <s:textfield name="EmpNo" value="%{EmpNo}"></s:textfield>
> 
>       <s:hidden name="editCard" value="%{true}"></s:hidden>
> 
>       <s:hidden name="addCardFinal" value="%{true}"></s:hidden>
> 
>       <s:submit value="%{getText('button_submit')}"/>
> 
> </s:form>
> 
> </s:if>
> 
>  
> 
> <!--Editing new card-->
> 
>  
> 
> <s:if test="editCard == true">
> 
> <s:text name="card_editing_proccess"></s:text>
> 
> <s:fielderror/>
> 
> <s:form method="GET" validate="true">
> 
>       <s:textfield name="cardNo"
> label="%{getText('card_add_edit_field')}"></s:textfield>
> 
>       <s:hidden name="EmpNo" value="%{EmpNo}"></s:hidden>
> 
>       <s:hidden name="editCard" value="%{true}"></s:hidden>
> 
>       <s:hidden name="editCardFinal" value="%{true}"></s:hidden>
> 
>       <s:submit value="%{getText('button_submit')}"/>
> 
> </s:form>
> 
> </s:if>
> 
>  
> 
> JAVA Action:
> 
>  
> 
> public void validate() {
> 
>             request = ServletActionContext.getRequest();
> 
>             String tmp = "";
> 
>             try {
> 
>                   tmp = request.getParameter("cardNo");
> 
>                   Integer i = this.getCardNo();
> 
>                   if (tmp != null && tmp.length() > 0) {
> 
>                         int tmpInt = Integer.parseInt(tmp);
> 
>                         this.setCardNo(tmpInt);
> 
>                         System.out.println("Card no (validation) = " +
> cardNo);
> 
>                         if (cardNo < 1 || cardNo > 999999) {
> 
>  
> 
>                              addFieldError("cardNo", "Must be int");
> 
>                              
> 
>                              
> 
>                              System.out.println("INPUT...");
> 
>                         }
> 
>                   }
> 
>             } catch (Exception e) {
> 
>  
> 
>                   e.printStackTrace();
> 
>             }
> 
>  
> 
>       }
> 
>  
> 
> Thanx a lot.
> 
>  
> 
> Best regards, Filippov Andrey
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Custom-validation-with-Struts2-tp16115973p19425117.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org