一、为什么要自定义struts2标签?
答:
struts2接受页面传值,不使用ActionFrom,而是,在Action里使用属性+get/set的方法传值,并且自动实现回写功能。
注意:我们用struts2时提交表单,传值是这么做的:
1》页面:使用struts2的标签<s:>将name设置为与Action的属性名字相同的
2》action写属性(名字与前台页面s标签的name完全相同)+get/set
3》什么也不做自动实现回写功能
那么,我们自己写的struts2标签:
1.标签的name属性和action的属性完全相同+get/set方法
2.这样,就可以自动传值过去了
二、注意:struts自定义的标签实现不了自动回写的功能(目前我的测试来说)
三、如何自定义struts2标签:
1.写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>userShow</short-name>
<uri>/show</uri>
<display-name>"user show"</display-name>
<tag>
<name>userShow</name>
<tag-class>cn.vit.auto.proto.utils.UserShowTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>role</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
2.写java实现类:
package cn.vit.auto.proto.utils;
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;
/**
*
* @author A
*
*/
public class UserShowTag extends ComponentTagSupport {
private static final long serialVersionUID = 1L;
private String role;
private String name;
@Override
public Component getBean(ValueStack stack, HttpServletRequest request,
HttpServletResponse response) {
return new UserShowHelper(stack, request);
}
protected void populateParams() {
super.populateParams();
UserShowHelper userShow = (UserShowHelper) component;
userShow.setRole(role);
userShow.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
package cn.vit.auto.proto.utils;
import java.io.Writer;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.components.Component;
import org.hibernate.Session;
import cn.vit.auto.proto.entity.User;
import cn.vit.auto.proto.service.UserService;
import com.opensymphony.xwork2.util.ValueStack;
/**
*
* @author Kang_Suwei
*
*/
public class UserShowHelper extends Component {
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private String role;
private String name;
private UserService userService = new UserService();
public UserShowHelper(ValueStack stack, HttpServletRequest request) {
super(stack);
this.request = request;
}
public UserShowHelper(ValueStack stack) {
super(stack);
}
@Override
public boolean start(Writer writer) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
boolean result = false;
try {
result = super.start(writer);
StringBuilder str = new StringBuilder();
if ("stores".equals(role)) {
List list = userService.listUserByRole(6, session);
str.append("<select name='" + name + "' id='user' >");
str.append("<option value='0'>");
str.append("");
str.append("</option>");
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
str.append("<option value='" + user.getId() + "'>");
str.append(user.getUserName());
str.append("</option>");
}
str.append("</select>");
writer.write(str.toString());
} else {
List list = userService.listUserByRole(6, session);
str.append("<select name='" + name + "' id='user' >");
str.append("<option value='0'>");
str.append("");
str.append("</option>");
for (int i = 0; i < list.size(); i++) {
User user = (User) list.get(i);
str.append("<option value='" + user.getId() + "'>");
str.append(user.getUserName());
str.append("</option>");
}
str.append("</select>");
writer.write(str.toString());
}
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}
return result;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.页面使用
<td><myTag:userShow name="fromUsers[${status.index}]" role="stores" /></td>
4.action
private List<Long> fromUsers;
get/set