开发Struts 2 自定义标签三步曲:
1.定义标签
public class AddressTag extends ComponentTagSupport {
//定义你JSP页面传来的标签设置的属性
private String id;
//此方法就是获得一个基本类的Component对象(需自定义生成)
@Override
public Component getBean(ValueStack valueStack, HttpServletRequest request, HttpServletResponse response) {AddressComponent addressComponent=new AddressComponent(valueStack, request);
..................................
return addressComponent;
}
}
2.创建Component子类
public class AddressComponent extends Component {
private String id;
//重写start方法 通过writer输出字符串。
public boolean start(Writer writer) {
boolean result = super.start(writer);
//从值栈中获取属性值
String id = getStack().findString(id);
writer.write("<div>"+id+"Hello World"+"</div>");
return result;
}
}
3.建.tld 文件
12 2.2.3 31.2 4my 5/address 6"jason time tab" 78 17address 9com.AddressTag 10empty 1112 16id 13false 14true 15
OK, 页面打开时,load入<%@ taglib prefix="" uri="/WEB-INF/tlds/address.tld" %>;
<w:address id="XXXX"/>