简体   繁体   English

使用 Struts2 访问 JSP 中的 Action 类

[英]Accessing Action class in JSP using Struts2

Does anyone know how to easily access the Action class in a JSP when using Struts2?有谁知道在使用 Struts2 时如何轻松访问 JSP 中的 Action 类? 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).虽然我知道通常可以使用 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: ...这将在您自己的 Action 类扩展的BaseAction类中,以便在您的 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!非常感谢您的任何建议!

If you don't want to use struts2 tags an equally valid approach is to use JSTL tags.如果您不想使用 struts2 标签,一个同样有效的方法是使用 JSTL 标签。 These tags are supported by struts2 and I'm guessing most major java web frameworks. 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.学习这几个标签的时间将节省大量时间。

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!

Besides Quaternion's answer , I want to add another way to call action object in here.The action object is on the top of the valueStack.In your jsp, you can access struts action like this,除了四元数的回答,我想在这里添加另一种调用动作对象的方法。动作对象在值堆栈的顶部。在你的jsp中,你可以像这样访问struts动作,

<% MyAction myAction = (MyAction) ActionContext.getContext().getValueStack().peek(); %>

References: https://struts.apache.org/tag-developers/access-to-valuestack-from-jsps.html参考资料: https : //struts.apache.org/tag-developers/access-to-valuestack-from-jsps.html

If you want to know more how to access via ognl expression, the following discussion may be helpful for you What are different ways to access variables with OGNL in Struts 2如果您想了解更多如何通过 ognl 表达式访问,以下讨论可能对您有所帮助在 Struts 2 中使用 OGNL 访问变量有哪些不同的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM