I'm pretty new to struts also, but here is what I've found:
If you set validate to true in the action mapping, the validation will
run before the action. If you set it to false, you may run the validate
method of the form yourself at any time. Also, it is interesting to note
that if you forward to a .jsp, it only runs the action the first time,
successive forwards to the jsp do not run the action. In order to run
the action, you usually want to forward to the action (/do/myAction).
In my situation, I had a form which I initially wanted to fill with data
for editing, and then take the data and update the database. To do
this, I set up one action with two action mappings. The first action
mapping was to load the data and the second to take the data and update
the database. The first action mapping, I set validate to false and
the second I set validate to true. I also used the param attribute of
the action within the struts config to set these differently depending
on the action (so that my servlet could tell which action I was doing.
Here is some sample stuff:
struts-config actions:
<action path="/messageEdit"
name="messageEditForm"
type="com.xxx.purchasing.main.webapp.MessageEditAction"
scope="request"
validate="false"
input="/WEB-INF/pages/message_edit.jsp"
parameter="edit">
<forward name="success" path="WEB-INF/pages/message_edit.jsp" />
</action>
<action path="/messageUpdate"
name="messageEditForm"
type="com.xxx.purchasing.main.webapp.MessageEditAction"
scope="request"
parameter="update"
validate="true"
input="/do/messageEdit">
<forward name="success" path="/do/messageMaintenance" />
</action>
In my message_edit.jsp, I have:
<html:form method="POST" action="/messageUpdate" focus="title">
HTH and it's probably not perfect, but it works,
Mike Witt
-----Original Message-----
From: Dick Starr [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, January 22, 2002 8:51 AM
To: struts-user
Subject: getting validation errors on initial display of a form
I am new to Struts and am using Ted's sample logon app as my basis for
learning. His part works fine. I added a new jsp page that contains two
fields, created a form bean & an action form with validation edits. My
problem is that when I execute my form via
http://localhost:8080/starrd/app/names.do I get validation errors appearing
immediately (before I enter any data in the form). I can't believe thats
normal: I must be doing something wrong.
Thanks in advance for the help.
Dick Starr
Here's the pertinent parts of my struts-config:
<form-beans>
<form-bean
name="nameForm"
type="com.starrcs.app.SANameForm"/>
<global-forwards>
<forward
name="names"
path="/app/names.do"/>
<action-mappings>
<action
path="/app/names"
type="com.starrcs.app.SANameAction"
name="nameForm"
scope="request"
validate="true"
input="/pages/app/Name.jsp">
<forward
name="continue"
path="/pages/app/Name.jsp"/>
</action>
Here's my Name.jsp:
<!-- Name.jsp 2002/01/21 RJS -->
<%@ page language="java" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html><head><title><bean:message key="app.name.title"/></title></head>
<html:base/>
<body>
<html:errors/>
<html:form action="/app/names" focus="name">
<table border="0" width="100%">
<tr><th align="right">Name:</th><td align="left">
<html:text property="name"/></td>
</tr>
<tr><th align="right">Address line one:</th><td align="left">
<html:text property="addr1"/></td>
<tr>
<td align="right"><html:submit property="submit" value="Submit"/></td>
<td align="left"><html:reset/></td>
</tr>
</table>
</html:form>
</body>
</html>
<%--
Allow user to submit username and password to logon action.
--%>
Here's the pertinent parts of SANameAction.java:
public final class SANameAction extends Action
{
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
// Obtain fields from web tier
String name = ((SANameForm) form).getName();
String addr1 = ((SANameForm) form).getAddr1();
// Log this event, if appropriate
if (servlet.getDebug() >= SAConstants.DEBUG)
{
StringBuffer message =
new StringBuffer("SANameAction: name = ");
message.append(name);
message.append(", addr1 = ");
message.append(addr1);
servlet.log(message.toString());
}
// Forward control to the welcome URI
// specified in the configuration.
return (mapping.findForward(SAConstants.WELCOME));
}
} // End SANameAction
Here's the pertinent parts of SANameForm.java:
public final class SANameForm extends ActionForm
{
// ---- Instance Variables
private String name = null;
private String addr1 = null;
// ---- Properties
public String getName()
{
return (this.name);
}
public void setName(String name)
{
this.name = name;
}
public String getAddr1()
{
return (this.addr1);
}
public void setAddr1(String addr1)
{
this.addr1 = addr1;
}
// ---- Public Methods
public void reset(ActionMapping mapping,
HttpServletRequest request)
{
setName(null);
setAddr1(null);
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
if ((name == null) || (name.length() < 1))
errors.add("name",
new ActionError("app.error.name.required"));
if ((addr1 == null) || (addr1.length() < 1))
errors.add("addr1",
new ActionError("app.error.addr1.required"));
return errors;
}
} // End SANameForm
When I first execute the form via http://localhost:8080/starrd/app/names.do
I immediately (before entering any data or executing the submit button)
get validation errors displayed for name and addr1 and get the following in
my log file:
2002-01-21 20:19:19 action: Processing a GET for /app/names
2002-01-21 20:19:19 action: Looking for ActionForm bean under attribute
'nameForm'
2002-01-21 20:19:19 action: Creating new ActionForm instance of class
'com.starrcs.app.SANameForm'
2002-01-21 20:19:19 action: Storing instance under attribute 'nameForm' in
scope 'request'
2002-01-21 20:19:19 action: Populating bean properties from this request
2002-01-21 20:19:19 action: Validating input form properties
2002-01-21 20:19:19 action: Validation error(s), redirecting to:
/pages/app/Name.jsp
Then if I fill in the two fields with data, I do not get validation errors
(which is correct). Then this is what is in my log:
2002-01-21 20:19:37 action: Processing a POST for /app/names
2002-01-21 20:19:37 action: Looking for ActionForm bean under attribute
'nameForm'
2002-01-21 20:19:37 action: Creating new ActionForm instance of class
'com.starrcs.app.SANameForm'
2002-01-21 20:19:37 action: Storing instance under attribute 'nameForm' in
scope 'request'
2002-01-21 20:19:37 action: Populating bean properties from this request
2002-01-21 20:19:37 action: Validating input form properties
2002-01-21 20:19:37 action: No errors detected, accepting input
2002-01-21 20:19:37 action: Looking for Action instance for class
com.starrcs.app.SANameAction
2002-01-21 20:19:37 action: SANameAction: name = data1, addr1 = data2
2002-01-21 20:19:37 action: Processing a POST for /app/welcome
2002-01-21 20:19:37 action: Looking for Action instance for class
com.starrcs.app.SAContinueAction
--
To unsubscribe, e-mail:
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>