如何显示"没有结果发现"使用数组列表中的struts2 [英] How to display "no result found" using arraylist in struts2
问题描述
在这里,我有些射击查询..和它显示的记录,但我想的是,如果没有记录,那么它应该显示没有找到匹配/结果。结果
我正在使用Struts2的ArrayList中。所以,我可以能够实现呢?如有任何人都可以进行更改或暗示的东西,将是非常有益的。我已经寻找解决方案这一点,但没能得到相关的解决方案。下面我张贴我的Action类和JSP页面上,我想显示结果。结果
提前致谢。 :)
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
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
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 :(
推荐答案
您可以使用&LT; S:如果&GT;
和&LT; S:别的&GT;
struts2的标签,检查列表是否为空或没有,例如:
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>
在情况下Struts1.x版本,可以使用&LT;逻辑:present&GT;
标记。希望这有助于。
In case Struts1.x version, you can use <logic:present>
tag. Hope this helps.
这篇关于如何显示&QUOT;没有结果发现&QUOT;使用数组列表中的struts2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!