自定义struts2 tag

struts2 专栏收录该内容
11 篇文章 0 订阅

 

添加Bean对象

 

 

package com.show.tag.input;

import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author liuqing
 * 基础Tag类
 *
 */
public class HtmlInput extends Component {
	
	private String theme;
	
	private String id;
	
	private String width;
	
	private String height;
	
	private String name;
	
	private String cssStyle;
	
	private String value;
	
    private boolean isNumber;
	
	private int min;
	
	private int max;
	
	private int precision;
	
	private int minLength;
	
	private int maxLength;
	
	private boolean isString;
	
	private String onchange;

	public HtmlInput(ValueStack stack) {
		super(stack);
	}

	public HtmlInput(ValueStack stack, String width, String height,
			String name, String cssStyle) {
		super(stack);
		this.width = width;
		this.height = height;
		this.name = name;
		this.cssStyle = cssStyle;
	}
	
	public HtmlInput(ValueStack stack, String id, String width, String height,
			String name, String cssStyle, String value) {
		super(stack);
		this.id = id;
		this.width = width;
		this.height = height;
		this.name = name;
		this.cssStyle = cssStyle;
		this.value = value;
	}

	public HtmlInput(ValueStack stack, String theme, String id, String width,
			String height, String name, String cssStyle, String value) {
		super(stack);
		this.theme = theme;
		this.id = id;
		this.width = width;
		this.height = height;
		this.name = name;
		this.cssStyle = cssStyle;
		this.value = value;
	}

	/**
	 * @return the width
	 */
	public String getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public String getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(String height) {
		this.height = height;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the cssStyle
	 */
	public String getCssStyle() {
		return cssStyle;
	}

	/**
	 * @param cssStyle the cssStyle to set
	 */
	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}


	/**
	 * @return the value
	 */
	public String getValue() {
		return value;
	}


	/**
	 * @param value the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}


	/**
	 * @return the id
	 */
	public String getId() {
		if (this.id == null || "".equals(id)) {
			this.id = this.name;
		}
		return id;
	}


	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}


	/**
	 * @return the theme
	 */
	public String getTheme() {
		return theme;
	}


	/**
	 * @param theme the theme to set
	 */
	public void setTheme(String theme) {
		this.theme = theme;
	}

	/**
	 * @return the isNumber
	 */
	public boolean isNumber() {
		return isNumber;
	}

	/**
	 * @param isNumber the isNumber to set
	 */
	public void setNumber(boolean isNumber) {
		this.isNumber = isNumber;
	}

	/**
	 * @return the min
	 */
	public int getMin() {
		return min;
	}

	/**
	 * @param min the min to set
	 */
	public void setMin(int min) {
		this.min = min;
	}

	/**
	 * @return the max
	 */
	public int getMax() {
		return max;
	}

	/**
	 * @param max the max to set
	 */
	public void setMax(int max) {
		this.max = max;
	}

	/**
	 * @return the precision
	 */
	public int getPrecision() {
		return precision;
	}

	/**
	 * @param precision the precision to set
	 */
	public void setPrecision(int precision) {
		this.precision = precision;
	}

	/**
	 * @return the minLength
	 */
	public int getMinLength() {
		return minLength;
	}

	/**
	 * @param minLength the minLength to set
	 */
	public void setMinLength(int minLength) {
		this.minLength = minLength;
	}

	/**
	 * @return the maxLength
	 */
	public int getMaxLength() {
		return maxLength;
	}

	/**
	 * @param maxLength the maxLength to set
	 */
	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}

	/**
	 * @return the isString
	 */
	public boolean isString() {
		return isString;
	}

	/**
	 * @param isString the isString to set
	 */
	public void setString(boolean isString) {
		this.isString = isString;
	}

	/**
	 * @return the onchange
	 */
	public String getOnchange() {
		return onchange;
	}

	/**
	 * @param onchange the onchange to set
	 */
	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}
	
}
 

 

 

 

package com.show.tag.input;

