熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts自定義標簽的過程

2022-06-13   來源: Java開源技術 

  近日體驗了一下Eclipse

  打開某個jsp頁面頁面上的select下拉列表框用Struts自定義標簽來完成

  新建頁面Testjsp

  在Web應用的WEBINF目錄下自定義標簽TLD文件TestTagtld

  

  <?xml version= encoding=UTF?>
<!DOCTYPE taglib PUBLIC //Sun Microsystems Inc//DTD JSP Tag Library //EN jsptaglibrary__dtd>
<taglib>
   <tlibversion></tlibversion>
   <jspversion></jspversion>
   <shortname>dtree</shortname>
   <uri>bean</uri>

  <tag>
     <name>selectResnodes</name>
     <tagclass>myWebtaglibSelectTag</tagclass>
     <bodycontent>empty</bodycontent>
     <attribute>
        <name>id</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
     </attribute>
     <attribute>
        <name>nodeslist</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
     </attribute>
     <attribute>
         <name>scope</name>
         <required>false</required>
         <rtexprvalue>false</rtexprvalue>
     </attribute>
   </tag>
</taglib>

  在myWebAction包下定義AbstractResActionjava文件和TestActionjava文件(利用了java的反射機制)

  AbstractResActionjava文件

  
package myWebAction;
import javalangreflectMethod;

  import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;

  public abstract class AbstractResAction extends Action {
 public final ActionForward execute(ActionMapping actionMapping
   ActionForm actionForm HttpServletRequest httpServletRequest
   HttpServletResponse httpServletResponse) {
  String cmd = (actionType); // 獲取參數其實為函數名
  
  Systemoutprintln(actionType = + cmd);
  
  if (cmd == null || cmdequals())
   return actionMappingfindForward(default);
  try {
   Method method = thisgetClass()
     getMethod(
       cmdnew Class[] { ActionMappingclass
         ActionFormclass HttpServletRequestclass
         HttpServletResponseclass });

  // 利用反射機制調用method方法而這個method方法是由頁面指定的actionType定義的
   return (ActionForward) methodinvoke(this new Object[] {
     actionMapping actionForm httpServletRequest
     httpServletResponse });
  } catch (Exception e) {
   eprintStackTrace();
   return actionMappingfindForward(default);
  }
 }
}
 
TestActionjava文件
package myWebAction;
import javautilArrayList;
 
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import comasiainfoainxadvancedresboResnodesBO;
 
public class QueryNodesAction extends AbstractResAction {
 public ActionForward showNodesList(ActionMapping mapping ActionForm form
   HttpServletRequest request HttpServletResponse response) {
  ResnodesBO bo = new ResnodesBO();
  ArrayList alNodes = boselectData();   //我的應用中定義了BO和DAO類來封裝對數據庫的讀寫此處為從數據庫中取出的數據結果集
  
  requestsetAttribute(resnodesListalNodes);  //頁面中的用到該自定義標簽時要用到resnodesList這個屬性
  return mappingfindForward(showResnode);
 }
}

  在myWebtaglib包下定義SelectTagjava文件

  
package myWebtaglib;
 
import javaioIOException;
import javautilArrayList;
import javaxservletjspJspException;
import javaxservletjspJspWriter;
import javaxservletjsptagextTagSupport;
import orgapachestrutsutilRequestUtils;
import myWebvoResnodesVO;
 
public class SelectTag extends TagSupport {
//這裡的三個屬性對應TestTagtld文件中對selectResnodes定義的三個屬性
 private String id;
 private String scope;
 private String nodeslist;
 /**
  * @throws IOException
  */
 public int doStartTag() {
  ArrayList list = null;
  try {
   list = (ArrayList) RequestUtilslookup(pageContext nodeslist
     scope);
  } catch (JspException e) {
   eprintStackTrace();
  }
  if (list == null || listsize() == )
   return SKIP_BODY;
  JspWriter out = pageContextgetOut();
  try {
   if (list != null) {
    outprintln(<select name=\Sel_Nodes\ style=\width:%\>);
    for (int i = ; i < listsize(); i++) {
     ResnodesVO nodevo = (ResnodesVO) listget(i);
     outprintln(<option value=\ + nodevogetId() + \>
       + nodevogetSName() + </option>);
//     Systemoutprintln(nodevogetSName());
    }
    outprintln(</select>);
    outflush();
   }
  } catch (IOException e) {
   eprintStackTrace();
  }
  return EVAL_BODY_INCLUDE;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  thisid = id;
 }
 public String getNodeslist() {
  return nodeslist;
 }
 public void setNodeslist(String nodeslist) {
  thisnodeslist = nodeslist;
 }
 public String getScope() {
  return scope;
 }
 public void setScope(String scope) {
  thisscope = scope;
 }
}

  修改strutsconfigxml文件

  
……
<actionmappings>
  ……
  <action type=myWebActionTestAction scope=request path=/queryResnodes>  <! 在地址欄或鏈接中請求時用該路徑queryResnodes >
   <forward name=showResnode path=/Testjsp />    <! 在TestActionjava中執行查詢數據的函數後跳轉至該showResnode定義的頁面即Testjsp >
  </action>
  ……
</actionmappings>
……

  保證Testjsp中包含如下的代碼

  
<%@ taglib uri=/WEBINF/TestTagtld prefix=slotnode%> 

  <body>

  ……

  <slotnode:selectResnodes id=nodelist nodeslist=resnodesList scope=request />

  ……

  </body>

其中prefix的名字是隨便起的只要有意義就可以
 
但是在後面用的時候就要用這個名字本例中是slotnode
<slotnode:selectResnodes …… 中的selectResnodes和TestTagtld中定義的<tag><name>selectResnodes</name>……必須是相同的的
另外nodeslist=resnodesList中resnodesList即TestActionjava中定義的
requestsetAttribute(resnodesListalNodes);  中寫入的屬性resnodesList兩個名字必須是相同的

  比如我的Web應用發布的名字為LearnTag發布在本地的Tomcat中默認端口則在地址欄輸入?actionType=showNodesList

  OK出來結果了吧

  整理的有點亂再總結一下

  從頁面發起do請求請求轉至action在action文件中執行數據庫查詢取得需要的數據

  將查詢得到的數據列表用setAttribute加入request

  自定義標簽中取request中的加入的數據列表

  由自定義標簽中的標簽java文件完成將數據打印出來的功能(用JspWriter類)

  在頁面顯示出來查看jsp文件的源代碼可以看到自定義的標簽和數據都轉換為html的代碼了


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28770.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.