写个自定义按纽的组件,给大家可以参考一下:
第一步:
package cn.jsf.fgliu
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
public class NewButtonTag extends UIComponentTag {
private String name;
private String style;
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getComponentType() {
// TODO 自动生成方法存根
return "NewButton";
}
public String getRendererType() {
// TODO 自动生成方法存根
return null;
}
protected void setProperties(UIComponent component){
super.setProperties(component);
NewButton button = (NewButton) component;
if (name != null) {
button.setName(name);
}
if(style!=null){
button.setStyle(style);
}
if(value!=null)
button.setMyValue(value);
}
}
第二步:
package cn.jsf.fgliu
import 编程.io.IOException;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class NewButton extends UIOutput {
private String name;
private String style;
private String myValue;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
protected String getComponentType() {
return "NewButton";
}
public void encodeEnd(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.write("<input type='button' name='"+ this.getName()+"' value = '"+ this.getMyValue()+"' style='"+this.getStyle()+"'>");
}
public String getMyValue() {
return myValue;
}
public void setMyValue(String myValue) {
this.myValue = myValue;
}
}
第三步:
在WEB-INF/face-config.xml文件中加入该组件
<component>
<component-type>NewButton</component-type>
<component-class>cn.jsf.fgliu.NewButton</component-class>
</component>
第四步:
写tld文件
<taglib>
<tag>
<name>newButton</name>
<tag-class>cn.jsf.fgliu.NewButtonTag</tag-class>
<body-content>JSP</body-content>
<description>my first button</description>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
第五步:
在web.xml中加入
<taglib>
<taglib-uri>http://www.cn.jsf.fgliu/button</taglib-uri>
<taglib-location>/WEB-INF/button-tag.tld</taglib-location>
</taglib>
第六步:
jsp页面中使用:
<%@ taglib uri="http://www.cn.jsf.fgliu/button" prefix="fgliu"%>
<fgliu:newButton name="OK" value="ok"
style="position:absolute;top:100px;left:61px;height:21px;width:80px;" />
这样就可以了,更多的内容请大家再多多交流。
第一步:
package cn.jsf.fgliu
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
public class NewButtonTag extends UIComponentTag {
private String name;
private String style;
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getComponentType() {
// TODO 自动生成方法存根
return "NewButton";
}
public String getRendererType() {
// TODO 自动生成方法存根
return null;
}
protected void setProperties(UIComponent component){
super.setProperties(component);
NewButton button = (NewButton) component;
if (name != null) {
button.setName(name);
}
if(style!=null){
button.setStyle(style);
}
if(value!=null)
button.setMyValue(value);
}
}
第二步:
package cn.jsf.fgliu
import 编程.io.IOException;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
public class NewButton extends UIOutput {
private String name;
private String style;
private String myValue;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
protected String getComponentType() {
return "NewButton";
}
public void encodeEnd(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.write("<input type='button' name='"+ this.getName()+"' value = '"+ this.getMyValue()+"' style='"+this.getStyle()+"'>");
}
public String getMyValue() {
return myValue;
}
public void setMyValue(String myValue) {
this.myValue = myValue;
}
}
第三步:
在WEB-INF/face-config.xml文件中加入该组件
<component>
<component-type>NewButton</component-type>
<component-class>cn.jsf.fgliu.NewButton</component-class>
</component>
第四步:
写tld文件
<taglib>
<tag>
<name>newButton</name>
<tag-class>cn.jsf.fgliu.NewButtonTag</tag-class>
<body-content>JSP</body-content>
<description>my first button</description>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>style</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
第五步:
在web.xml中加入
<taglib>
<taglib-uri>http://www.cn.jsf.fgliu/button</taglib-uri>
<taglib-location>/WEB-INF/button-tag.tld</taglib-location>
</taglib>
第六步:
jsp页面中使用:
<%@ taglib uri="http://www.cn.jsf.fgliu/button" prefix="fgliu"%>
<fgliu:newButton name="OK" value="ok"
style="position:absolute;top:100px;left:61px;height:21px;width:80px;" />
这样就可以了,更多的内容请大家再多多交流。