近日体验了一下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的代码了