• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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?

0
william007
Asked:
william007
  • 5
  • 3
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
fargoCommented:
good that you solve the problem!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now