How to show error on JSP

I am using Struct framework in building JSP, and I am very new on Struct framework.

Validation.xml
==========

      <formset>
            <form name="user">
                  <field property="userName" depends="required,minlength,maxlength">
                        <arg0 key="aaa"/>
                        <arg1 key="${var:maxlength}" name="maxlength" resource="false"/>
                        <arg1 key="${var:minlength}" name="minlength" resource="false"/>
                        <var>
                              <var-name>minlength</var-name>
                              <var-value>5</var-value>
                        </var>
                        <var>
                              <var-name>maxlength</var-name>
                              <var-value>11</var-value>
                        </var>
                  </field>
            </form>
      </formset>

struts-config.xml
===========
<struts-config>
      <form-beans>
            <form-bean name="userRegistrationForm" type="strutsTutorial.UserRegistrationForm"/>
      </form-beans>
      <action-mappings>
            <action path="/userRegistration" type="strutsTutorial.UserRegistrationAction" name="userRegistrationForm" attribute="user" input="/userRegistration.jsp">
                  <forward name="success" path="/regSuccess.jsp"/>
                  <forward name="failure" path="/regFailure.jsp"/>
            </action>
      </action-mappings>
      <message-resources parameter="MessageResources.properties"/>
      <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>

My jsp page is like this
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<html:form action="userRegistration">
<html:errors/>
<table>
<tr>
<td>
<html:text property="userName"/>
<html:submit/>
</td>
</tr>
</table>
</html:form>
</body>
</html>

But I don't get the error display on the jsp page after the user click on submit button with blank textbox,
may I know how to correct it?

LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
fargoCommented:
u r missing validate=true in your action mapping

<action path="/userRegistration" type="strutsTutorial.UserRegistrationAction" name="userRegistrationForm" attribute="user"
validate="true" // missing one
input="/userRegistration.jsp">
               <forward name="success" path="/regSuccess.jsp"/>
               <forward name="failure" path="/regFailure.jsp"/>
          </action>
0
 
william007Author Commented:
Hi, I just put it, but still no luck.

Do we need to do any thing in the ValidationForm in order to display the error message at <html:errors/>? Or everything is done automatically as long as we set the validation.xml correctly?
0
 
fargoCommented:
validation.xml should be correct. As far as i see, you are not using the resources file to display the message..moreover,
the keys are duplicate

<arg0 key="aaa"/> // what is this for?
<arg1 key="${var:maxlength}" name="maxlength" resource="false"/>
should be arg2 <arg2 key="${var:minlength}" name="minlength" resource="false"/>


0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
fargoCommented:
the action form should extend org.apache.struts.validator.ValidatorActionForm
0
 
william007Author Commented:
I have changed that, but still don't see the error to be appeared.
Followed is my Classes

UserRegistrationForm.java
=================
package strutsTutorial;

import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.ValidatorActionForm;

public class UserRegistrationForm extends ValidatorActionForm {

      /**
       *
       */
      private static final long serialVersionUID = 1L;

      private String userName;

      public String getUserName() {
            return userName;
      }

      public void setUserName(String string) {
            try {
                  
                  FileOutputStream fout = new FileOutputStream("c:\\log.txt");

                  // Print a line of text
                  
                  new PrintStream(fout).println("hello world!");

                  // Close our output stream
                  fout.close();
            } catch (Exception e) {

            }

            userName = string;
      }
}


UserRegistrationAction.java
=================
package strutsTutorial;

import org.apache.struts.action.Action;

public class UserRegistrationAction extends Action {
//Nothing for the moment      

}
0
 
fargoCommented:
the depends property is case sensitive... u should use
<field property="userName" depends="required,minLength,maxLength">
                    <arg0 key="${var:maxLength}" name="maxlength" resource="false"/>
                    <arg1 key="${var:minLength}" name="minlength" resource="false"/>
                    <var>
                         <var-name>minLength</var-name>
                         <var-value>5</var-value>
                    </var>
                    <var>
                         <var-name>maxLength</var-name>
                         <var-value>11</var-value>
                    </var>
               </field>
0
 
william007Author Commented:
Thanks, fargo.
Actually all the things is correct, other then
<message-resources parameter="MessageResources.properties" />
I should change it to
<message-resources parameter="MessageResources" />
as we do not need to include the properties extension.

Let me elaborate one by one
>>u r missing validate=true in your action mapping
I think it is by default, as I delete it still ok

>><arg1 key="${var:maxlength}" name="maxlength" resource="false"/>
>>should be arg2 <arg2 key="${var:minlength}" name="minlength" resource="false"/>
Actually nothing wrong here, since in the resource file
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.

>>org.apache.struts.validator.ValidatorActionForm
http://www.mail-archive.com/struts-user@jakarta.apache.org/msg96980.html

Very appreciate for your effort:-)

But since I have answered it myself, I would like to PAQ this question.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
fargoCommented:
good that you solve the problem!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.