import java.io.IOException;
import java.io.Writer;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author liuqing
 * @version 1.0
 * 2011-8-24
 *
 */
public class DateBox extends HtmlInput {
	
	private String theme;
	
	private int inputNumber;
	
	private String match;
	
	private String split;
	
	private String align;
	
	
	public DateBox(ValueStack stack, String id,String width,String height,
			String name,String cssStyle,String value,int inputNumber, String match,
			String split, String align,String theme,boolean isNumber,int min
			,int max,int precision,int minLength,int maxLength,boolean isString
			,String onchange) {
		super(stack,id,width,height,name,cssStyle,value);
		this.inputNumber = inputNumber;
		this.match = match;
		this.split = split;
		this.align = align;
		this.theme = theme;
		this.setNumber(isNumber);
		this.setMin(min);
		this.setMax(max);
		this.setPrecision(precision);
		this.setMinLength(minLength);
		this.setMaxLength(maxLength);
		this.setString(isString);
		this.setOnchange(onchange);
	}

	public DateBox(ValueStack stack) {
		super(stack);
	}

	/* (non-Javadoc)
	 * @see org.apache.struts2.components.Component#start(java.io.Writer)
	 */
	@Override
	public boolean start(Writer writer) {
		StringBuffer buf = new StringBuffer("<div style=\"margin:2px;text-align:" +
				this.align +
				";\"><input name=\"");
		buf.append(this.getName())
		.append("\" type=\"text\" class=\"easyui-datebox\" ")
		.append("style=\"width:").append(width(this.getWidth())).append("px;")
		.append(" height:").append(height(this.getHeight())).append("px;");
		String stackValue = stack.findString(this.getName());
		buf.append(this.getCssStyle()).append("\" ");
		if (stackValue != null && !"".equals(stackValue)
				//&& (this.getValue() == null || "".equals(this.getValue()))
				) {
			buf.append(" value=\"").append(stackValue).append(" \" />");
		}
		else {
			buf.append(" value=\"").append(this.getValue()).append("\" />");
		}
		try {
			writer.write(buf.toString());
			writer.flush();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		return super.start(writer);
	}



	/**
	 * @return the inputNumber
	 */
	public int getInputNumber() {
		return inputNumber;
	}

	/**
	 * @param inputNumber the inputNumber to set
	 */
	public void setInputNumber(int inputNumber) {
		this.inputNumber = inputNumber;
	}

	/**
	 * @return the match
	 */
	public String getMatch() {
		return match;
	}

	/**
	 * @param match the match to set
	 */
	public void setMatch(String match) {
		this.match = match;
	}

	/**
	 * @return the split
	 */
	public String getSplit() {
		return split;
	}

	/**
	 * @param split the split to set
	 */
	public void setSplit(String split) {
		this.split = split;
	}

	/**
	 * @return the align
	 */
	public String getAlign() {
		return align;
	}

	/**
	 * @param align the align to set
	 */
	public void setAlign(String align) {
		this.align = align;
	}

	/**
	 * @return the theme
	 */
	public String getTheme() {
		return theme;
	}

	/**
	 * @param theme the theme to set
	 */
	public void setTheme(String theme) {
		this.theme = theme;
	}
	
	private String width(String value) {
		if (value == null || "".equals(value) 
				|| value.length() == 0) {
			return "160";
		}
		return value;
	}
	private String height(String value) {
		if (value == null || "".equals(value) 
				|| value.length() == 0) {
			return "26";
		}
		return value;
	}
	
}
 

step 2:添加Tag 类注意struts2 valueStack

 

package com.show.tag.input;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ComponentTagSupport;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * @author liuqing
 *
 */
public class DateBoxTag extends ComponentTagSupport {
	
    private String width = "";
	
	private String height = "";
	
	private String name = "";
	
	private String cssStyle = "";
	
	private String value = "";
	
    private boolean isNumber;
		
	private int min;
	
	private int max;
	
	private int precision;
	
	private int minLength;
	
	private int maxLength;
	
	private boolean isString;
	
	private String align = "";

