日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用displaytag實現(xiàn)數(shù)據(jù)庫分頁(displaytag數(shù)據(jù)庫分頁)

在軟件開發(fā)中,經(jīng)常需要對數(shù)據(jù)庫進(jìn)行分頁操作,以便在頁面上顯示較大數(shù)據(jù)集的一部分,這樣可以提高頁面加載速度,并且也有助于減輕服務(wù)器的壓力。 displaytag是一種在Java應(yīng)用程序中實現(xiàn)分頁顯示數(shù)據(jù)的流行工具。在本文中,我們將介紹如何使用displaytag來實現(xiàn)數(shù)據(jù)庫分頁。

步驟1:使用Eclipse創(chuàng)建Java Web應(yīng)用程序

我們需要創(chuàng)建一個Java Web應(yīng)用程序,在Eclipse中可以輕松實現(xiàn)。在Eclipse菜單中選擇File -> New -> Other -> Web -> Dynamic Web Project,然后提供一個項目名稱和目標(biāo)運行時環(huán)境(比如Tomcat),單擊下一步。

在項目設(shè)置對話框中,選擇Target Runtime(如Apache Tomcat v9.0),并選擇Dynamic web module版本(如4.0),接著點擊下一步。

在Web module設(shè)置對話框中,設(shè)置上下文根(這是Web應(yīng)用程序在Tomcat中URL地址)及頁面存儲目錄。單擊完成,我們已成功創(chuàng)建了Java Web應(yīng)用程序。

步驟2:連接到數(shù)據(jù)庫

為了,我們需要先連接到數(shù)據(jù)庫。在我們的示例代碼中,我們將連接到MySQL數(shù)據(jù)庫。因此,我們需要在Web應(yīng)用程序中的lib目錄中添加MySQL數(shù)據(jù)庫連接器(mysql-connector-java-8.0.11.jar或更高版本)。然后,我們需要在項目的web.xml文件中添加以下配置:

“`

DB Connection

jdbc/dbconn

javax.sql.DataSource

Contner

DB Connection

jdbc/dbconn

javax.sql.DataSource

“`

接下來,我們就需要在Tomcat的conf目錄的context.xml文件中添加以下代碼:

“`

auth=”Contner”

type=”javax.sql.DataSource”

username=”USERNAME”

password=”PASSWORD”

driverClassName=”com.mysql.jdbc.Driver”

url=”jdbc:mysql://localhost:3306/DBNAME”/>

“`

在這里,我們需要將USERNAME和PASSWORD替換為數(shù)據(jù)庫的用戶名和密碼,同時將DBNAME替換為數(shù)據(jù)庫名稱。

步驟3:創(chuàng)建數(shù)據(jù)庫表并添加數(shù)據(jù)

在我們的示例代碼中,我們將創(chuàng)建一個表,名稱為movies,表結(jié)構(gòu)如下:

“`

CREATE TABLE movies (

id INT NOT NULL AUTO_INCREMENT,

title VARCHAR(255) NOT NULL,

director VARCHAR(255) NOT NULL,

release_year INT NOT NULL,

PRIMARY KEY (id)

);

“`

接著,我們將添加一些測試數(shù)據(jù):

“`

INSERT INTO movies (title, director, release_year) VALUES (“The Shawshank Redemption”, “Frank Darabont”, 1994);

INSERT INTO movies (title, director, release_year) VALUES (“The Godfather”, “Francis Ford Coppola”, 1972);

INSERT INTO movies (title, director, release_year) VALUES (“The Godfather: Part II”, “Francis Ford Coppola”, 1974);

INSERT INTO movies (title, director, release_year) VALUES (“The Dark Knight”, “Christopher Nolan”, 2023);

INSERT INTO movies (title, director, release_year) VALUES (“12 Angry Men”, “Sidney Lumet”, 1957);

INSERT INTO movies (title, director, release_year) VALUES (“Schindler’s List”, “Steven Spielberg”, 1993);

INSERT INTO movies (title, director, release_year) VALUES (“Pulp Fiction”, “Quentin Tarantino”, 1994);

INSERT INTO movies (title, director, release_year) VALUES (“The Lord of the Rings: The Fellowship of the Ring”, “Peter Jackson”, 2023);

INSERT INTO movies (title, director, release_year) VALUES (“Forrest Gump”, “Robert Zemeckis”, 1994);

INSERT INTO movies (title, director, release_year) VALUES (“The Empire Strikes Back”, “Irvin Kershner”, 1980);

INSERT INTO movies (title, director, release_year) VALUES (“The Matrix”, “The Wachowski Brothers”, 1999);

“`

步驟4:創(chuàng)建Movie實體類

為了將數(shù)據(jù)庫表中的數(shù)據(jù)顯示在Web應(yīng)用程序中,我們需要創(chuàng)建Movie實體類。該類的代碼如下:

