jsp怎样实现动态表格

来源:互联网 发布:ubuntu安装fcitx五笔 编辑:程序博客网 时间:2024/04/30 17:40

jsp怎样实现动态表格

2014-03-16 10:44月恋vv  分类:JAVA相关 | 浏览 1156 次
 编程语言
我需要从数据库中读出数据,然后显示在页面上,我用的循环td的方法,但是这样很丑,当数据量很少的时候它排版很开,有没有比较好的方法显示动态数据?然后我希望每条数据有一个按钮,点击会把这条数据传值到另一个页面的,这里我用的方法是计数(计算点击的数据在表格中的位置,然后在新的页面中重新查询找值),我觉得这个方法不太好,希望指点, 谢谢!!

这是我前面做的,请大家毫不留情的批判,毕竟我是第一次做网页,如果有大神能教我每条记录加一个下拉框,就更好啦,

2014-03-16 11:52提问者采纳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Index Page</title>
<script type="text/javascript">
    function showBook(bookId) {
        window.location.href = "showBook.jsp?bookId="+bookId;
    }
</script>
</head>
<body>
    <%!
    //定义Book类,实际程序中应定义在另一个文件中。
    public class Book {
 
        public Book() {
        }
 
        public Book(Long id, String name, String author) {
            this.id = id;
            this.name = name;
            this.author = author;
        }
 
        private Long id;
        private String name;
        private String author;
 
        public Long getId() {
            return id;
        }
 
        public void setId(Long id) {
            this.id = id;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
 
        public String getAuthor() {
            return author;
        }
 
        public void setAuthor(String author) {
            this.author = author;
        }
 
    }
    %>
    <%
        //构造列表对象,实际程序中是从数据库读取的信息。
        List<Book> books = new ArrayList();
        books.add(new Book(1L, "三国演义", "罗贯中"));
        books.add(new Book(2L, "水浒传", "施耐庵"));
        books.add(new Book(3L, "西游记", "吴承恩"));
        books.add(new Book(4L, "红楼梦", "曹雪芹"));
    %>
    <table border="1">
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
        <%
            for (Book book : books) {
        %>
        <tr>
            <td><%=book.getId()%></td>
            <td><%=book.getName()%></td>
            <td><%=book.getAuthor()%></td>
            <td><input type="button" value="详细信息" onclick="showBook(<%=book.getId()%>)" /></td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>

 最后生成的页面源文件是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Index Page</title>
<script type="text/javascript">
    function showBook(bookId) {
        window.location.href = "showBook.jsp?bookId="+bookId;
    }
</script>
</head>
<body>
     
     
    <table border="1">
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
         
        <tr>
            <td>1</td>
            <td>三国演义</td>
            <td>罗贯中</td>
            <td><input type="button" value="详细信息" onclick="showBook(1)" /></td>
        </tr>
         
        <tr>
            <td>2</td>
            <td>水浒传</td>
            <td>施耐庵</td>
            <td><input type="button" value="详细信息" onclick="showBook(2)" /></td>
        </tr>
         
        <tr>
            <td>3</td>
            <td>西游记</td>
            <td>吴承恩</td>
            <td><input type="button" value="详细信息" onclick="showBook(3)" /></td>
        </tr>
         
        <tr>
            <td>4</td>
            <td>红楼梦</td>
            <td>曹雪芹</td>
            <td><input type="button" value="详细信息" onclick="showBook(4)" /></td>
        </tr>
         
    </table>
</body>
</html>

 效果类似于这样

提问者评价
非常感谢,这对我太有帮助了
评论 | 4 2

fuhaiwei   | 十一级 采纳率73%

擅长: JAVA相关 JavaScript Html/Css 数据结构及算法 C/C++

为您推荐:

按默认排序 | 按时间排序

其他2条回答

2014-03-16 11:162yisam | 八级
下拉框还不简单?直接用<select><option>下拉列表</option></select>

循环列表时候用<table><tr><td>内容</td></tr></table>,table里可以动态设置单元里面的间隙、单元格之间的距离、边框、用CSS继续可以设置单元格的长宽高、是overflows时是否需要换行、背景颜色、边框属性等等
追问:
这个我知道,我就是对动态记录的传值有些疑惑,下拉框也是根据记录动态生成的,然后下拉框的值也是从数据库中读取。
追答:
哈?这有什么问题,传过来,你就接收就好啦。比如你传了一个List值(命名是tlist)过来,放在request Attribute里,获取就用<s:select value="#request.tlist">就行啦。不会用struts标签,那就用最笨的方法,循环写在select表情的<option>值1</option><option>值2</option>之间就可以啦,默认值(也就是你数据库取出的值)就在通过<%%>循环时比较,在<option selected="selected">就OK啦?很难吗?很难的话,重新学一下html语言和jsp吧。
0 0