struts自定义标榜 下拉标签(字典表)-----------完成全步骤
--主---表 库设计table 001
字段 Id int nonull
userName varchar null
password varchar null
role_id varchar null
--从---表 库设计table 002 Role字典表
字段 Id int nonull
Name varchar null
/**********************************/
辅助类
/#############写有id,name属性的DTO----------------------UtilDto
package com.zhuqiufa.struts.dto;
public class UtilDto {
//ID
private String id;
//Name
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*******************************/
辅助类---------------------函数nullToStr--------参数不传NULL
/######
PUtil.nullToStr
package com.dd.struts.util;
import java.util.Stack;
public class PUtil {
/*
* 字符串不为null
*/
public static final String nullToStr(String str)
{
if(str==null)
{
str="";
}
return str;
}
}
/**********************************/
/#####RoleTag标签类
RoleTag.java
package com.dd.struts.util;
import java.util.List;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.dd.struts.dto.UtilDto;
public final class RoleTag extends TagSupport {
//属性名
private String name="role_id";
//属性值
private String value=null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int doEndTag() throws JspException {
JspWriter out=pageContext.getOut();
try {
out.println("<SELECT style=\"FONT-SIZE: 12px; FONT-FAMILY: MS Gothic\" name=\""+ getName() + "\" id=\""+ getName() + "\">");
if ("".equals(PUtil.nullToStr(getValue())))
out.println("<OPTION selected></OPTION>");
else {
out.println("<OPTION value=\"\"></OPTION>");
}
//取Role字典列表
List Rlist=TagUtil.RoleList();
if(Rlist!=null)
{
int len=Rlist.size();
UtilDto UDto=null;
for(int i=0;i<len;i++) {
UDto=(UtilDto)Rlist.get(i);
if(PUtil.nullToStr(getValue()).equals(UDto.getId())){
out.println("<OPTION value="+UDto.getId()+" selected>"+UDto.getName()+"</OPTION>");
continue;
}
out.println("<OPTION value="+UDto.getId()+">"+UDto.getName()+"</OPTION>");
}
}
out.println("</SELECT>");
}
catch(Exception ex){
throw new JspException("IOException"+ex.toString());
}
return super.doEndTag();
}
}
/****************************
TagUtil.RoleList------------取Role列表数据库操作
package com.dd.struts.util;
import java.util.ArrayList;
import java.util.List;
import cn.gov.core.server.pm.IPersistenceManager;
import cn.gov.core.server.pm.PersistenceException;
import cn.gov.core.server.pm.util.PersistenceUtil;
import com.ewansoft.struts.utils.Constants;
public class TagUtil {
/*
* 取角色字典getRoleList
*/
public static List RoleList() {
IPersistenceManager pm = PersistenceUtil.getPM(Constants.DATA_SOURCE);
try {
//取得Role字典表列表
return pm.queryForList("RoleList" , null);
} catch (PersistenceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ArrayList();
}
}
/*******************************
提取RoleList字典列表sql语句 -------XML配置
---------------------------------TestTag.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Dict">
<resultMap id="dto-result" class="com.dd.struts.dto.UtilDto">
<result column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
<select id="RoleList" resultMap="dto-result">
select id,name from ctwob.002
</select>
</sqlMap>
/*****************************
组合标签的应用Test.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>Test</shortname>
<uri>http://dd.com/struts/tags-testtag</uri>
<info>
This tag library contains functionality for the Addressbook Struts
Sample Application. With small modifications, they can be used
as generic tags.
</info>
<tag>
<name>Role</name>
<tagclass>com.dd.struts.util.RoleTag</tagclass>
<bodycontent>empty</bodycontent>
<info>
role dict
</info>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
/******************************
/
大结局自定义的标签应用
<%@ taglib uri="http://dd.com/struts/tags-testtag" prefix="Test"%>
/*********************************
应用如下:<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://dd.com/struts/tags-testtag" prefix="Test"%>
<html>
<head>
<title>Insert DataBase</title>
</head>
<body>
<html:form action="/insert">
userName : <html:text property="userName"/><html:errors property="userName"/><br/>
password : <html:text property="password"/><html:errors property="password"/><br/>
role_id :<Test:Role name="role_id" value="${role_id}"/><br>
<app:area/>
<html:submit/>
</html:form>
</body>
</html>
---------------------------
这是本人从制作到应用--------页面数据到数据库得到数据全过程----------测试成功
--------------------------