gpt4 book ai didi

javascript - struts2中如何使用arraylist显示 "no result found"

转载 作者:行者123 更新时间:2023-12-03 09:37:45 26 4
gpt4 key购买 nike

这里我正在触发一些查询..它显示记录,但我想要的是如果没有记录,那么它应该显示“未找到匹配/结果”。
我在 Struts2 中使用 ArrayList 。那么我能实现它吗?如果有人可以进行更改或提出一些非常有帮助的建议,请提供帮助。我已经为此搜索了解决方案,但无法获得相关的解决方案。下面我发布了我想要显示结果的 Action 类和 jsp 页面。
提前致谢。 :)

BookSearchAction.java

package org.entity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class BookSearchAction extends ActionSupport {

Book book;

List<Book> bookResultList;

public Book getBook() {
return book;
}

public void setBook(Book book) {
this.book = book;
}

public List<Book> getBookResultList() {
return bookResultList;
}

public void setBookResultList(List<Book> bookResultList) {
this.bookResultList = bookResultList;
}

public BookSearchAction() {
// TODO Auto-generated constructor stub
}

@Override
public String execute() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/staff", "root", "siddheshkk");
System.out.println("Driver Loaded");
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("select * from books12 where name like '%"
+ book.getBookName() + "%'");
bookResultList = new ArrayList<Book>();
while (rs.next()) {
bookResultList.add(new Book(rs.getString(1), rs.getInt(2)));
}

con.close();
} catch (Exception e) {
System.out.println(e);
}

return "success";
}

}

成功.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Results</title>
</head>
<body>
<h1>data retrieved successfully..</h1>
<h3>Here is the data: </h3><br>
<table border="2">
<th>
<tr><td>Book Name</td><td>Cost</td></tr>
</th>
<s:iterator value="bookResultList">
<s:iterator>
<tr>
<td><s:property value="bookName"/></td>
<td><s:property value="bookCost"/></td>
</tr>
</s:iterator>

</s:iterator>
</table>
</body>
</html>

任何人请帮助我:(

最佳答案

您可以使用<s:if><s:else> struts2的标签来检查列表是否为空,例如

<s:if test="%{bookResultList.size>0}">
<table>
<s:iterator value="bookResultList">
<tr>
<td><s:property value="bookName"/></td>
<td><s:property value="bookCost"/></td>
</tr>
</s:iterator>
</table>
</s:if>
<s:else>
<div> No data found</div>
</s:else>

如果是Struts1.x版本,可以使用<logic:present>标签。希望这会有所帮助。

关于javascript - struts2中如何使用arraylist显示 "no result found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31279378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com