问题
Here I am firing some query ..and it displays record but what i want is if there is no record then it should display "no match/result found" .
I am using ArrayList in Struts2 . So can i be able to achieve it ? Please if anyone can make changes or suggest something that would be very helpful. I have searched solution for this but wasn't able to get relevant solution. Below I am posting my Action class and jsp page on which i want to display results.
Thanks in advance. :)
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>
Anyone please help me :(
回答1:
You can use <s:if>
and <s:else>
tags of struts2 to check whether the list is empty or not, for example
<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>
In case Struts1.x version, you can use <logic:present>
tag. Hope this helps.
来源:https://stackoverflow.com/questions/31279378/how-to-display-no-result-found-using-arraylist-in-struts2