Struts2 timing on filling in <s:property> tags in JSP page
-
21-12-2019 - |
Вопрос
I have a simple struts2 web application with one action class and one interceptor. The action class has a getter method getTitle()
which fetches a string from the session. The interceptor is on the bottom of the default stack so it executes lastly and first after the invocation. It is built in such a way that it can modify the title that is in the session BEFORE action invocation and AFTER invocation. The resulting JSP page then shows this title.
I thought the following logic would apply:
- interceptor logic before invocation runs
- action class logic runs
- interceptor logic after invocation runs
- struts knows the result and gets the
.jsp
page in the result- struts fills in the
<:s>
tags in.jsp
and callgetTitle()
from the action class.
However this didn't work. The resulting JSP always showed the title as it was before step 3 was executed. I added some simple logging in my java code and it confirmed my suspicion. This was the order of execution:
- log: interceptor logic before invocation runs
- log: action class logic runs
- log:
getTitle()
method from action class runs- log: interceptor logic after invocation runs
So somehow step 6 became step 3. Why is this and how can I add logic in my interceptor AFTER action invocation to alter the title?
Решение
Nutshell: implement PreResultListener
to have stuff happen before rendering, but after invocation.
http://struts.apache.org/release/2.3.x/docs/writing-interceptors.html
I don't know what title
is, but I'm having a hard time understanding why anything relating to something like a page title, or a book title from a domain object, or much else would need to:
- Be retrieved by an action, or
- Be changed by an interceptor