user1884155 user1884155 - 2 months ago 10
Java Question

Struts2 timing on filling in <s:property> tags in JSP page

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:



  1. interceptor logic before invocation runs

  2. action class logic runs

  3. interceptor logic after invocation runs

  4. struts knows the result and gets the
    .jsp
    page in the result

  5. struts fills in the
    <:s>
    tags in
    .jsp
    and call
    getTitle()
    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:



  1. log: interceptor logic before invocation runs

  2. log: action class logic runs

  3. log:
    getTitle()
    method from action class runs

  4. 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?

Answer

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:

  1. Be retrieved by an action, or
  2. Be changed by an interceptor
Comments