Struts2 将操作的方法结果传递给自定义标签

发布于 2024-10-01 00:56:06 字数 1243 浏览 5 评论 0原文

我想创建可以与 Struts2 一起使用的简单虚拟标签。

我有一个操作:

class MyAction extends ActionSupport{

  /** 
    Some code
   */
  public Department getRoot(){
    /** Some code foes here...*/
    return departmentInstance;
  }
}

一个标签:

<%@tag language="java" pageEncoding="UTF-8"  body-content="empty"  %>
<%@ attribute name="tree" required="true"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="p"%>
<p:defineObjects />
<%@tag import="ejb.model.Department"%><%
 Object attrTree = pageContext.getAttribute("tree");
 System.out.println("TreeTagHelper->tree=["+attrTree+"]");
 if(attrTree!=null){
  System.out.println("TreeTagHelper->tree.class=["+attrTree.getClass().getName()+"]");
 }else{
  System.out.println("TreeTagHelper->tree.class=[NULL]");
 }
 try{
 //some code...
 }catch(Exception e){
  System.out.println("Error while drawing tree["+e.getMessage()+"]");
 }
%>

以及带有标签的jsp:

<%--将 Department 实例传递给标签 --%>

如果我想将 MyAction#getRoot 的结果传递给我的虚拟标签,我该怎么办?

我已经尝试过这些:

什么也没有发生,在标签中我得到值为 root 的 String 或得到 null。

我无法将对象传递给标记属性。

我做错了什么?

I want to create simple dummy tag which can work with Struts2.

I have an action:

class MyAction extends ActionSupport{

  /** 
    Some code
   */
  public Department getRoot(){
    /** Some code foes here...*/
    return departmentInstance;
  }
}

a tag:

<%@tag language="java" pageEncoding="UTF-8"  body-content="empty"  %>
<%@ attribute name="tree" required="true"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="p"%>
<p:defineObjects />
<%@tag import="ejb.model.Department"%><%
 Object attrTree = pageContext.getAttribute("tree");
 System.out.println("TreeTagHelper->tree=["+attrTree+"]");
 if(attrTree!=null){
  System.out.println("TreeTagHelper->tree.class=["+attrTree.getClass().getName()+"]");
 }else{
  System.out.println("TreeTagHelper->tree.class=[NULL]");
 }
 try{
 //some code...
 }catch(Exception e){
  System.out.println("Error while drawing tree["+e.getMessage()+"]");
 }
%>

and my jsp with tag:

<%-- pass instance of Department to tag --%>

What do I have to do if I want to pass result of MyAction#getRoot to my dummy tag?

I've tried to these:

Nothing happens, in tag I get String with value root or get null.

I can't pass an object to tag attribute.

What do I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凹づ凸ル 2024-10-08 00:56:06

首先,我强烈建议您避免在 JSP 页面中出现 scriptlet 块。它已经被弃用很长时间了。使用 JSP EL/JSTL(或 Struts2 中的 OGNL)是更好的方法。

如果您的操作通过 getRoot() 方法公开 Department,那么您可以将其传递给 JSP 标记,如下所示:

<your:jspTag tree="${action.root}"/>

注意: 您不能将 OGNL 表达式传递给 JSP 简单标记您可以使用 Struts2 标签。

然后,假设标签中的“tree”指的是部门:

<%@ tag language="java" pageEncoding="UTF-8" body-content="empty" %>
<%@ attribute name="tree" required="true" type="ejb.model.Department" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="p" %>
<p:defineObjects />

${tree.someProperty}

First, I strongly encourage you to avoid scriptlet blocks in your JSP pages. It's been deprecated for a really long time. Using the JSP EL/JSTL (or OGNL in Struts2) is a much better approach.

If your action exposes Department via a getRoot() method, then you can pass it to a JSP tag as:

<your:jspTag tree="${action.root}"/>

Note: You cannot pass OGNL expressions to JSP simple tags the way you can to a Struts2 tag.

Then, assuming 'tree' in your tag refers to Department:

<%@ tag language="java" pageEncoding="UTF-8" body-content="empty" %>
<%@ attribute name="tree" required="true" type="ejb.model.Department" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="p" %>
<p:defineObjects />

${tree.someProperty}
心凉怎暖 2024-10-08 00:56:06

谢谢,我不知道这种访问操作的方式:${action.root}
在等待回复的过程中,我发现了其他基于jn Struts2的解决方案。

我的解决方案更好,因为它与 struts2 没有紧密耦合。
谢谢你!

<%@tag language="java" pageEncoding="UTF-8" body-content="空" %>
<%@属性名称=“树”必需=“true”%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="p"%>

<%@tag import="web.tag.TreeTagHelper"%>
<%@tag import="ejb.model.Department"%><%

    /** Get value of tag attribute. */
    String attrTree = (String)pageContext.getAttribute("tree");

    /** Find attribute value in stack*/
    Department department = (Department)com.opensymphony.xwork2.ActionContext.getContext().getValueStack().findValue(attrTree);
    try{
        TreeTagHelper tth = new TreeTagHelper(department, out);
        tth.printTree();
    }catch(Exception e){
        System.out.println("Error while drawing tree["+e.getMessage()+"]");
    }
%>

Thanks, I didn't know about this way accessing actions: ${action.root}
While waiting for the reply, I found other solution based jn Struts2.

I your solution is better because it's not tightly coupled with struts2.
Thank you!

<%@tag language="java" pageEncoding="UTF-8" body-content="empty" %>
<%@ attribute name="tree" required="true"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="p"%>

<%@tag import="web.tag.TreeTagHelper"%>
<%@tag import="ejb.model.Department"%><%

    /** Get value of tag attribute. */
    String attrTree = (String)pageContext.getAttribute("tree");

    /** Find attribute value in stack*/
    Department department = (Department)com.opensymphony.xwork2.ActionContext.getContext().getValueStack().findValue(attrTree);
    try{
        TreeTagHelper tth = new TreeTagHelper(department, out);
        tth.printTree();
    }catch(Exception e){
        System.out.println("Error while drawing tree["+e.getMessage()+"]");
    }
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文