java - 如何在 JSP 中显示从 Hibernate 查询获得的列表

标签 java hibernate jsp struts2 ognl

我正在创建一个 struts hibernate 应用程序。我已经使用 hibernate 查询获得了一个列表并将其传递到 Action 类中。但我不知道如何在 JSP 中显示它。

我已经根据查询成功获取了列表。 现在我想在 JSP 中显示这个列表。

我已经发布了 struts.xml 和用于显示结果的 JSP。 好心检查。但是在 JSP 中什么也没有出现。 我正在使用 s:iterate 来显示列表。但没有运气。 甚至我也曾尝试在 s:iterate 下打印简单文本,只是为了测试。 但它也没有出现。

POJO 类:

package org.sachin.Model;

import java.sql.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;

@Entity
@Table(name="feed")
public class FeedBack {

private FeedBack feedback;
 private int taste;
 private int waiter;
 private int infra;
 private int price;

 private int id;

 private Date date=new java.sql.Date(new java.util.Date().getTime());
public int getTaste() {
    return taste;
}
public void setTaste(int taste) {
    this.taste = taste;
}
public int getWaiter() {
    return waiter;
}
public void setWaiter(int waiter) {
    this.waiter = waiter;
}
public int getInfra() {
    return infra;
}
public void setInfra(int infra) {
    this.infra = infra;
}
public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}


@Temporal(javax.persistence.TemporalType.DATE)
public Date getDate() {
    return date;
}
public void setDate(Date date) {
    this.date = date;
}
public FeedBack getFeedback() {
    return feedback;
}
public void setFeedback(FeedBack feedback) {
    this.feedback = feedback;
}


}

Action 类:

package org.sachin.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sachin.Model.Datepicker;
import org.sachin.Model.FeedBack;
import org.sachin.hibernate.DateSpecific;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SelectDateAction extends ActionSupport {

    /**
     * 
     */
private static final long serialVersionUID = -8922465133057293868L;
private Datepicker datepicker;
private List<FeedBack> list =new ArrayList<>();;

    public List<FeedBack> getList() {
    return list;
}
public void setList(List<FeedBack> list) {
    this.list = list;
}
    public String execute(){

        String date=getDatepicker().getDate1();


        DateSpecific da=new DateSpecific();

         list=da.find(date);
         for(FeedBack feed:list){
             System.out.println("price");
             System.out.println(feed.getPrice());


         }

        System.out.println("hi"+date);

        return SUCCESS;
    }


    public Datepicker getDatepicker() {
        return datepicker;
    }
    public void setDatepicker(Datepicker datepicker) {
        this.datepicker = datepicker;
    }

}

hibernate 类:

public List<FeedBack> find(String Date) {


                //  Transaction t=session.beginTransaction(); 

            String SQL_QUERY = " from FeedBack where date='"+Date+"'";
            System.out.println("i am in hiber");
            System.out.println(SQL_QUERY);
            org.hibernate.Query query = session.createQuery(SQL_QUERY);
            List<FeedBack> list = query.list();
          for(FeedBack f:list){
              System.out.println("price");
              System.out.println(f.getPrice());
          }
            return list;

        }

这是我的 struts.xml,其中定义了所有操作。现在操作是 ByDate

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

    <constant name="struts.devMode" value="true" />
<!--    <constant name="struts.action.excludePattern" value="/ServletToExcludeFromStruts*" />
 -->
    <include file="login.xml"></include>
    <include file="AdminLogin.xml"></include>
    <include file="Feedback.xml"></include>
    <include file="NewUser.xml"></include>
    <include file="feedback.xml"></include>
    <include file="expression.xml"></include>
    <include file="logout.xml"></include>

    <package name="helloworld" namespace="/tut" extends="struts-default">
        <action name="add" class="org.sachin.action.EditAdminAction"
            method="execute">
            <result name="success">/JSP/success.jsp</result>
            <result name="error">/JSP/AdminUserNameExists.jsp</result>

        </action>

    </package>

     <package name="serve" namespace="/tut" extends="struts-default">

        <action name="ByDate" class="org.sachin.action.SelectDateAction"
            method="execute">
            <result name="success" type="redirect">/JSP/iterate.jsp</result>
            <result name="error" type="redirect">/JSP/FeedBack.jsp</result>
            <result name="input" type="redirect">/JSP/Rateus.jsp</result>

        </action>
    </package>


</struts>

这是我用于显示列表的 JSP

iterate.jsp:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<s:form>
<body>
 <h1>Struts 2 Iterator tag example</h1>

<h3>Simple Iterator</h3>
<ol>
<s:iterator value="list">
 HE<li><s:property /></li>
</s:iterator>
</ol>
  </body>
  </s:form>
  </html>

我已经根据查询成功获取了列表。 现在我想在 JSP 中显示这个列表。

最佳答案

请检查以下代码。

<s:iterator value="list" var="item"> 
<s:property value="%{#item.price}" /> 
</s:iterator> 

关于java - 如何在 JSP 中显示从 Hibernate 查询获得的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25442784/

相关文章:

java - HTTP 状态 500 - 在第 16 行处理 JSP page/trail.jsp 时发生异常

java - 如何使用 Java 打印没有科学记数法的 double 值?

java - 为什么要返回 Iterator 低耦合 (OOP)?

java - 如何在给定模型(Java Web)中引用另一个模型?

java - 在 JPA 的多对多关系中使用组合键

javascript - 为什么动态值不会以这种形式打印?

java - 在 Android 中使用外部 Java 库

java - Hibernate 在查询时没有响应

java - Oracle 序列不会从 Java/Hibernate 递增

css - 修复带有显示标签的表格的宽度