Struts2自定义标签

自定义标签编写分为以下几步:
1.编写UIBean:
Java代码

@StrutsTag(description = "my email", name = "email", tldTagClass = "org.oa.web.tags.MyMailTag")
public class MyMail extends UIBean {
public static final String TEMPLATE_NAME = "mymail";

private String address;

public MyMail(ValueStack stack, HttpServletRequest request,
HttpServletResponse response) {
super(stack, request, response);
}

@StrutsTagAttribute(description = "set e-mail address ", type = "String")
public void setAddress(String address) {
this.address = address;
}

@Override
protected String getDefaultTemplate() {
return TEMPLATE_NAME;
}

@Override
protected void evaluateExtraParams() {
super.evaluateExtraParams();
if (address != null) {
addParameter("address", findString(address));
}
}

}


@StrutsTag(description = "my email", name = "email", tldTagClass = "org.oa.web.tags.MyMailTag")
public class MyMail extends UIBean {
public static final String TEMPLATE_NAME = "mymail";

private String address;

public MyMail(ValueStack stack, HttpServletRequest request,
HttpServletResponse response) {
super(stack, request, response);
}

@StrutsTagAttribute(description = "set e-mail address ", type = "String")
public void setAddress(String address) {
this.address = address;
}

@Override
protected String getDefaultTemplate() {
return TEMPLATE_NAME;
}

@Override
protected void evaluateExtraParams() {
super.evaluateExtraParams();
if (address != null) {
addParameter("address", findString(address));
}
}

}


2.编写tag类:
AbstractUITag这个类要用到JSP-API,我大部分的时间都是浪费在这里,
这地方主要问题在于分清开发环境与运行环境.
我的包都是放在WEB-INF/lib下地.所以这里是运行环境,同时也是开发环境.
这里不要放入jsp-api.jar.tomcat下面lib文件夹中有.这样jsp-api.jar只需要在开发时
引用这个包,而不要把jsp-api.jar放入WEB-INF/lib下.

Java代码
public class MyMailTag extends AbstractUITag {

private static final long serialVersionUID = 1L;
private String address;

@Override
public Component getBean(ValueStack stack, HttpServletRequest req,
HttpServletResponse res) {

return new MyMail(stack, req, res);
}

@Override
protected void populateParams() {
super.populateParams();
MyMail mail = (MyMail) component;
mail.setAddress(address);
}

public void setAddress(String address) {
this.address = address;
}

}

public class MyMailTag extends AbstractUITag {

private static final long serialVersionUID = 1L;
private String address;

@Override
public Component getBean(ValueStack stack, HttpServletRequest req,
HttpServletResponse res) {

return new MyMail(stack, req, res);
}

@Override
protected void populateParams() {
super.populateParams();
MyMail mail = (MyMail) component;
mail.setAddress(address);
}

public void setAddress(String address) {
this.address = address;
}

}


3.tld文件(只是重要部分.)
这个文件我是放在这个目录中的 WEB-INF/tld/user-tags.tld
Xml代码
<tlib-version>1.10</tlib-version>
<short-name>u</short-name>
<uri>/user-tags</uri>
<tag>
<name>email</name>
<tag-class>org.oa.web.tags.MyMailTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>address</name>
<required>true</required>
</attribute>
</tag>

<tlib-version>1.10</tlib-version>
<short-name>u</short-name>
<uri>/user-tags</uri>
<tag>
<name>email</name>
<tag-class>org.oa.web.tags.MyMailTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>address</name>
<required>true</required>
</attribute>
</tag>


4.建立mymail.ftl文件.
文件放置在下面这个目录中
webroot/template/xhtml
webroot是我项目的根目录,
template不变,
xhtml是当前你使的哪种theme,如果是simple你就建simple文件夹.跟据自身情况定.

My E-mail:${parameters.address?html}

5配置web.xml文件
Xml代码
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tld/user-tags.tld</taglib-uri>
<taglib-location>/user-tags</taglib-location>
</taglib>
</jsp-config>

<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tld/user-tags.tld</taglib-uri>
<taglib-location>/user-tags</taglib-location>
</taglib>
</jsp-config>


6.action类
Java代码
public class MyEmailAction {


private String address;

@Override
public String execute() throws Exception {
setAddress("XXXXXXXXX@gmail.com");
return super.execute();
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}

public class MyEmailAction {


private String address;

@Override
public String execute() throws Exception {
setAddress("XXXXXXXXX@gmail.com");
return super.execute();
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}


7.建立jsp文件
Jsp代码
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ taglib prefix="u" uri="/user-tags" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Insert title here</title>
</head>
<body>
<h1>struts-tags:<s:property value="address"/></h1>
<h1>user-tags: <u:email address="%{address}"/></h1>
</body>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ taglib prefix="u" uri="/user-tags" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Insert title here</title>
</head>
<body>
<h1>struts-tags:<s:property value="address"/></h1>
<h1>user-tags: <u:email address="%{address}"/></h1>
</body>


struts2.xml就不写了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值