我讓錯誤的HQL嘗試訪問另一個表中的列。抱歉。 此問題已解決。 我有Object類型的ArrayList。從會話中提取。 它存在。我檢查。但我甚至可以從會話中獲取信息。將其更改爲ArrayList。休眠打印會話對象數據列表中的數據
我的目標是打印結果。我選擇了一些sql列,我只是想輸出它們。當然這是對象。 我沒有錯誤,這是問題。只有我的類型產品的現有數組列表對象。
ArrayList <prod> goodlist = (ArrayList <prod>) session.getAttribute("goodlist_s");
for (prod glst : goodlist) {
%>
<tr>
<td><%//=out.println(u.getname())%></td>
<td><%=glst.getcatid()%></td>}
我嘗試將其轉換爲字符串ArrayList。
這是我的功能
@SuppressWarnings("unchecked")
public static ArrayList<prod> getListOfProds(String catname,String name,Integer pricel, Integer priceh){
ArrayList<prod> list = new ArrayList<prod>();
Session session = HibernateUtil.openSession();
Transaction tx = null;
try {
tx = session.getTransaction();
tx.begin();
Query query = session.createQuery("from prod where catname=?");
query.setString(0, catname);
//query.setInteger(1, pricel);
//query.setInteger(2, priceh);
list = (ArrayList<prod>) query.list();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return list;
}
BTW,我會發來的所有代碼 的index.jsp
<%@page import="java.util.List"%>
<%@page import="service.IndexService"%>
<%@page import="java.util.Date"%>
<%@page import="goods.prod"%>
<%@page import="goods.cat"%>
<%@page import ="java.util.ArrayList"%>
<%@page import ="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charprods=UTF-8">
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<title>Result Page</title>
</head>
</head>
<body>
<form action="IndexServlet" method="POST">
<table align="center" cellpadding = "10">
<tr>
<td>Category</td>
<td><input type="text" name="catname" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Category id</td>
<td><input type="number" name="catid" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Naimenovanie</td>
<td><input type="text" name="name" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Price low</td>
<td><input type="number" name="pricel" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Price high</td>
<td><input type="number" name="priceh" maxlength="100" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</table>
prodsAttribute
<%
prod prod = (prod) session.getAttribute("prod");
%>
<%
ArrayList <prod> goodlist = (ArrayList <prod>) session.getAttribute("goodlist_s");
%>
<b>You have<% if (null == prod) out.println("No text entered.");%></b>
<b>You have<% if (null == goodlist) out.println("goodlist does NOT exists.");%></b>
<b>You have<% if (null != goodlist) {
ArrayList<String> jlistTitles = new ArrayList<String>((ArrayList<String>) session.getAttribute("goodlist_s"));
if (null == jlistTitles) out.println("convertedStringlist does NOT exists.");
if (null != jlistTitles) out.println("convertedStringlist exists. How can I use it?");
for(int i = 0; i < goodlist.size(); i++) {
System.out.println(goodlist.get(i).getprice());
//print object type
}
out.println("goodlist exists. How can I use it?");}%></b>
<br>
<b><%if (null!=prod) out.println(prod.getid()+ " "+prod.getname()+ " "+prod.getprice());%></b>
<br/>
<%
if (null!=goodlist) {
for (prod glst : goodlist) {
%>
<tr>
<td><%//=out.println(u.getname())%></td>
<td><%=glst.getcatid()%></td>
<td><%=glst.getprice()%></td>
<td><%="hh"%></td>
</tr>
<%}}%>
</form>
</body>
</html>
Indexservlet.java
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import goods.prod;
import service.IndexService;
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = -8670416133536111566L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charprods=UTF-8");
//Long id = Long.parseLong(request.getParameter("id"));
String name = request.getParameter("name");
String catname = request.getParameter("catname");
Integer pricel = Integer.parseInt(request.getParameter("pricel"));
Integer priceh = Integer.parseInt(request.getParameter("priceh"));
prod dot = IndexService.getprodbyparam(catname,name,pricel,priceh);
List<prod> goodlist = IndexService.getListOfProds(catname,name,pricel,priceh);
request.getSession().setAttribute("goodlist_s", goodlist);
request.getSession().setAttribute("prod", dot);
response.sendRedirect("index.jsp");
}
}
與功能
package service;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import HibernateUtil.HibernateUtil;
import goods.prod;
public class IndexService {
public static prod getprodbyparam(String catname,String name,Integer pricel, Integer priceh) {
Session session = HibernateUtil.openSession();
Transaction tx = null;
prod prod = null;
try {
tx = session.getTransaction();
tx.begin();
//Query query = session.createQuery("select prod.id from prod as prod inner join cat.id as id where name=?");
Query query = session.createQuery("from prod where name=? and price between ? and ?");
query.setString(0, name);
query.setInteger(1, pricel);
query.setInteger(2, priceh);
//query.prodsInteger(3, catid);
prod = (prod)query.uniqueResult();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return prod;
}
@SuppressWarnings("unchecked")
public static ArrayList<prod> getListOfProds(String catname,String name,Integer pricel, Integer priceh){
ArrayList<prod> list = new ArrayList<prod>();
Session session = HibernateUtil.openSession();
Transaction tx = null;
try {
tx = session.getTransaction();
tx.begin();
Query query = session.createQuery("from prod where catname=?");
query.setString(0, catname);
//query.setInteger(1, pricel);
//query.setInteger(2, priceh);
list = (ArrayList<prod>) query.list();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return list;
}
}
督促與干將
package goods;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "prod")
public class prod implements Serializable {
@Id @GeneratedValue
@Column(name = "id")
private Long id;
private Integer catid;
private String name;
private Integer price;
public prod() {
}
public prod(Long id, Integer catid, String name, Integer price) {
this.id = id;
this.catid = catid;
this.name = name ;
this.price = price;
}
public Long getid() {
return id;
}
public void setid(Long id) {
this.id = id;
}
public Integer getcatid() {
return catid;
}
public void setcatid(Integer catid) {
this.catid = catid;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public Integer getprice() {
return price;
}
public void setprice(Integer price) {
this.price = price;
}
}
的web.xml Indexservice
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Improve</display-name>
<servlet>
<servlet-name>IndexServlet</servlet-name>
<servlet-class>servlet.IndexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/IndexServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
你是否想要這樣做的第一次? – Acewin
您是否嘗試了request.setAttribute而不是將對象設置爲會話 – Acewin
request.getSession()。setAttribute(「goodlist_s」,goodlist); \t request.getSession()。setAttribute(「prod」,dot);這是在我的代碼。當然,我會按要求做。 – ifooi