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).
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:
<%
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?
Many thanks for any advice!
${myValue}. I find it nearly inconceivable that a tag, and even OGNL, is so difficult to understand that you'd resort to scriptlets. Are there specific things that cause you issues? If so, maybe it would make more sense to address that, than to try to figure out ways to write very suspicious code to do things trivially provided out-of-the-box. - Dave Newton