近日體驗了一下Eclipse
打開某個jsp頁面,頁面上的select下拉列表框用Struts自定義標簽來完成
1、新建頁面:Test.jsp
2、在Web應用的WEB-INF目錄下自定義標簽TLD文件:TestTag.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>dtree</shortname>
<uri>http://jakarta.apache.org/struts/tags-bean</uri>
<tag>
<name>selectResnodes</name>
<tagclass>myWeb.taglib.SelectTag</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>
3、在myWeb.Action包下定義AbstractResAction.java文件和TestAction.java文件(利用了java的反射機制)
AbstractResAction.java文件:
package myWeb.Action;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public abstract class AbstractResAction extends Action {
public final ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
String cmd = httpServletRequest.getParameter("actionType"); // 獲取參數,其實為函數名
System.out.println("actionType = " + cmd);
if (cmd == null || cmd.equals(""))
return actionMapping.findForward("default");
try {
Method method = this.getClass()
.getMethod(
cmd,new Class[] { ActionMapping.class,
ActionForm.class, HttpServletRequest.class,
HttpServletResponse.class });
// 利用反射機制,調用method方法。而這個method方法是由頁面指定的actionType定義的。
return (ActionForward) method.invoke(this, new Object[] {
actionMapping, actionForm, httpServletRequest,
httpServletResponse });
} catch (Exception e) {
e.printStackTrace();
return actionMapping.findForward("default");
}
}
}
TestAction.java文件:
package myWeb.Action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.asiainfo.ainx.advancedres.bo.ResnodesBO;
public class QueryNodesAction extends AbstractResAction {
public ActionForward showNodesList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ResnodesBO bo = new ResnodesBO();
ArrayList alNodes = bo.selectData(); //我的應用中定義了BO和DAO類來封裝對數據庫的讀寫。此處為從數據庫中取出的數據結果集
request.setAttribute("resnodesList",alNodes); //頁面中的用到該自定義標簽時要用到resnodesList這個屬性
return mapping.findForward("showResnode");
}
}
4、在myWeb.taglib包下定義SelectTag.java文件
package myWeb.taglib; import java.io.IOException; import java.util.ArrayList;import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport;import org.apache.struts.util.RequestUtils;import myWeb.vo.ResnodesVO; public class SelectTag extends TagSupport {//這裡的三個屬性對應TestTag.tld文件中對selectResnodes定義的三個屬性 private String id; private String scope; private String nodeslist; /** * @throws IOException */ public int doStartTag() { ArrayList list = null; try { list = (ArrayList) RequestUtils.lookup(pageContext, nodeslist, scope); } catch (JspException e1) { e1.printStackTrace(); } if (list == null || list.size() == 0) return SKIP_BODY; JspWriter out = pageContext.getOut(); try { if (list != null) { out.println("<select name=\"Sel_Nodes\" style=\"width:90%\">"); for (int i = 0; i < list.size(); i++) { ResnodesVO nodevo = (ResnodesVO) list.get(i); out.println("<option value=\"" + nodevo.getId() + "\">" + nodevo.getSName() + "</option>"); // System.out.println(nodevo.getSName()); } out.println("</select>"); out.flush(); } } catch (IOException e) { e.printStackTrace(); } return EVAL_BODY_INCLUDE; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNodeslist() { return nodeslist; } public void setNodeslist(String nodeslist) { this.nodeslist = nodeslist; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } }
5、修改struts-config.xml文件:
……
<action-mappings>
……
<action type="myWeb.Action.TestAction" scope="request" path="/queryResnodes"> <!-- 在地址欄或鏈接中請求時用該路徑queryResnodes-->
<forward name="showResnode" path="/Test.jsp" /> <!-- 在TestAction.java中執行查詢數據的函數後,跳轉至該showResnode定義的頁面,即Test.jsp-->
</action>
……
</action-mappings>
……
6、保證Test.jsp中包含如下的代碼:
<%@ taglib uri="/WEB-INF/TestTag.tld" prefix="slotnode"%>
<body>
……
<slotnode:selectResnodes id="nodelist" nodeslist="resnodesList" scope="request" />
……
</body>
其中prefix的名字是隨便起的,只要有意義就可以。
但是在後面用的時候就要用這個名字,本例中是slotnode。
<slotnode:selectResnodes …… 中的selectResnodes和TestTag.tld中定義的<tag><name>selectResnodes</name>……必須是相同的的
另外nodeslist="resnodesList"中resnodesList即TestAction.java中定義的
request.setAttribute("resnodesList",alNodes); 中寫入的屬性resnodesList,兩個名字必須是相同的。
7、比如我的Web應用發布的名字為LearnTag,發布在本地的Tomcat中,默認端口8080則在地址欄輸入:http://localhost:8080/LearnTag/queryResnodes.do?actionType=showNodesList
OK.出來結果了吧。
整理的有點亂,再總結一下:
從頁面發起。do請求,請求轉至action,在action文件中執行數據庫查詢,取得需要的數據
將查詢得到的數據列表用setAttribute加入request
自定義標簽中取request中的加入的數據列表
由自定義標簽中的標簽java文件完成將數據打印出來的功能(用JspWriter類)
在頁面顯示出來。查看jsp文件的源代碼,可以看到自定義的標簽和數據都轉換為html的代碼了