File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes simple struts app made confusing by documentation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "simple struts app made confusing by documentation" Watch "simple struts app made confusing by documentation" New topic
Author

simple struts app made confusing by documentation

Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
hey guys...I've got a VERY SIMPLE web application I need to write and I'd like to use struts. Problem is I've read so much documentation/examples, I've gone cross-eyed. Anybody out there willing to "take me under their wing" and help me out?

Here's the issue I'm confused about....assume I have three JSP views (identify.jsp, verify.jsp, success.jsp) in my app. The first two have forms that collect data, and the third is just the "thanks" page.

assume the user is looking at identify.jsp. When they click the submit button, the IdentifyAction is ran, which processes the data submitted from the identify.jsp form. Assuming all is well, the IdentifyAction will forward to "success" which is identified in the action mapping as the next page....verify.jsp.

so now the user is looking at verify.jsp, but the URL line still reads /context/root/MyApp/identify.do

so it appears that every page you view after the start page will have a URL of the previous action, which seems very unintuitive. In the end of my example, you'd be reading a page that says, "congrats....blah blah" but the URL would still read /context/root/MyApp/Confirm.do

Am I missing something? Does that even make sense?
[ May 10, 2006: Message edited by: Jason Berk ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's an assignment for you:

Go to www.amazon.com and start browsing around for products. Go ahead and add something to your shopping cart and then remove it. After each page, look at the URL. Does the URL at any point make any sense to you, or provide you with any useful information about what is going on?

My point is this: The unspoken rule with web application is "Don't look at or worry about what's in the browser's URL, because it's information that the application needs to do its job, but won't help or inform you in any way".


Merrill
Consultant, Sima Solutions
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
well, atleast struts.apache.org is back online....DOH!

regarding the flow of a web application....

is it "wrong" to think there is a 1-to-1 mapping between a JSP and an Action?

I mean, does foo.jsp submit to fooAction which then forwards to bar.jsp (this results in a URL of foo.do while showing the bar.jsp...seems wrong)

or....

does foo.jsp submit to barAction which then forwards to bar.jsp
(this fixes the URL issue, but seems confusing because barAction would be responsible for processing foo.jsp's data)

furthermore, I have all my JSPs inside WEB-INF so they aren't publically accessible...but there's nothing (from what I understand) in struts that stops a user from putting /bar.do in the URL, and trying to access the web app "mid-stream". Am I correct in assuming I would have to code barAction to realize that fooAction never occurred and deal with it?

Merrill...I understand you amazon.com exercise, and I (in ways) agree. But you have to admit, when you are looking at your shopping cart on a website, you would find it strange that the URL was something like /editProfile.do or /showDescription.do. I agree the parameters of the URL are irrelivant but the action should be somewhat indicative of the view....make sense?

thanks for any help,

jason
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
here's some actual code....It works and seems very intuitive from a flow control point of view...but like I said before, it seems wrong that the URL for the verifyXXX page is Identify.do

am I miss understanding something?


/WebContent/MyApp/index.jsp ->
<logic:redirect action="/MyApp/CollectCustomerInfo"/>

/WEB-INF/struts/modules/MyApp/jsp/IdentifyCustomer.jsp ->
<html:form action="/IdentityCustomer" method="post">

/WEB-INF/struts/modules/MyApp/config/struts-config.xml ->
<action
path="/CollectCustomerInfo"
parameter="/WEB-INF/struts/modules/MyApp/jsp/IdentifyCustomer.jsp"
type="org.apache.struts.actions.ForwardAction" />

<action
path="/IdentityCustomer"
name="identifyForm"
scope="request"
validate="true"
type="foo.MyApp.IdentityCustomerAction">
<forward name="verifyEmpl"
path="/WEB-INF/struts/modules/MyApp/jsp/VerifyEmployee.jsp"/>
<forward name="verifyCust"
path="/WEB-INF/struts/modules/MyApp/jsp/VerifyCustomer.jsp" />
</action>
[ May 10, 2006: Message edited by: Jason Berk ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
is it "wrong" to think there is a 1-to-1 mapping between a JSP and an Action?


Actually, this is an incorrect assumption. The whole point of having a forward as a separate entity from an action is that a single action does not always forward to the same JSP. In certain situations, a single action could choose between many JSPs to forward to.

Actually, I was being a bit facetious with my amazon.com example. Naturally you will want to name things appropriatedly if only for your own benefit as the developer. I do stand by the point, though, that it's really none of the user's business what's in the URL.

Since you do want your URLs to make at least some sense, the solution is to understand that it's the action that will be in the URL, not the JSP, and name your actions accordingly. I'd suggest naming an action by what it does, rather than by what page it may or may not display.

For example, the action called by login.jsp should not be /loginSuccess or /loginFailure, but /processLogin. Granted, the URL at the top of the login success page is going to say /processLogin instead of /loginSuccess, but at least it makes some sense that what you've just done to get to this point is process the login information. If you called it /loginSuccess and it ended up forwarding to the login failure page because of improper identification, it would make no sense at all.

If you really, really, really care about what's in the URL, one solution is to do "action chaining". Using this technique, you have one action to initialize the page, and a completely separate action to process it. So, instead of foo.do forwarding to bar.jsp and having the URL still show foo.do, you would name the first action fooProcess.do and have it forward to another action (barInit.do) instead of a jsp. That way the URL will read barInit.do when bar.jsp is being displayed. This link explains how to do it. You will note, however, that the authors of the documentation recommend not doing this. It does fix your URL problem, though.

Am I correct in assuming I would have to code barAction to realize that fooAction never occurred and deal with it?


You are exactly correct in assuming this. It's a pain, huh? That's actually one of the reasons I don't want the URL to make any sense to the user. I don't want the user to mistakenly assume he/she knows what is going on in the application by what's in the URL and then bookmark the page. Bad User!

Seriously, though, most of the applications I write are intended to be accessed only through the welcome page. I understand that in other applications, it may be appropriate for users to bookmark a page or cut and paste the URL into an email. In these cases, you will have to be more concerned about what's in the URL.

Actually, you can relieve some of the tedium of having to check things in every action by creating a base Action class that handles tasks that every action has to do and then extending this class instead of the Struts Action class. For example, if every action has to check to see if the user is logged on or not, you can put this code in the base action class.
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
Not that the action-chaining section wasn't good reading, but the section immediately before the action chaining section is the "How can I create a wizard workflow?" section...which is EXACTLY what I'm trying to do.

Once again....thanks Merrill and company
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I often like to think that each jsp page has two actions: one for showing the page and one for processing the page. In the case of a "success" screen I would not have another action but many times I would, especially in the case of edit screens. For example, the flow of your application may be that the end user searches for a list of employees, clicks on an employee link to edit an individual, and then clicks the save button which will return back to the search results screen. The actions here might be:

> ProcessEmployeeSearch > DisplayEditEmployee > SaveEmployee > RefreshEmployeeSearch (redirect = true)

So after these steps the user will see "RefreshEmployeeSearch.do" in the URL. Having "SaveEmployee.do" is bad because if the user hits the refresh button it will try to save the data again. I actually think there is a pattern for this...something like "redirect after post"

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple struts app made confusing by documentation
 
Similar Threads
So, what do you all think of Struts ??
JSP unable to find Struts2 action
Session lost due to....?
branching webapp using single DynaValidatorForm
Mapping to local context root