我错误的HQL尝试从另一个表访问列。遗憾。
这个问题已经解决了。
我有Object类型的ArrayList。从会话中获取。
它存在。我查一下但我t print it it from jsp or java. even I create methods.
// with type List I can
甚至可以从会话中获取信息。将其更改为ArrayList。
我的目标是打印结果。我选择了一些sql列,我只想输出它们。当然这是对象。 我没有错误,那就是问题。只有我类型的现有arraylist对象。
ArrayList <prod> goodlist = (ArrayList <prod>) session.getAttribute("goodlist_s");
for (prod glst : goodlist) {
%>
<tr>
<td><%//=out.println(u.getname())%></td>
<td><%=glst.getcatid()%></td>}
我尝试将其转换为String ArrayList。
ArrayList<String> jlistTitles = new ArrayList<String>((ArrayList<String>) session.getAttribute("goodlist_s")); //converted fine
for(int i = 0; i < goodlist.size(); i++) {
System.out.println(goodlist.get(i).getprice());
//print object type
}
**Shows nothing**
这是我的功能
@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
<?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>
答案 0 :(得分:0)
您可以尝试下面的代码,看看它是否有效
ArrayList<String> goodlist_s= (ArrayList<String>)session.getAttribute("goodlist_s");
<%
for(int i = 0; i < goodlist_s.size(); i++)
{
String myString = (String) goodlist_s.get(i);
}
%>