Hi,
I am new to Struts2. It seems to provide some of the functionality that I
want in MVC architecture. But I am having difficulties configuring it to
work the way I want. I sure would appreciate some assistance.
OK, lets start at the top. I want a single action class for all URLs
because the various page information is stored in a database. So, the
database contains several rows for each URL. For example, lets say I have
two pages:
/home
/info
So, depending on the URL you went to, you will get a page that contains the
database rows for the specified URL. But if you go to an undefined URL (for
example: /fred or /info/home), you will be shown the default, /home.
First Question: How do I get the URL in a struts2 action? when using
servlets, I would just grab it from the request object.
Since I want it to work with all URLs, I have configured the action like
this (struts.xml):
<action name="*" class="com.example.actions.MainSite">
<result name="SUCCESS">/MainSite.jsp</result>
</action>
Since I want to use the URL, and I don't want to add ".action" (or any other
extension), I have configured a blank action extension (struts.xml):
<constant name="struts.action.extension" value=""/>
Herein lies a problem... the MainSite.jsp contains an image
(/images/logo.gif). Since struts2 is now grabbing control for all requests,
it is processing the request for /images/logo.gif as an action instead of
sending the file.
So, I see several possible solutions, but not sure which is the best, or
even if it is possible:
1. Configure it properly.
2. Create an interceptor to process it before it gets to the action.
3. Process the request in the action and respond with a new result name
that results in the file being processed.
Did I miss any other choices?
1. Hopefully, there is a simple configuration option that I missed. After
all, it worked before I attempted to catch ALL URLS without extensions.
(BTW, it works with either extensions = "*" or action name="*", but not when
both are set)
2. I haven't done an interceptor before, but it seems logical that the
interceptor is what I want to do... is there one that has already been
provided for this purpose?
3. This is a simple fix, I guess. I would just look at the url and
determine that it is an image (or a request for /images/....) and configure
the result to process the resulting file:
<result name="FILE">${filename}</result>
Thanks for your time and thanks in advance for your effort to help me find a
solution.
Phil