[英]Struts2 pass action's method result to custom tag
我想創建可以與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:
<%-- pass instance of Department to tag --%>
如果要將MyAction#getRoot
結果MyAction#getRoot
給我的虛擬標簽,該怎么辦?
我嘗試過這些:
什么也沒發生,在標記中,我得到的字符串的root
值為root
或為null。
我無法將對象傳遞給標簽屬性。
我做錯了什么?
首先,我強烈建議您避免在JSP頁面中使用scriptlet塊。 它已被棄用很長時間了。 使用JSP EL / JSTL(或Struts2中的OGNL)是一種更好的方法。
如果您的操作通過getRoot()
方法公開Department,則可以將其傳遞給JSP標記,如下所示:
<your:jspTag tree="${action.root}"/>
注意:不能像傳遞Struts2標記那樣將OGNL表達式傳遞給JSP簡單標記。
然后,假設標簽中的“樹”是指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}
謝謝,我不知道這種訪問動作的方式:$ {action.root}在等待回復時,我發現了其他基於jn Struts2的解決方案。
我您的解決方案更好,因為它與struts2沒有緊密結合。 謝謝!
<%@ tag language =“ java” pageEncoding =“ UTF-8” body-content =“ empty”%> <%@屬性名=“ tree” required =“ true”%> <%@ taglib uri =“ http: //java.sun.com/portlet_2_0“前綴=” 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()+"]");
}
%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.