aspose file tools*
The moose likes Struts and the fly likes struts modules with JSPs in WEB-INF and public redirects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts modules with JSPs in WEB-INF and public redirects" Watch "struts modules with JSPs in WEB-INF and public redirects" New topic
Author

struts modules with JSPs in WEB-INF and public redirects

Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
here at work I have a "situation". We have one WAR with multiple "applications". We would like to start using struts for new projects, so I'd like to set up the action servlet using modules. Each new "application" would be a module. Business rules require us to keep our JSPs behind WEB-INF so they aren't made public, with the exception of an "index.jsp" located in WebContent which can only contain a redirect or forward to an Action, generally named "AppName.do"

Here's the flow I'm looking to achive:
(context root = "apps")

1: user browses to "/apps/FOOApp/index.jsp"
2: index.jsp redirects/forwards users to "FOOApp.do"
3: FOOApp.do will generally be a ForwardAction
to "WebContent/WEB-INF/FOOApp/jsp/startPage.jsp"

My questions are as follows:
1: what should be defined in the default and module specific struts config files?
2: what is the syntax of the redirect/forward for index.jsp
3: what does the user's URL look like when they are at startPage.jsp...does it contain the module name (just curious)?

I've seen tons of examples showing html LINKS that move between modules, but nothing using redirects.

thanks for any advice,

Jason Berk
AIM / YahooIM: skydive814
Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186
hi,

If you want to redirect user from Index.jsp to "foo.do" then create a action called "foo" with some logic that u want to use on coming (next) pages. Then Write following line into ur index.html
<html:link action="foo" />
Then add forward to this action class to redirect/forward user to "Startpage.jsp".
Here in ur Strutsconfig.xml file will look like as follow:-
===============
//general headers.......
<action path="/foo" type="com.pal.struts.action.FooAction">
<forward name="path1" path="jsp/startPage.jsp" />
</action>
================

This is the simplest entry into ur Strustconfig.xml.
Now if u want that ur FooAction class should do more tasks for you then you should write additional entries (input, attribute, name etc) for <action> tag inside Strutsconfig.xml. Also u need to write all logic inside ur Action class(here inside Fooaction.java)

Thats all.


********Deserve Before You Desire********
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
did you even read my post?

I dont' want a link...I want a

<logic:redirect .../>

