一.display tag
DisplayTag是一个非常好用的表格显示标签,适合MVC模式。可以对的Table进行分页、数据导出、分组、对列排序等.下面将用Struts2+display tag做个最简单的分页程序.
1.首先要下它的jar包,将jar包放到WEB-INF的lib文件夹下它的核心jar包是jstl-1.2.jar,另外需要一些辅助jar包,这些辅助包都有不同的功能,具体的功能可以访问http://displaytag.sourceforge.net/10/dependencies.html ,根据需要下载不同的jar包。例子中用到jar包如下:
commons-logging-1.0.4.jar
displaytag-1.1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
commons-lang-2.3.jar
standard-1.1.2.jar
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
jstl-1.2.jar
itext-1.3.jar
commons-digester-1.7.jar
2.然后在web.xml下添加一个filter
<filter>
<filter-name>exportFilter</filter-name>
<filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
在jsp页面做一个引用:
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display" %>
3.在src下面com.hua.example先建一个bean
package com.hua.example;
public class StudentInfo {
private String name;
private String age;
private String mark;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
}
建action
package com.hua.example;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport{
private List<StudentInfo>studentInfos;
public List<StudentInfo> getStudentInfos() {
return studentInfos;
}
public void setStudentInfos(List<StudentInfo> studentInfos) {
this.studentInfos = studentInfos;
}
public String dispayAllStudents()
{
//定义一个list存放要显示的记录
studentInfos=new ArrayList<StudentInfo>();
for(int i=0;i<50;i++)
{
StudentInfo studentInfo=new StudentInfo();
studentInfo.setName("jian"+i);
studentInfo.setAge("jian"+i);
studentInfo.setMark("jian"+i);
studentInfos.add(studentInfo);
} return SUCCESS;
}
}
4.配置struts.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<display-name>Struts 2.0 Hello World</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- display tag 配置的过滤器 -->
<filter>
<filter-name>exportFilter</filter-name>
<filter-class>
org.displaytag.filter.ResponseOverrideFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<display-name>Struts 2.0 Hello World</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- display tag 配置的过滤器 -->
<filter>
<filter-name>exportFilter</filter-name>
<filter-class>
org.displaytag.filter.ResponseOverrideFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.相应的页面如下index.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri=" http://displaytag.sf.net " prefix="display" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Student Message</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<a href="/displaytagexample/studentInfo.action">点击查看display tag分页的简单例子</a>
</body>
</html>
studentinfo.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net/el " prefix="display"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Student Message</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<!-- name 是action的list 如果你把他放在session中了就不用写requestURI 否则就一定要写。requestURI值action的路径 -->
<display:table name="studentInfos" pagesize="10"
requestURI="studentInfo.action">
<display:column property="name" title="姓名" />
<display:column property="age" title="年龄" />
<display:column property="mark" title="分数" />
</display:table>
</body>
</html>
运行后的效果如下:
[First/Prev] 1 , 2 , 3 , 4 , 5 [Next /Last ]
姓名 | 年龄 | 分数 |
---|---|---|
jian0 | jian0 | jian0 |
jian1 | jian1 | jian1 |
jian2 | jian2 | jian2 |
jian3 | jian3 | jian3 |
jian4 | jian4 | jian4 |
jian5 | jian5 | jian5 |
jian6 | jian6 | jian6 |
jian7 | jian7 | jian7 |
jian8 | jian8 | jian8 |
jian9 | jian9 | jian9 |
显示的格式自己可以定义CSS在studentinfo.jsp加上
<style type="text/css">
.table {
border: 1px solid #74B3DC;
color: #000;
background: #fff;
width: 99.7% !important;
width: 99.5%;
}
.table td,.table th{
border: 1px solid #e0e0e0;
border-left: 0px;
border-top: 0px;
padding: 0.2em;
}
.table thead th{
border: 1px solid #e0e0e0;
border-left: 0px;
border-top: 0px;
text-align: left;
font-size: 1em;
font-weight: bold;
background: #d7e9f5;
}
}
</style>
在<display:table name="studentInfos" pagesize="10"
requestURI="studentInfo.action" class="table" >加上引用
最后显示的效果如下
下面是display tag 其他的一些用法和功能
http://hi.baidu.com/netnotes/blog/item/d6527d121bef1850f919b878.html