You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Suriyanarayanan, Senthil Kumar" <se...@capitalone.com> on 2001/03/23 22:40:54 UTC

How to use html:errors ?

How do you make the errors shows up on the generated page.? Here is the code
snapshot..

LogonForm.java
public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request)
	{
		if(password == null || !password.equals("password"))
		{
			ActionErrors aes = new ActionErrors();
			ActionError ae = new ActionError("Please type the
valid password");
			aes.add("?", ae);
			return aes;
		}
		return null;
	}
What should I put at the place of '?' in the above add method 

struts-config.xml

	<form-beans>
	    <form-bean name="logonForm" type="package.LogonForm"/>
	</form-beans>


  <action-mappings>
    <!-- Logon Action -->
    <action    path="/logon"
               type="package.LogonAction"
               scope="request"
               name="logonForm"
               validate="true"
               input="/logon.jsp">
               <forward name="successpage" path="/success.jsp" />
    </action>
  </action-mappings>


logon.jsp

<html:errors/>
<html:form action="logon.do">
	UserId: <html:text property="userId" size="15" maxlength="15"
redisplay="false" />
	Password: <html:password property="password" size="16"
maxlength="16" redisplay="false" />
	<html:submit/>
</html:form>

I saw one of the postings talking about saveErrors method. It belongs to
Action Object, and I how do
I get reference to action object from the validate method?

Thanks in advance,
Senthil Kumar.S
 
**************************************************************************
The Information transmitted herewith is sensitive information intended only
for use to the individual or entity to which it is addressed. If the reader
of this message is not the intended recipient, you are hereby notified that
any review, retransmission, dissemination, distribution, copying or other
use of, or taking of any action in reliance upon, this information is
strictly prohibited. If you have received this communication in error,
please contact the sender and delete the material from your computer.

Re: How to use html:errors ?

Posted by Ted Husted <hu...@apache.org>.
Since Struts is a framework, there a number of inter-related parts that
work together, the error handling is a good example of that. 

When you detect an error in your Action, you need a way to display it to
the user. Since most Actions are not designed to be "views", this is a
problem. 

Struts solves this problem using four (count'em 4) components, the
ActionErrors class, the message resource, the SaveError method, and,
finally, the html:errors tag.

The ActionErrors class is a way to "log" an error. Rather have you embed
a string, the ActionErrors class lets you insert a message resource key.
This is the same resource that bean:message tag uses to provide
internationalization. The SaveError method puts your ActionError class
under a reserved key in the request context, where the html:error tag
can find it. 

In your Action, you would usually have code snippets that look like
this:

// grab a reference to the application resource collection
MessageResources messages = getResources(); 

// create a error message buffer
ActionErrors errors = new ActionErrors();

// process action 
// detect error

if (detect_error) {
    errors.add(ActionErrors.GLOBAL_ERROR,new
ActionError("error.detected"));
}

// On error, return to input form
if (!errors.empty()) {
    saveErrors(request, errors); // put the error(s) where the
htlm:error tag can find them
     return (new ActionForward(mapping.getInput())); // go home
}

If the error concerns a particular field on a form, you might also have
if (field_error) {
    errors.add("field name",new ActionError("error.fieldname"));
}

An important thing to realize is that "error.fieldname" is not what the
tag will display. The html:error tag assumes that the strings are the
name of a key in your application's message resource, which might look
like this 

; application.properties
errors.header = <ul>
error.fieldname = <li>Password required</li>
error.detected = <li>Database not available</li>
error.username = <li>That username was not found</li>
errors.footer =</ul>

For more complex needs, ActionErrors can also use "replacement values"
to build customized messages, or to build messages from stock phrases. 

For more on these classes, see

< http://jakarta.apache.org/struts/api  > 

"Suriyanarayanan, Senthil Kumar" wrote:
> 
> How do you make the errors shows up on the generated page.? Here is the code
> snapshot..
> 
> LogonForm.java
> public ActionErrors validate(ActionMapping mapping, HttpServletRequest
> request)
>         {
>                 if(password == null || !password.equals("password"))
>                 {
>                         ActionErrors aes = new ActionErrors();
>                         ActionError ae = new ActionError("Please type the
> valid password");
>                         aes.add("?", ae);
>                         return aes;
>                 }
>                 return null;
>         }
> What should I put at the place of '?' in the above add method
> 
> struts-config.xml
> 
>         <form-beans>
>             <form-bean name="logonForm" type="package.LogonForm"/>
>         </form-beans>
> 
>   <action-mappings>
>     <!-- Logon Action -->
>     <action    path="/logon"
>                type="package.LogonAction"
>                scope="request"
>                name="logonForm"
>                validate="true"
>                input="/logon.jsp">
>                <forward name="successpage" path="/success.jsp" />
>     </action>
>   </action-mappings>
> 
> logon.jsp
> 
> <html:errors/>
> <html:form action="logon.do">
>         UserId: <html:text property="userId" size="15" maxlength="15"
> redisplay="false" />
>         Password: <html:password property="password" size="16"
> maxlength="16" redisplay="false" />
>         <html:submit/>
> </html:form>
> 
> I saw one of the postings talking about saveErrors method. It belongs to
> Action Object, and I how do
> I get reference to action object from the validate method?
> 
> Thanks in advance,
> Senthil Kumar.S