0

我是新手,正在测试struts2。在拦截器中声明的会话中获得值。想要在 JSP 中显示会话值但无法做到。

有人告诉我如何在 OGNL 中做到这一点吗?

拦截器

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

1 回答 1

2

你可以在你的jsp中访问<s:property value='#session.Greeting'/>

于 2012-09-21T05:08:20.007 回答