Struts自定义标签的历程[Java编程]
本文“Struts自定义标签的历程[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
近日体验了一下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");
}
}
以上是“Struts自定义标签的历程[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |