简体   繁体   English

访问JSP Java scriptlet中的控制器方法而不是使用标签?

[英]Access to controller methods in JSP Java scriptlet rather than using tags?

My struts configuration: 我的支柱配置:

<action name="myAction" class="my.controller.MyAction">
    <result name="myPage">/myPage.jsp</result>

MyAction has a method public String getSomeValue() { ... } . MyAction有一个方法public String getSomeValue() { ... }

In myPage.jsp , I can print that value easily to the HTML stream: myPage.jsp ,我可以轻松地将该值打印到HTML流:

<s:property value="someValue" />

However, I would like to print it to the console: 但是,我想将其打印到控制台:

<%

//how do I reference myActionBean
String someVal = myActionBean.getSomeValue();
System.out.println(someVal);

%>

My question is, how do I reference the action controller (replace myActionBean in the code above) inside a JSP code block, just like the s:property tag does in its syntax that eliminates the "get" part of the method ? 我的问题是,如何在JSP代码块内引用动作控制器(在上面的代码中将myActionBean替换),就像s:property标记的语法中消除了方法的“获取”部分一样? I would like to access myActionBean.getSomeValue() in Java in the JSP rather than doing it in a tag. 我想在JSP myActionBean.getSomeValue() Java访问myActionBean.getSomeValue()而不是在标记中进行操作。 I know this is not a recommended way of doing things but this is just for debugging. 我知道这不是建议的处理方式,但这仅用于调试。

As suggested by @DaveNewton, I was able to access the action class from the context: 如@DaveNewton所建议,我能够从上下文访问动作类:

<%
    ActionContext context = ActionContext.getContext();

    //this will access actionClass.getFoo() just like the tag
    //<s:property value="%{foo}"/> does but outputs to HTML
    Object fooObj = context.getValueStack().findValue("foo");
    String fooStr = (String)fooObj;

    //so that we can print it to the console
    //because the tag can only output to HTML 
    //and printing to the console is the objective of the question
    System.out.println("foo = " + fooStr);
%>

I had to import ActionContext on top of the JSP: 我必须在JSP上导入ActionContext

<%@ page import="com.opensymphony.xwork2.ActionContext" %>

I understand some folks don't like that I should want to do this but that is actually exactly what I wanted to do. 我知道有些人不喜欢我应该这样做,但这实际上正是我想要做的。 I know well that I could do a System.out in getFoo() itself but I wanted to do it in the JSP. 我知道我可以在getFoo()本身中执行System.out ,但我想在JSP中进行。

You can get action bean from action invocation like you do in the interceptor or from the value stack where it's already pushed. 您可以像在拦截器中一样从动作调用中获取动作bean,也可以从已经被推送的值堆栈中获取动作bean。 Since you have access to the value stack from JSP and know how to print property the easiest way for you to set the action bean to the request attribute with <s:set> tag. 由于您可以从JSP访问值堆栈,并且知道如何打印属性,因此最简单的方法是使用<s:set>标记将操作bean设置为request属性。

<s:set var="action" value="action" scope="request"/> 

Now you can get the action bean 现在您可以获得动作豆

<% 
  MyAction myActionBean = request.getAttribute("action");
  String someVal = myActionBean.getSomeValue();
  System.out.println(someVal);
%>

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

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