Struts2 将操作的方法结果传递给自定义标签
我想创建可以与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我强烈建议您避免在 JSP 页面中出现 scriptlet 块。它已经被弃用很长时间了。使用 JSP EL/JSTL(或 Struts2 中的 OGNL)是更好的方法。
如果您的操作通过
getRoot()
方法公开 Department,那么您可以将其传递给 JSP 标记,如下所示:注意: 您不能将 OGNL 表达式传递给 JSP 简单标记您可以使用 Struts2 标签。
然后,假设标签中的“tree”指的是部门:
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:
谢谢,我不知道这种访问操作的方式:${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"%><%
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"%><%