在用struts2写自定义标签时遇到了一些问题,说了一个多小时解决了,嗯就在这里做个备忘吧!
(第一次写,,呵呵,有点怕出错,如果说的不好大家不要笑..呵呵)
说明一下我的环境:eclipse+tomcat
自定义标签编写分为以下几步:
1.编写UIBean:
2.编写tag类:
AbstractUITag这个类要用到JSP-API,我大部分的时间都是浪费在这里,
这地方主要问题在于分清开发环境与运行环境.
我的包都是放在WEB-INF/lib下地.所以这里是运行环境,同时也是开发环境.
这里不要放入jsp-api.jar.tomcat下面lib文件夹中有.这样jsp-api.jar只需要在开发时
引用这个包,而不要把jsp-api.jar放入WEB-INF/lib下.
3.tld文件(只是重要部分.)
这个文件我是放在这个目录中的 WEB-INF/tld/user-tags.tld
4.建立mymail.ftl文件.
文件放置在下面这个目录中
webroot/template/xhtml
webroot是我项目的根目录,
template不变,
xhtml是当前你使的哪种theme,如果是simple你就建simple文件夹.跟据自身情况定.
My E-mail:${parameters.address?html}
5配置web.xml文件
6.action类
7.建立jsp文件
struts2.xml就不写了.
(第一次写,,呵呵,有点怕出错,如果说的不好大家不要笑..呵呵)
说明一下我的环境:eclipse+tomcat
自定义标签编写分为以下几步:
1.编写UIBean:
- @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下.
- 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
- <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文件
- <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类
- 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文件
- <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就不写了.