Accessing Action class in JSP using Struts2

一个人想着一个人 提交于 2019-12-06 13:03:49

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.

It is strongly recommended that you avoid servlets/scriplets in typical business programming using any Java Web Framework.

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)

<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

<%
  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.

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:

<s:property value="getHello('Russell')"/>

Which outputs:

Hello Russell!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!