[newbie] Help: Struts, html:errors

R

Russ Perry Jr

Got another odd thing going on with our struts application...
Validation seems to be a little screwy. Right now I'm working
with our login.jsp, which is pretty basic.

We have a LoginActionForm that extends ValidatorForm. We
have our messages in ApplicationResources.properties and our
form/field info in validation.xml. But, for some reason
<html:errors /> is not outputting anything. However, where
I've put <logic:messagesPresent ...>, the messages DO show up
properly.

Initially, we had validate() commented out since reading that
ValidatorForm provides that (and reset()), but I put it back
with this format:

public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
ActionErrors errors = super.validate(actionMapping,
httpServletRequest);

if (errors == null)
{
errors = new ActionErrors();
}

return errors;
}

....based on some sample code I found. Neither way makes a difference.
A little debugging shows that it is definitely going in here, and
moreso, errors != null so the super.validate() method appears to be
working fine (as the logic:messagesPresent tag would seem to verify).

Does html:errors NOT work with ValidatorForm, or is there maybe
something else I should be checking?

The other odd thing is that if I include <html:javascript
formName="LoginActionForm">, the script is included, and we get the
popup (windows.alert()) box, but it's blank! I don't know if that's
a clue, or something else, but figured I'd mention it.

Anyway, again, if anyone can offer some suggestions, I'd sure
appreciate it!
 
S

Sudsy

Russ Perry Jr wrote:
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
ActionErrors errors = super.validate(actionMapping,
httpServletRequest);

if (errors == null)
{
errors = new ActionErrors();
}

return errors;
}

There's a difference between returning null (indicating no errors)
and returning an empty ActionErrors. For some examples please see:
<http://www.sudsy.net/technology/validation.html>
 
R

Russ Perry Jr

Sudsy said:
There's a difference between returning null (indicating no errors)
and returning an empty ActionErrors. For some examples please see:
<http://www.sudsy.net/technology/validation.html>

Thanks again for the reply!

However, I didn't see on that page what the distinction is... How
does it act differently between null or empty? I notice that the
Javadoc for ValidatorPlugin says that validate() will either "return
null or an ActionErrors object with no recorded error messages" but
doesn't mention that the distinction is important, let alone how.

Also, is that related to this line in the struts-config.xml?:
<message-resources parameter="ApplicationResources" null="false"/>
It does seem to be relevant to the Javascript issue I mentioned I
was having... Without the "null" bit (which is how we had it) the
Javascript that gets included looks like this (note that the second
parameter is empty):

