Re: [Struts] chaining
From: Sudsy (bitbucket44_at_hotmail.com)
Date: 11/19/03
- Next message: jerry: "Re: How to call same instance methods in different orders"
- Previous message: Brenton Fletcher: "Re: OOP Design Question"
- In reply to: virtual: "Re: [Struts] chaining"
- Next in thread: virtual: "Re: [Struts] chaining"
- Reply: virtual: "Re: [Struts] chaining"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 19 Nov 2003 17:16:04 -0500
virtual wrote:
>>Check out the DTD for the struts-config.xml file. Pay particular
>>attention to the scope attribute of the action tag. You probably
>>want to specify scope="session".
>>HTH
>
>
> Of course I had known this idea but it is not what I need.
> I don't want to keep it in session.
>
>
Well, there is another way but you've got to be "vewwy, vewwy
careful" as Elmer Fudd would say. It can bite you if you don't
manage your forwards properly.
Suppose I have the following in my struts-config.xml:
<form-beans>
<form-bean
name="EntryForm"
type="com.myorg.EntryForm" />
</form-beans>
<action-mappings>
<action
path="/entry"
type="com.myorg.EntryAction"
name="EntryForm"
scope="request"
unknown="false"
validate="false">
<forward
name="edit"
path="/WEB-INF/jsp/edit.jsp"
redirect="false" />
<forward
name="confirm"
path="/WEB-INF/jsp/confirm.jsp"
redirect="false" />
</action>
</action-mappings>
And suppose the page which is used for the initial data entry
looks like this:
<head>
<title>Data Entry Form</title>
</head>
<body>
<form action="/context/entry.do" method="POST">
<table>
<tr>
<th align="RIGHT">Enter a string</th>
<td align="LEFT"><input type="text" maxlength="30" size="30"
name="astring"/></td>
</tr>
<tr>
<td><input type="submit" name="command" value="Edit"/></td>
<td><input type="submit" name="command" value="Confirm"/></td>
</tr>
</table>
</form>
</body>
I'm also assuming that you've got the correct accessor and mutator
methods in EntryForm.
The EntryAction is very simple:
package com.myorg;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public ActionForward execute(
ActionMapping mapping,
ActionForm actionForm,
HttpServletRequest req,
HttpServletResponse resp )
throws Exception {
EntryForm form = (EntryForm) actionForm;
String cmd = form.getCommand();
if( cmd.equalsIgnoreCase( "edit" ) )
return( mapping.findForward( "edit" ) );
return( mapping.findForward( "confirm" ) );
}
}
Now here's where we apply the magic! Here's the confirm.jsp page:
<%@ taglib uri="html" prefix="html" %>
<head>
<title>Data Confirmation Form</title>
</head>
<body>
<form action="/context/confirm.do" method="POST">
<table>
<tr>
<th align="RIGHT">String</th>
<td align="LEFT"><html:text maxlength="30" size="30" property="astring"
name="EntryForm" readonly="true"/></td>
</tr>
<tr>
<td><html:submit property="command" value="Edit"/></td>
<td><html:submit property="command" value="Confirm"/></td>
</tr>
</table>
</form>
</body>
Two things worth noting:
- the readonly="true" is set (as per your outline)
- we specify name="EntryForm"
Since EntryForm is the name specified in the action (and the
form-bean of course), and we're still in request scope, we
can use that bean to populate the text field.
We can even re-use the same form for the confirm action:
<action
path="/confirm"
type="com.myorg.ConfirmAction"
name="TestForm"
scope="request"
unknown="false"
validate="false">
<forward
name="edit"
path="/WEB-INF/jsp/edit.jsp"
redirect="false" />
<forward
name="continue"
path="/WEB-INF/jsp/confirmed.jsp"
redirect="false" />
The edit.jsp page is almost identical to confirm.jsp except
without the readonly attribute. The confirmation page
(confirmed.jsp) is only slightly different as it uses a bean
tag:
<%@ taglib uri="bean" prefix="bean" %>
<head>
<title>Confirmation Page</title>
</head>
<body>
<table>
<tr>
<th align="RIGHT">Entered string:</th>
<td align="LEFT"><bean:write name="EntryForm" property="astring"/></td>
</tr>
</table>
</body>
Is this more up your alley?
- Next message: jerry: "Re: How to call same instance methods in different orders"
- Previous message: Brenton Fletcher: "Re: OOP Design Question"
- In reply to: virtual: "Re: [Struts] chaining"
- Next in thread: virtual: "Re: [Struts] chaining"
- Reply: virtual: "Re: [Struts] chaining"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|