当在xwork.xml中配置<action>时,在<result>部分可以写成这样:
......
<result name="success" type="redirect">
<param name="location">/${path}</param>
</result>
......
其中,${}包含了一个ognl表达式,从WebWork的OgnlValueStack中获取内容,进而动态生成内容。
在Action中可以这样写:
......
private String path;
public String getPath(){
return path;
}
public String execute() throws Exception {
//把下面的代码解除注释可以在stack中存储其它的对象
//HttpServletRequest request=ServletActionContext.getRequest();
//OgnlValueStack stack=ServletActionContext.getValueStack(request);
path="success.jsp";
return SUCCESS;
}
由于Action会位于栈顶,因此"${path}"会调用getPath()方法,获得"success.jsp",动态生成结果"/success.jsp"。
本文介绍如何在Struts2框架中使用Ognl表达式实现动态URL重定向。通过在Action类中设置路径变量,并利用<result>标签中的redirect类型结合Ognl表达式,可以实现动态跳转到指定页面。
2437

被折叠的 条评论
为什么被折叠?



