struts tiles problem retrieving action called

T

Thomas Lutz

hi everybody,
thanks in advance for all help.

i have set up a web application using struts and tiles.

i copied the change locale class from the struts example app
validator, and got it running.

my problem is:
i'd like to return to the page, better, the action, where i clicked
the link "switch to english/germant/whatever".
and i am not able to determine in my "tiled" jsps which action was
called.

example:
1.call http://localhost:8080/_core/displaytag.test
2.struts maps and takes me to the tile
3.request.getRequestURI says i am in /_core/templates/main.jsp and not
in /_core/displaytag.simple.do

how do i get the info "displaytag.simple.do" ?

thank you,
tom lutz

p.s.: config files following:

<struts-config>
<form-beans>
<!-- Locale form bean -->
<form-bean name="localeForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="language" type="java.lang.String" />
<form-property name="country" type="java.lang.String" />
<form-property name="page" type="java.lang.String" />
</form-bean>
</form-beans>

<!-- =========================================== Global Forward
Definitions -->

<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward name="welcome" path="/Welcome.do" />
</global-forwards>

<!-- =========================================== Action Mapping
Definitions -->

<action-mappings>
<!-- Default "Welcome" action -->
<!-- Forwards to Welcome -->
<action path="/Welcome" forward=".aim.core.welcome" />

<!-- test forwards -->
<action path="/displaytag.simple"
type="org.apache.struts.actions.ForwardAction"
parameter=".aim.core.test.displaytag.simple" />


<!-- Locale Action -->
<action path="/locale"
type="com.aimfinsysag.web.broker.LocaleAction" name="localeForm"
scope="request">
<forward name="success" path=".aim.core.welcome" />
</action>
</action-mappings>


<!-- ===================================== Controller Configuration
-->

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"
/>

<!-- ================================ Message Resources Definitions
-->

<message-resources parameter="resources.application" />
<message-resources parameter="resources.menu" key="resources.menu"
/>


<!-- ======================================= Plug Ins Configuration
-->

<!-- ========== Tiles plugin =================== -->
<!-- -->
<!--
-->
<!-- comment following if struts1.0.x -->
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
<set-property property="definitions-parser-validate" value="true" />
</plug-in>
<!-- end comment if struts1.0.x -->

<!-- ========== Struts-Menu Plugin ============= -->
<!-- -->
<plug-in className="net.sf.navigator.menu.MenuPlugIn">
<set-property property="menuConfig" value="/WEB-INF/menu-config.xml"
/>
</plug-in>

</struts-config>

<tiles-definitions>

<!-- ======================================================= -->
<!-- Master definition -->
<!-- ======================================================= -->

<!-- Doc index page description -->
<definition name=".aim.core.main" path="/templates/main.jsp">
</definition>

<!-- ======================================================= -->
<!-- Main page body definitions -->
<!-- ======================================================= -->

<definition name=".aim.core.welcome" extends=".aim.core.main">
<put name="content" value="/templates/welcome.jsp" />
</definition>

<definition name=".aim.core.test.displaytag.simple"
extends=".aim.core.main">
<put name="content" value="/templates/displaytag/simple.jsp" />
</definition>

</tiles-definitions>
 
R

Ryan Stewart

Thomas Lutz said:
hi everybody,
thanks in advance for all help.

i have set up a web application using struts and tiles.

i copied the change locale class from the struts example app
validator, and got it running.

my problem is:
i'd like to return to the page, better, the action, where i clicked
the link "switch to english/germant/whatever".
and i am not able to determine in my "tiled" jsps which action was
called.

example:
1.call http://localhost:8080/_core/displaytag.test
2.struts maps and takes me to the tile
3.request.getRequestURI says i am in /_core/templates/main.jsp and not
in /_core/displaytag.simple.do

how do i get the info "displaytag.simple.do" ?
[...]
I'm working on the best way to solve this problem right now. The problem is that
upon forwarding, a request may be (and often is) wrapped with an
HttpSerlvetRequestWrapper, and this wrapper is set to return a different
requestURI from what was actually sent. One possibility is the
request.getRequestURL method. It "reconstructs the URL the client used to make
the request" according to the docs. This will be your original URL, which was
requested by your browser, including the protocol, but not including any query
string. The query string can be had via request.getQueryString(). Using the URL
provided by this and some other methods of the request object, you can do some
string manipulation to get rid of the parts you don't want, like the protocol,
host, and port if it's there, and even the context name if you want. All these
can be retrieved from the request object, compared to, and stripped out of the
URL. This was the first thing I tried. It seems to work on initial testing, but
just feels shaky. I'm not confident in it. After more research, I came up with
something else. Forwarding is done with the RequestDispatcher.forward(request,
response) method. According to the servlet specification, the arguments passed
to this method must be either the original request and response or may be an
HttpServletRequestWrapper and HttpServletResponseWrapper. If it is a wrapper, it
*must* wrap the original request and response objects. So assuming your
container complies with the spec, you original request is still there
*somewhere*, no matter how many times you forward. HttpServletRequestWrapper has
a getRequest method, which returns the request that is being wrapped. Therefore
instead of doing a straight request.getRequestURI(), do something like this:
HttpServletRequest tmpRequest = request;
while (tmpRequest instanceof HttpServletRequestWrapper) {
tmpRequest = (HttpServletRequest) ((HttpServletRequestWrapper)
tmpRequest).getRequest();
}
String uri = tmpRequest.getRequestURI();

This code isn't checked for typos, but the logic has been tested. The while loop
unwraps all the wrappers to get back to the original request, then the String
"uri" will contain your original request URI. Let me know how it works for you
or if you find another solution.
 
T

Thomas Lutz

Therefore
instead of doing a straight request.getRequestURI(), do something like this:
HttpServletRequest tmpRequest = request;
while (tmpRequest instanceof HttpServletRequestWrapper) {
tmpRequest = (HttpServletRequest) ((HttpServletRequestWrapper)
tmpRequest).getRequest();
}
String uri = tmpRequest.getRequestURI();

This code isn't checked for typos, but the logic has been tested. The while loop
unwraps all the wrappers to get back to the original request, then the String
"uri" will contain your original request URI. Let me know how it works for you
or if you find another solution.

up and running !

this is exactly what i need, and it works. no typos :).

i am going to search the struts source now, because i can't believe
they have no tag to retrieve the "root" requestURI.

thank you, you really saved my day :) !

tom
 
Joined
Feb 29, 2012
Messages
1
Reaction score
0
Any solutions or Fix?

I am facing same issue when I tried to implement tiles, currently we are setting the mapping path in action. Is there any way I can get the action mapping in JSP directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top