声明标签
\WebRoot\WEB-INF\tangs.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>/tangs</uri>
<display-name>"Tangs Tags"</display-name>
<tag>
<name>button</name>
<tag-class>com.tag.button.ButtonTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
定义标签接受到的值
\com\tag\button\Button.java
package com.tag.button;
public class Button {
private String parent;
public Button() {
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
}
处理标签
\com\tag\button\Buttons.java
package com.tag.button;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.struts2.components.Component;
import com.opensymphony.xwork2.util.ValueStack;
public class Buttons extends Component {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Buttons(ValueStack arg0) {
super(arg0);
}
@Override
public boolean start(Writer arg0) {
boolean result = super.start(arg0);
try {
StringBuilder str = new StringBuilder();
boolean isValid = true;
Button bt = (Button) this.getStack().findValue(name);
isValid = bt != null ? true : false;
if (isValid) {
str.append("<input type=\"submit\" value=\"web.button.query\" method=\"search\"/>");
}
arg0.write(str.toString());
} catch (IOException ex) {
Logger.getLogger(Trees.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
}
调用标签
\
com\tag\button\ButtonTag.java
package com.tag.button;
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;
public class ButtonTag extends ComponentTagSupport {
private static final long serialVersionUID = 1L;
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public Component getBean(ValueStack arg0, HttpServletRequest arg1,
HttpServletResponse arg2) {
return new Buttons(arg0);
}
protected void populateParams() {
super.populateParams();
Buttons buttons = (Buttons) component;
buttons.setName(name);
}
}
测试
\
com\tag\button\ButtonAction.java
package com.tag.button;
import com.opensymphony.xwork2.ActionSupport;
public class ButtonAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Button names;
public Button getNames() {
return names;
}
public void setNames(Button names) {
this.names = names;
}
@Override
public String execute() throws Exception {
Button btn = new Button();
btn.setParent("1");
names = btn;
return SUCCESS;
}
}
备忘
接受对象数组的处理:
private Tree[] names;
public Tree[] getNames() {
return names;
}
public void setNames(Tree[] names) {
this.names = names;
}
@Override
public String execute() throws Exception {
Tree[] trees = new Tree[5];
names = trees;
return SUCCESS;
}
public boolean start(Writer arg0) {
boolean result = super.start(arg0);
try {
StringBuilder str = new StringBuilder();
boolean isValid = true;
Tree[] trees = (Tree[]) this.getStack().findValue(name);
isValid = trees != null ? true : false;
if (isValid) {
str.append("");
}
arg0.write(str.toString());
} catch (IOException ex) {
Logger.getLogger(Trees.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
jsp页面的调用:
<%@taglib uri="/tangs" prefix="tangs"%>
<html>
<body>
<tangs:button name="names"/>
</body>
</html>
没时间详细写了,不过应该看得懂。
posted on 2008-11-06 18:48
Jarry 阅读(7082)
评论(3) 编辑 收藏 所属分类:
Struts2.x