Solved

Struts: How to handle server side validation when using dynamic forms!!!

Posted on 2006-07-13
6
2,333 Views
Last Modified: 2013-11-24
Hi! I am using struts 1.2 and is new to struts. Since I am using dynamic forms how do I do server side validation? I mean, I want to authenticate username and password from my form against the database. ( The client side validation is done with the validator framework. ) Should I do that in my Action class? And, if I do the authentication in the action class how do I set the action error object if there is an error to come back to the form and print it. Note: I am using hibernate as my ORM tool. The other question is: why is my client side validation not working?

here is my jsp:
-------------------------------------------------------------------------------------------------------
<head>
    <html:base />
       <html:javascript formName="loginFormBean" dynamicJavascript="true"
               staticJavascript="false"/>
    <title>LoginHome.jsp</title>
  </head>
<html:form action="/login" method="post" onsubmit="return validateLoginFormBean(this);">
      <table border="0">
        <tr>
          <td>Login:</td>
          <td><html:text property="username" size="15" maxlength="30"/></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><html:text property="password"/></td>
        </tr>
        <tr>
          <td colspan="2" align="center">
            <html:submit>Go</html:submit>  
          </td>
        </tr>
        <tr><html:errors/></tr>
      </table>
    </html:form>
--------------------------------------------------------------------------------------------------

Here is the LoginAction.java class

public class LoginAction extends Action {
    // --------------------------------------------------------- Instance Variables
    protected final Log logger = LogFactory.getLog(getClass());
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response) {
            DynaActionForm loginForm = (DynaActionForm) form;
            // TODO Auto-generated method stub
            logger.info("Forwarding from login action class");
            return mapping.findForward("success");
      }
}

--------------------------------------------------------------------------------------------

Here are the relevant mappings in struts-config:

 <form-beans>
      <form-bean name="loginFormBean"  type="org.apache.struts.validator.DynaValidatorActionForm">
      <form-property name="username" type="java.lang.String"/>
      <form-property name="password" type="java.lang.String"/>
    </form-bean>
..........
.........
<action
      attribute=""
      input="/jsp/LoginHome.jsp"
      name="loginFormBean"
      path="/login"
      scope="request"
      validate="true"
      type="com.ims.action.LoginAction">
      <forward name="success" path="/jsp/showmenu.jsp"/>
    </action>
.......

--------------------------------------------------------------------------------------------------------

Here is my validation.xml:

<formset>
 <form name="loginFormBean">
    <field property="username" depends="required, minlength">
       <arg key="loginFormBean.username"/>
       <var>
          <var-name>minlength</var-name>
          <var-value>10</var-value>
        </var>
    </field>
    <field property="password" depends="required,mask,minlength">
         <arg key="loginFormBean.password"/>
         <var>
            <var-name>mask</var-name>
            <var-value>^[0-9a-zA-Z]*$</var-value>
        </var>
        <var>
          <var-name>minlength</var-name>
          <var-value>10</var-value>
        </var>
    </field>
  </form>
  </formset>
--------------------------------------------------------------------------------------------------------

1) how do I set the action error object in my loginAction class if there is an error with authentication?

2) Some how the client side validation is trigerring, without any error. I mean even if I do enter anything in the textbox and click submit, I am being forwarded to the next page (showmenu.jsp)


I think I am missing something small for my second problem........
subirc
0
Comment
Question by:subirc
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:anumalas
Comment Utility
In form beans you are doing wrong

Inseted of <form-bean name="loginFormBean"  type="org.apache.struts.validator.DynaValidatorActionForm"> write

<form-bean name="loginFormBean"  type="org.apache.struts.validator.DynaValidatorForm">

For further reference go thru follwinglink

www.javapassion.com/j2ee/ StrutsValidationFramework_speakernoted.pdf

Hope this will solve your problem :)

 
0
 

Author Comment

by:subirc
Comment Utility
No still not working!!!  When I am clicking submit (without entering anything in username + password) a error pops up saying

error: _required is undefined
Somewhere that underscore is coming....

subirc
0
 
LVL 3

Expert Comment

by:anumalas
Comment Utility


Can you post complete error trace.. so we can go thru easily..
0
 

Author Comment

by:subirc
Comment Utility
I think I have solved the problem another way. I am writing all my client side validation in the Action Class itself using Action Messages. The server side is also done there. There is no error trace unfortunately. It's that when I was clicking on submit it was forwarding to the success page without doing any validation. You already have seen my validation.xml and struts-config. How do I get back my points?

subirc
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
Comment Utility
Closed, 40 points refunded.
Netminder
Site Admin
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now