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: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: