java 使用 Struts2 访问 JSP 中的 Action 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7504365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Action class in JSP using Struts2
提问by Alex
Does anyone know how to easily access the Action class in a JSP when using Struts2? While I know it is often possible to use Struts tags and OGNL, I actually find them both to be confusing (clearly due to ignorance) and quite frankly find it easier to maintain Java in the JSP (not to mention it's easier to explain to new programmers as everyone knows Java).
有谁知道在使用 Struts2 时如何轻松访问 JSP 中的 Action 类?虽然我知道通常可以使用 Struts 标记和 OGNL,但实际上我发现它们都令人困惑(显然是由于无知)并且坦率地说,发现在 JSP 中维护 Java 更容易(更不用说向新人解释更容易了)众所周知Java的程序员)。
I have searched for a solutions for years, and the best solution I have found is to call a static method from a class, that looks like:
多年来,我一直在寻找解决方案,我发现的最佳解决方案是从类中调用静态方法,如下所示:
public static BaseAction getCurrentAction(HttpServletRequest request) {
OgnlValueStack ognlStack = (OgnlValueStack)request.getAttribute(org.apache.struts2.ServletActionContext.STRUTS_VALUESTACK_KEY);
return (BaseAction)ognlStack.getRoot().get(0);
}
...which would be in a BaseAction
class extended by you own Action class, so that in your JSP you can say:
...这将在BaseAction
您自己的 Action 类扩展的类中,以便在您的 JSP 中您可以说:
<%
MyAction action = (MyAction)BaseAction.getCurrentAction(request);
String myValue = action.getMyValue();
%>
However this all seems overly complicated and it assumes a precise order in the OgnlValueStack
- there must be a better way, non?
然而,这一切似乎过于复杂,而且它假定了一个精确的顺序OgnlValueStack
——一定有更好的方法,不是吗?
Many thanks for any advice!
非常感谢您的任何建议!
回答by Quaternion
If you don't want to use struts2 tags an equally valid approach is to use JSTL tags. These tags are supported by struts2 and I'm guessing most major java web frameworks.
如果您不想使用 struts2 标签,一个同样有效的方法是使用 JSTL 标签。struts2 支持这些标签,我猜大多数主要的 Java Web 框架都支持这些标签。
It is strongly recommended that you avoid servlets/scriplets in typical business programming using any Java Web Framework.
强烈建议您在使用任何 Java Web 框架的典型业务编程中避免使用 servlet/脚本。
You probably already know this but to get a property from the action just say:
您可能已经知道这一点,但要从操作中获取属性,只需说:
<s:property value="myProperty"/>
Or equally valid using the JSTL (some here would even say more valid as the view no longer depends on struts2)
或者使用 JSTL 同样有效(这里有些人甚至会说更有效,因为视图不再依赖于 struts2)
<c:out value="${myProperty}" />
There are few programmers (and I would stand to say no seasoned struts2 programmers) who would find this harder to understand than
很少有程序员(我敢说没有经验丰富的 struts2 程序员)会发现这比
<%
MyAction action = (MyAction)BaseAction.getCurrentAction(request);
String myValue = action.getMyValue();
%>
There are only a handful of tags required to generate a page, you need to get properties, iterate to produce tables/lists and that's about it. The time learning those few tags will save a great deal of time.
生成页面只需要少数标签,您需要获取属性、迭代以生成表/列表,仅此而已。学习这几个标签的时间将节省大量时间。
回答by Russell Shingleton
To follow up on Quaternion's answer, you can access any public method in your action class from OGNL tags or JSTL as he suggested.
要跟进 Quaternion 的回答,您可以按照他的建议从 OGNL 标签或 JSTL 访问您的操作类中的任何公共方法。
You can also pass parameters to the action class through the tags:
您还可以通过标签将参数传递给操作类:
public String getHello(String value){
return "Hello " + value + "!";
}
Which is called on the JSP:
在 JSP 上调用:
<s:property value="getHello('Russell')"/>
Which outputs:
哪些输出:
Hello Russell!