歡迎您光臨本站 註冊首頁

stuts2中tree的實現

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  在struts2中,tree的實現主要是通過struts2-dojo-plugin-XXX.jar中的tree標籤來實現的,首先在項目中引入此包,我使用的是sturts2.1.8.1.

  在JSP中使用<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>導入標籤標籤的同時,別忘了再HEAD標籤中添加<sx:head/>,客戶端JS文件的導入全指望它了!下面是實現tree的三種方式:

  1.靜態方式 也就是tree中的各個節點是在jsp文件中靜態寫入的,在運行之前就知道其結構,下面是一個Demo:

  JSP

  <h2>1.Three Without AJAX</h2><sx:tree label="Tree without AJAX" id="parentId" templateCssPath="/struts/tree.css" showRootGrid="true" showGrid="true"> <sx:treenode label="child1" id="child1Id"> <sx:treenode label="grandchild1" id="grandchild1Id"/> <sx:treenode label="grandchild2" id="grandchild2Id"/> <sx:treenode label="grandchild3" id="grandchild3Id"/> <sx:treenode label="grandchild4" id="grandchild4Id"/> <sx:treenode label="grandchild5" id="grandchild5Id"/> </sx:treenode> <sx:treenode label="child2" id="child2Id"/> <sx:treenode label="child3" id="child3Id"/> <sx:treenode label="child4" id="child4Id"/> <sx:treenode label="child5" id="child5Id"/> <sx:treenode label="child6" id="child6Id"> <sx:treenode label="gChild1" id="gChild1Id"/> <sx:treenode label="gChild2" id="gChild2Id"/> </sx:treenode> <sx:treenode label="child7" id="child7Id"/></sx:tree>

  效果圖如上所示.

  2.半動態方式 下面的例子是根據伺服器端的項目目錄生成的節點樹,在jsp中的表現代碼上顯得非常簡捷:

  首先要生成一個用於封裝目錄下文件結構的類:FileWrapper.Class

  FileWrapper.java

  package tree;import java.io.File; public class FileWrapper { private File file; public FileWrapper(String path){ file = new File(path); } public FileWrapper(File file){ this.file = file; } public String getId(){ return "file_" file.hashCode(); } public String getName(){ return file.getName(); } public String getAbsolutePath(){ return file.getAbsolutePath(); } public FileWrapper[] getChildren(){ File[] files = file.listFiles(); if(files != null && files.length>0){ int length = files.length; FileWrapper[] wrappers = new FileWrapper[length]; for(int i=0;i<length;i ){ wrappers[i] = new FileWrapper(files[i]); } return wrappers; } else{ return new FileWrapper[0]; } }}

  新建一個Action,用於向ValueStack中添加生成目錄樹所需的一些數據,

  DynamicTreeAction.java

  package tree;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.ActionSupport;public class DynamicTreeAction extends ActionSupport implements ServletRequestAware { private static final long serialVersionUID = -8571025932301813496L; private FileWrapper root; private HttpServletRequest request; public FileWrapper getRoot(){ return root; } public void setServletRequest(HttpServletRequest request){ this.request = request; } public String execute(){ root = new FileWrapper(request.getSession().getServletContext().getRealPath("/")); return SUCCESS; }}

  在xml中配置action

  struts.xml

  <package name="ajax" namespace="/ajax" extends="json-default"> <action name="DynamicTree" class="tree.DynamicTreeAction"> <result>/tree.jsp</result> </action> </package>

  JSP文件內容:

  <h2>2.Tree with AJAX</h2><sx:tree id="appFiles" rootNode="root" nodeTitleProperty="name" nodeIdProperty="id" childCollectionProperty="children"/>

  看起來非常簡捷,其實struts已經幫我們幹了好多事情,

  OK,查看一下效果:


[火星人 ] stuts2中tree的實現已經有461次圍觀

http://coctec.com/docs/java/show-post-60608.html