function required () {
this.aa = new Array("loginID", "", new Function ("varName" , "
return this[varName];"));
this.ab = new Array("password", "", new Function ("varName" , "
return this[varName];"));
}

If I change it to be null="true", then it fills it in like this:

function required () {
this.aa = new Array("loginID", "???en_US.errors.required???", new
Function ("varName", " return this[varName];"));
this.ab = new Array("password", "???en_US.errors.required???",
new Function ("varName", " return this[varName];"));
}

This makes it look to me like it can't find our ApplicationResources
file, or it's finding expecting one for a different Locale. We're
not doing any i18n right now, so I figure it should pick up the
default one, and it fact it was a few days ago.

Perhaps there's something mismatching from our ApplicationResources
file (trimmed):

errors.required={0} is required.
login.loginID=LoginID
login.password=Password

....class (trimmed):

public class LoginActionForm extends ValidatorForm
// all get/set methods present, but no validate() & reset()

....validation.xml (trimmed):

<form name="LoginActionForm">
<field property="loginID" depends="required">
<arg0 key="login.loginID"/>
</field>
<field property="password" depends="required">
<arg0 key="login.password"/>
</field>
</form>

....struts-config.xml (trimmed):

<action input="/login.jsp" name="LoginActionForm"
path="/loginAction" scope="session" type="monitoronline.LoginAction"
validate="true">
<forward name="success" path="/announcements.jsp" />
<forward name="failure" path="/error.jsp" />
</action>
[...]
<message-resources parameter="monitoronline.ApplicationResources"
null="false" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>

....or JSP (trimmed):

<html:javascript formName="LoginActionForm"/>

<html:form action="/loginAction" onsubmit="return
validateLoginActionForm(this)">

<html:text property="loginID" size="20" />
<logic:messagesPresent name="org.apache.struts.action.ERROR"
property="loginID">
<font color="red">Required!</font>
</logic:messagesPresent>

<input type="password" size="20" name="password" /> <%-- NOTE: don't
change to html:password, as puts password in HTML! --%>
<logic:messagesPresent name="org.apache.struts.action.ERROR"
property="password">
<font color="red">Required!</font>
</logic:messagesPresent>
</html:form>

(BTW, we have "input" instead of "html:password" just so the password
doesn't show up in plaintext in the HTML; it doesn't make a difference
with the problem we're having).

Anything obviously wrong there?
 
S

Sudsy

Russ Perry Jr wrote:
Thanks again for the reply!

However, I didn't see on that page what the distinction is... How
does it act differently between null or empty? I notice that the
Javadoc for ValidatorPlugin says that validate() will either "return
null or an ActionErrors object with no recorded error messages" but
doesn't mention that the distinction is important, let alone how.

---> From the javadocs for ActionForm:

When the properties of this bean have been populated, but before the
execute method of the Action is called, this bean's validate method will
be called, which gives the bean a chance to verify that the properties
submitted by the user are correct and valid. If this method finds
problems, it returns an error messages object that encapsulates those
problems, and the controller servlet will return control to the
corresponding input form. Otherwise, the validate method returns null,
indicating that everything is acceptable and the corresponding
Action.execute method should be called.

---> From the javadocs for the html:errors tag:

errors - Conditionally display a set of accumulated error messages.

Displays a set of error messages prepared by a business logic component
and stored as an ActionErrors object, a String, or a String array in
request scope. If such a bean is not found, nothing will be rendered.


I will address your additional queries once I've had a chance to sit
down and digest them (org.apache.commons.digester) completely.
 
S

Sudsy

Russ said:
If I change it to be null="true", then it fills it in like this:

function required () {
this.aa = new Array("loginID", "???en_US.errors.required???", new
Function ("varName", " return this[varName];"));
this.ab = new Array("password", "???en_US.errors.required???",
new Function ("varName", " return this[varName];"));
}

Ah, so you need this:
<http://www.sudsy.net/technology/struts-messageresources.html>

<message-resources parameter="monitoronline.ApplicationResources"
null="false" />

So you need a file named
WEB-INF/classes/monitoronline/ApplicationResources.properties
Is it there? Or is it in WEB-INF/classes?

<input type="password" size="20" name="password" /> <%-- NOTE: don't
change to html:password, as puts password in HTML! --%>

Ever look at the HTML generated by said:
<logic:messagesPresent name="org.apache.struts.action.ERROR"

Should be:
<logic:messages property="org.apache.struts.action.GLOBAL_ERROR"
id="password">

Read the articles and feel free to reply off-list. Don't want to
burden everyone else with the iterations, eh? You're more than
welcome to post the ultimate solution, however.
Fair enough?
 
R

Ryan Stewart

Sudsy said:
Russ Perry Jr wrote:


There's a difference between returning null (indicating no errors)
and returning an empty ActionErrors. For some examples please see:
<http://www.sudsy.net/technology/validation.html>
Gotta disagree with you on this one unless you can provide a good
explanation. From the docs for ActionForm.validate():
"If no errors are found, return null or an ActionErrors object with no
recorded error messages."

How do you consider there to be a difference between null and an empty
ActionErrors object?
 
R

Ryan Stewart

Ryan Stewart said:
Gotta disagree with you on this one unless you can provide a good
explanation. From the docs for ActionForm.validate():
"If no errors are found, return null or an ActionErrors object with no
recorded error messages."

How do you consider there to be a difference between null and an empty
ActionErrors object?
Woops, should've read the whole thread before I posted that :) Still, I
don't consider there to be a difference between null and an empty
ActionErrors. The docs say one thing one place and something else other
places. In my experience, both methods work. Have you noticed a difference?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top