【问题标题】:How to display "no result found" using arraylist in struts2如何在struts2中使用arraylist显示“未找到结果”
【发布时间】:2015-07-07 21:13:31
【问题描述】:

在这里我触发了一些查询..它显示记录,但我想要的是如果没有记录,那么它应该显示“未找到匹配/结果”。
我在 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>

请大家帮帮我:(

【问题讨论】:

    标签: javascript java jsp arraylist struts2


    【解决方案1】:

    可以使用struts2的&lt;s:if&gt;&lt;s:else&gt;标签来检查列表是否为空,例如

    <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;logic:present&gt;标签。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2019-06-18
      • 2012-05-31
      相关资源
      最近更新 更多