说明:在OgnlValueStack.setValue(expr, value);如果不提供getter,setter方法是取不出来的,可以通过一下方式获取
OgnlInterceptor.class
package
com.ognl.test;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;
public class OgnlInterceptor implements Interceptor
{
public void destroy()
{
}
public void init()
{
}
public String intercept(ActionInvocation invocation) throws Exception
{
invocation.getStack().push( new TestBean());
invocation.getStack().setValue( " url " , " http://www.baidu.com " );
return " test " ;
}
}
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;
public class OgnlInterceptor implements Interceptor
{
public void destroy()
{
}
public void init()
{
}
public String intercept(ActionInvocation invocation) throws Exception
{
invocation.getStack().push( new TestBean());
invocation.getStack().setValue( " url " , " http://www.baidu.com " );
return " test " ;
}
}
TestBean.class
package
com.ognl.test;
public class TestBean
... {
private String url = "http://www.google.cn";
public String getUrl()
...{
return url;
}
public void setUrl(String url)
...{
this.url = url;
}
}
public class TestBean
... {
private String url = "http://www.google.cn";
public String getUrl()
...{
return url;
}
public void setUrl(String url)
...{
this.url = url;
}
}
xwork.xml
<
interceptor
name
="ognlInterceptor"
class
="com.ognl.test.OgnlInterceptor"
/>
< global-results >
< result name ="test" type ="redirect" > ${url} </ result >
</ global-results >
< action name ="testAction" class ="com.test.TestAction" >
< external-ref name ="studentDao" > studentDao </ external-ref >
< result name ="success" type ="freemarker" >
< param name ="location" > index.ftl </ param >
</ result >
< interceptor-ref name ="testInterceptor" />
</ action >
< global-results >
< result name ="test" type ="redirect" > ${url} </ result >
</ global-results >
< action name ="testAction" class ="com.test.TestAction" >
< external-ref name ="studentDao" > studentDao </ external-ref >
< result name ="success" type ="freemarker" >
< param name ="location" > index.ftl </ param >
</ result >
< interceptor-ref name ="testInterceptor" />
</ action >