开发者

Struts2 pass action's method result to custom tag

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?


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}


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()+"]");
    }
%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