这里我正在触发一些查询..它显示记录但是我想要的是如果没有记录那么它应该显示“找不到匹配/结果”。
我在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";
}
}
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>
任何人都请帮助我:(
答案 0 :(得分:1)
您可以使用struts2的<s:if>
和<s:else>
标签来检查列表是否为空,例如
<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>
标记。希望这会有所帮助。