“`

public class Movie {

private int id;

private String title;

private String director;

private int releaseYear;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getDirector() {

return director;

}

public void setDirector(String director) {

this.director = director;

}

public int getReleaseYear() {

return releaseYear;

}

public void setReleaseYear(int releaseYear) {

this.releaseYear = releaseYear;

}

}

“`

步驟5:編寫MovieDao類

接下來,我們需要創(chuàng)建一個MovieDao類,該類用于從數(shù)據(jù)庫中獲取Movie實例。類的代碼如下:

“`

public class MovieDao {

private Connection con;

public MovieDao() {

try {

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup(“java:/comp/env/jdbc/dbconn”);

con = ds.getConnection();

}

catch (NamingException | SQLException e) {

e.printStackTrace();

}

}

public List getMovies(int startIndex, int length) throws SQLException {

List list = new ArrayList();

String sql = “SELECT * FROM movies LIMIT ?, ?”;

PreparedStatement stmt = con.prepareStatement(sql);

stmt.setInt(1, startIndex);

stmt.setInt(2, length);

ResultSet rs = stmt.executeQuery();

while (rs.next()) {

Movie m = new Movie();

m.setId(rs.getInt(“id”));

m.setTitle(rs.getString(“title”));

m.setDirector(rs.getString(“director”));

m.setReleaseYear(rs.getInt(“release_year”));

list.add(m);

}

rs.close();

stmt.close();

return list;

}

public int getMoviesCount() throws SQLException {

String sql = “SELECT COUNT(*) FROM movies”;

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(sql);

rs.next();

int count = rs.getInt(1);

rs.close();

stmt.close();

return count;

}

}

“`

在這個類中,我們首先通過JNDI連接到我們的MySQL數(shù)據(jù)庫。然后,我們編寫getMovies和getMoviesCount方法,用于從movies表中獲取電影數(shù)據(jù)和電影數(shù)量數(shù)據(jù)。

步驟6:編寫Movies.jsp頁面

我們需要編寫Movies.jsp頁面,并使用displaytag來實現(xiàn)數(shù)據(jù)庫分頁。頁面的代碼如下:

“`

List of Movies

List of Movies

<%

try {

int currentPage = request.getParameter(“page”) != null ? Integer.parseInt(request.getParameter(“page”)) : 1;

int startIndex = (currentPage – 1) * 5;

MovieDao dao = new MovieDao();

List movies = dao.getMovies(startIndex, 5);

int totalRows = dao.getMoviesCount();

pageContext.setAttribute(“movies”, movies);

pageContext.setAttribute(“totalRows”, totalRows);

}

catch (SQLException e) {

e.printStackTrace();

}

%>

“`

在這個頁面中,我們首先導(dǎo)入了MovieDao和Movie實體類。然后,我們使用displaytag來顯示movies表中的電影數(shù)據(jù)。我們設(shè)置了pagesize為5,表示每頁顯示5條數(shù)據(jù)。在表格之下,我們使用display:pagination來顯示分頁導(dǎo)航欄。

在頁面上方,我們使用P腳本并調(diào)用MovieDao來獲取電影數(shù)據(jù)。我們首先嘗試從請求參數(shù)中獲取當(dāng)前頁數(shù),如果為空,則默認(rèn)為之一頁。我們設(shè)置startIndex為每頁顯示5條數(shù)據(jù)的起始索引。然后,我們調(diào)用MovieDao的getMovies和getMoviesCount方法,分別獲取movies表中的電影數(shù)據(jù)和電影數(shù)量數(shù)據(jù)。我們使用pageContext.setAttribute方法將數(shù)據(jù)存儲在pageContext中,以便在displaytag表格中使用。

結(jié)論

成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián)為您提供網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù)!

struts分頁顯示問題

建議LZ換中仿隱思路,采用displayTag標(biāo)簽來實現(xiàn)吧

你可以先嘗試下

這上面的小程序案例,你會發(fā)現(xiàn)displayTag標(biāo)櫻猜簽很強備頌廳大…

把你的值方在request 或塵毀者session屬性帆陪中 action里 用 getAttribute()方法

百度的分頁派轎備就是問號傳參的

displaytag 數(shù)據(jù)庫分頁的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于displaytag 數(shù)據(jù)庫分頁,使用displaytag實現(xiàn)數(shù)據(jù)庫分頁,struts分頁顯示問題的信息別忘了在本站進(jìn)行查找喔。

香港云服務(wù)器機房,創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)云服務(wù)器廠商,回大陸優(yōu)化帶寬,安全/穩(wěn)定/低延遲.創(chuàng)新互聯(lián)助力企業(yè)出海業(yè)務(wù),提供一站式解決方案。香港服務(wù)器-免備案低延遲-雙向CN2+BGP極速互訪!


網(wǎng)站標(biāo)題:使用displaytag實現(xiàn)數(shù)據(jù)庫分頁(displaytag數(shù)據(jù)庫分頁)
本文來源:http://www.dlmjj.cn/article/djooddc.html