	@Override
	public Component getBean(ValueStack value, HttpServletRequest request,
			HttpServletResponse response) {
		DateBox box = new DateBox(value);
		box.setWidth(width);
		box.setHeight(height);
		box.setName(name);
		box.setCssStyle(cssStyle);
		box.setValue(this.value);
		box.setAlign(align);
		return box;
	}

	/**
	 * @return the width
	 */
	public String getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public String getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(String height) {
		this.height = height;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the cssStyle
	 */
	public String getCssStyle() {
		return cssStyle;
	}

	/**
	 * @param cssStyle the cssStyle to set
	 */
	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}

	/**
	 * @return the value
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @param value the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the isNumber
	 */
	public boolean isNumber() {
		return isNumber;
	}

	/**
	 * @param isNumber the isNumber to set
	 */
	public void setNumber(boolean isNumber) {
		this.isNumber = isNumber;
	}

	/**
	 * @return the min
	 */
	public int getMin() {
		return min;
	}

	/**
	 * @param min the min to set
	 */
	public void setMin(int min) {
		this.min = min;
	}

	/**
	 * @return the max
	 */
	public int getMax() {
		return max;
	}

	/**
	 * @param max the max to set
	 */
	public void setMax(int max) {
		this.max = max;
	}

	/**
	 * @return the precision
	 */
	public int getPrecision() {
		return precision;
	}

	/**
	 * @param precision the precision to set
	 */
	public void setPrecision(int precision) {
		this.precision = precision;
	}

	/**
	 * @return the minLength
	 */
	public int getMinLength() {
		return minLength;
	}

	/**
	 * @param minLength the minLength to set
	 */
	public void setMinLength(int minLength) {
		this.minLength = minLength;
	}

	/**
	 * @return the maxLength
	 */
	public int getMaxLength() {
		return maxLength;
	}

	/**
	 * @param maxLength the maxLength to set
	 */
	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}

	/**
	 * @return the isString
	 */
	public boolean isString() {
		return isString;
	}

	/**
	 * @param isString the isString to set
	 */
	public void setString(boolean isString) {
		this.isString = isString;
	}

	/**
	 * @return the align
	 */
	public String getAlign() {
		return align;
	}

	/**
	 * @param align the align to set
	 */
	public void setAlign(String align) {
		this.align = align;
	}
	
}
 

 

 step 3 写tld 文件

 

  <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>2.2.3</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>tangs</short-name>
    <uri>/struts-tags-zk</uri>
    <display-name>"Tangs Tags"</display-name>
    <tag>
        <name>toolbar</name>
        <tag-class>com.show.toolbar.OperationTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>addOnclick</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>addHref</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>saveOnclick</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>saveHref</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>removeOnclick</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>removeHref</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>printOnclick</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>printHref</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>searchOnclick</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>searchHref</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>backOnclick</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>backHref</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>inputUnderline</name>
        <tag-class>com.show.tag.input.UnderLineTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>inputNoneLine</name>
        <tag-class>com.show.tag.input.NoneLineTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>dateBox</name>
        <tag-class>com.show.tag.input.DateBoxTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>align</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>datetimeBox</name>
        <tag-class>com.show.tag.input.DatetimeBoxTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>align</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>inputList</name>
        <tag-class>com.show.tag.input.ListInputTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>id</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>align</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>inputNumber</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>match</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>split</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>theme</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>useNumber</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>min</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>max</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>precision</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>minLength</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>maxLength</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>useString</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>onchange</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    
    <tag>
        <name>textArea</name>
        <tag-class>com.show.tag.input.TextAreaInputTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>id</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>width</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        
        <attribute>
            <name>height</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>cssStyle</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>align</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>match</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>split</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>theme</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>useNumber</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>min</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>max</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>precision</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>minLength</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>maxLength</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>useString</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>useBorder</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    
    
</taglib>
 

  <%@taglib prefix="sx" uri="/struts-tags-zk" %>

 

 <sx:dateBox width="300" align="right" name="startDate" />

  

 OK

 

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值