Search code examples
jakarta-eestruts2interceptorognl

How to display session value in JSP which is declared in interceptor?


I am new and testing struts2. Got value in session declared in the interceptor. Want to display session value in JSP but unable to do it.

Anyone tell me how to do it in OGNL?

Interceptor

package com.myapp.interceptors;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class GreetingInterceptor implements Interceptor {


    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        String Greeting = "My first Interceptor.";
        invocation.getInvocationContext().getSession().put("Greeting", Greeting);

        String result = invocation.invoke();
        return result;
    }
}

struts.xml

<struts>

    <!-- Configuration for the default package. -->
    <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor name="Greeting" class="com.myapp.interceptors.GreetingInterceptor"/>
        </interceptors>

        <!-- Greeting  -->
        <action name="greet" method="Greet" class="com.myapp.actions.GreetingAction">
            <result name="success">index.jsp</result>
            <interceptor-ref name="Greeting"/>
        </action>
    </package>

</struts>

Solution

  • you can access in your jsp as <s:property value='#session.Greeting'/>