so the user is automagically sent to the action, which I DO NOT need to write, because it should be a ForwardAction provided by struts. How do I create a JSP (that's not in a module) with an auto redirect to a ForwardAction which will forward to a JSP in a module hidden inside WEB-INF?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
1: what should be defined in the default and module specific struts config files?

If you expect the user to know the url to an individual app, you really don't need much of anything in the default struts-config.xml file except possibly an action definition and a JSP that are used just in case the user enters http://www.yourcompany.com/apps/ without any other parameters. Everything else should be in the module config files.
2: what is the syntax of the redirect/forward for index.jsp
3: what does the user's URL look like when they are at startPage.jsp...does it contain the module name (just curious)?

<logic:redirect action="/FOOApp" />
or
<logic:forward action="/FOOApp" />

Because the user has already requested a page in the FOOApp mudule, Struts will automatically assume that the action in the <logic:redirect> tag or any other tag is referring to the FOOApp module. The FOOApp module will continue to be the one Struts looks at as long as no links or redirects refer to a URL in another module.

A redirect will change the URL to "/apps/FOOApp/FooApp.do", whereas a forward will leave it as "/apps/FOOApp/index.jsp".

So, obiously the answer to question 3 is yes, it does continue to show the module name in the URL.

The struts-examples.war file that comes with the Struts download is an example of a web application divided into modules. You can learn a lot about how modules work by running this application and studying the code.
[ May 03, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
thanks for the pointers....

after reading: http://www.javaworld.com/javaworld/jw-09-2004/jw-0913-struts-p3.html

specifically the "Context-related issues", I think I need to updated the "ActionForward" property of the controller tag. I'm using WSAD5.1.2 and my struts-app.xml is checked against: http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd and using the 1.2.9 struts jar.

for some reason, I can't get WSAD to let me add the <controller> tag. any ideas?

Jason
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm using RAD V6.0, but I believe WSAD 5.1.2 is similar. Just use the Struts Configuration File GUI editor and click on the "Controller" tab. You'll see the text field for "Forward Pattern:".

Be aware that while you can use later versions of Struts, the tooling for WSAD is all geared toward Struts 1.1. This doesn't prevent the application from working, but the tools will sometimes give broken link errors and other errors incorrectly.

While using a forward pattern is a convenience, you don't really need it to resolve a context issue. As long as none of your forwards specify redirect="true" and as long as you only access JSP pages through a forward, your context will remain in /apps/FOOApp.

P.S. I edited my last post since you replied, so you may want to read it again.
[ May 03, 2006: Message edited by: Merrill Higginson ]
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
maybe my previous post was confusing. I have an HTML file with links to the following locations:

/apps/Apple
/apps/Orange
/apps/Berry

those locations exist in WebContent of my WAR...ie:
WebContent/Apple/index.jsp
WebContent/Orange/index.jsp
WebContent/Berry/index.jsp

I would like all those index.jsp pages to redirect to the following Actions, respectively...
Apple.do
Orange.do
Berry.do

my first question is: are the above actions declared as action mappings in the default config file, or in struts-apple.xml, strut-orange.xml and strut-berry.xml? (note, all my config files are in WEB-INF)

Now, I would like the above mentioned action mappings to be strut's ForwardActions which will forward the user to a JSP page behind WEB-INF using (I think) a syntax like so:

<action
path="/Apple.do"
type="org.apache.struts.actions.ForwardAction"
parameter="WEB-INF/Apple/firstPage.jsp"/>

<action
path="/Orange.do"
type="org.apache.struts.actions.ForwardAction"
parameter="WEB-INF/Orange/firstPage.jsp"/>

<action
path="/Berry.do"
type="org.apache.struts.actions.ForwardAction"
parameter="WEB-INF/Berry/firstPage.jsp"/>

If I understand everything I've read...
the user can browse to
"http://server/apps/Apple"
and his URL will then change to
"http://server/apps/Apple/Apple.do"
and he'll be looking at the contents of:
"WEB-INF/Apple/firstPage.jsp"

Right now, I'm getting a 404:
"Error 404: /Apple/WEB-INF/Apple/firstPage.jsp"

as I mentioned in the previous post, I think the controller is hosing me by adding the module ("/Apple") to the front of the parameter value listed in the action.

reading the following at http://www.javaworld.com/javaworld/jw-09-2004/jw-0913-struts-p3.html

snip....
struts-config.xml(s) has a controller tag; add a property in this tag named forwardPattern. This property's value can be prefixed to all the ActionForward paths. This property's default value is $M$P, which means the module prefix will be prefixed to ActionForward's path. We can change this value to anything; e.g., WEB-INF/pages/$M$P. As a result, all the ActionForward paths will be searched in the directory WEB-INF/pages/<module-prefix>/.
....snip

leads me to believe I need to change the forwardPattern from its default of $M$P to "WEB-INF/$M".

Is what I'm trying to do _that_ out of the ordinary?

On a side note, I find it strange that every single "best practice" article I've read has listed using modules and hiding your JSPs behind WEB-INF but I couldn't find a single example of doing both anywhere online. Every example uses either one or the other....

thanks!

Jason (AIM: skydive814)
Jason Berk
Ranch Hand

Joined: May 03, 2006
Posts: 41
SUCCESS!

I got it working....Thanks for the help Merrill, it pointed me in the correct direction and I now have things working as desired.

My problem was so simple I was looking right past it.

instead of
<logic:redirect action="/Apple" />

I needed to do this:
<logic:redirect action="/Apple/Apple" />

so the controller can find the Apple action in the Apple module config...doh!

I guess I can pick some different names so things don't look so "weird" in the IDE, but at any rate, I can now protect the JSPs while allowing module development.

once again...thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: struts modules with JSPs in WEB-INF and public redirects