如何在jsp中显示从hibernate查询中获取的列表 [英] How to display in jsp a list obtained from hibernate query

查看:129
本文介绍了如何在jsp中显示从hibernate查询中获取的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经成功地在查询的基础上获取列表。
现在我想在JSP中显示这个列表。



我发布了 struts.xml , JSP也显示结果。
请检查。但在JSP中没有任何显示。
我使用 s:iterate 来显示列表。但没有运气。
即使我已经尝试在 s:iterate 下打印简单文本,仅用于测试。
但它也没有出现。

POJO类:

 包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 infra;
私有诠释价格;

private int id;
$ b $ 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;


$ b @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 Class:

  package org.sachin.action; 

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

导入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;
私人Datepicker datepicker;
私人列表<反馈> list = new ArrayList<>();;

公共列表<反馈> 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);

返回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();

字符串SQL_QUERY =来自FeedBack where date ='+ Date +';
System.out.println(我在hiber);
System.out.println(SQL_QUERY);
org.hibernate.Query query = session.createQuery(SQL_QUERY);
列表<反馈> list = query.list();
for(FeedBack f:list){
System.out.println(price);
System.out.println(f.getPrice());
}
返回列表;

$ b

这是我的 struts.xml ,其中所有动作都已定义。现在该动作是 ByDate



struts.xml
$ b

 < xml version =1.0encoding =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.devModevalue =true/>
<! - <常数名称=struts.action.excludePatternvalue =/ 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 =helloworldnamespace =/ tutextends =struts-default>
method =execute>
< result name =success> /JSP/success.jsp< / result>
< result name =error> /JSP/AdminUserNameExists.jsp< / result>

< / action>

< / package>

< package name =servenamespace =/ tutextends =struts-default>

method =execute>
< result name =successtype =redirect> /JSP/iterate.jsp< / result>
< result name =errortype =redirect> /JSP/FeedBack.jsp< / result>
< result name =inputtype =redirect> /JSP/Rateus.jsp< / result>

< / action>
< / package>


< / struts>

这是我的JSP用于显示列表



iterate.jsp
$ b

 < ;%@ page language =javacontentType =text / html; charset = ISO-8859-1
pageEncoding =ISO-8859-1%>
<%@ taglib prefix =suri =/ struts-tags%>

<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">
< html>
< body>
< h1> Struts 2迭代器标记示例< / h1>

< h3>简单迭代器< / h3>
< ol>
< s:iterator value =list>
HE< li>< s:property />< / li>
< / s:iterator>
< / ol>
< / body>
< / s:form>
< / html>

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

解决方案

使用 iterator 标记:


迭代器将迭代一个值。可迭代的值可以是以下任何一个:
java.util.Collection java.util.Iterator java.util.Enumeration
java.util.Map 或数组。


代码
$ b

 < s:iterator value =list/> 
价格:< s:property value =price/>< br>
< / s:iterator>


I am creating an struts hibernate application. I have obtained a list using hibernate query and passed it in action class. But I don't know how to display it in JSP.

I have successfully getting the list on the basis of query. Now I want to display this list in JSP.

I have posted struts.xml and the JSP for showing result also. Kindly check. But in JSP nothing is showing up. I am using s:iterate to show the list. But no luck. Even I have tried printing simple text under s:iterate, just for testing. But it is also not showing up.

POJO class:

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 Class:

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 class:

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;

        }

This is my struts.xml in which all actions are defined.For now the action is 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>

This is my JSP for showing list

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>

I have successfully getting the list on the basis of query. Now I want to display this list in JSP.

解决方案

Use iterator tag:

Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator, java.util.Enumeration, java.util.Map, or an array.

The code:

<s:iterator value="list"/>
  Price: <s:property value="price"/><br>
</s:iterator> 

这篇关于如何在jsp中显示从hibernate查询中获取的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