Java 将指向产品或类别的链接从控制器Servlet显示到jsp页面
我试图在用户选择给定产品后将产品链接到他们自己的jsp页面。我做错了什么,因为数据在product.jsp上不会显示一次 我尝试使用变量Java 将指向产品或类别的链接从控制器Servlet显示到jsp页面,java,jsp,jstl,el,Java,Jsp,Jstl,El,我试图在用户选择给定产品后将产品链接到他们自己的jsp页面。我做错了什么,因为数据在product.jsp上不会显示一次 我尝试使用变量selectedProduct检索数据,但应用程序似乎无法识别该变量,或者不知道从何处检索变量selectedProduct的数据 我不知道为什么,因为在我看来,selectedProduct将与selectedCategory相同。。。显然不是。。。为什么?我错过什么了吗 过去一周我一直在研究解决方案。欢迎任何帮助 控制器Servlet: package co
selectedProduct
检索数据,但应用程序似乎无法识别该变量,或者不知道从何处检索变量selectedProduct
的数据
我不知道为什么,因为在我看来,selectedProduct
将与selectedCategory
相同。。。显然不是。。。为什么?我错过什么了吗
过去一周我一直在研究解决方案。欢迎任何帮助
控制器Servlet:
package controller;
import cart.ShoppingCart;
import wishlist.Wishlist;
import entity.Category;
import entity.Product;
import java.io.IOException;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.CategoryFacade;
import session.ProductFacade;
public class ControllerServlet extends HttpServlet {
@EJB
private CategoryFacade categoryFacade;
@EJB
private ProductFacade productFacade;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
HttpSession session = request.getSession();
Category selectedCategory;
Product selectedProduct;
Collection<Product> categoryProducts;
// if category page is requested
if (userPath.equals("/category")) {
// get categoryId from request
String categoryId = request.getQueryString();
if (categoryId != null) {
// get selected category
selectedCategory = categoryFacade.find(Short.parseShort(categoryId));
// place selected category in session scope
session.setAttribute("selectedCategory", selectedCategory);
// get all products for selected category
categoryProducts = selectedCategory.getProductCollection();
// place category products in session scope
session.setAttribute("categoryProducts", categoryProducts);
}
// if product page is requested
if (userPath.equals("/product")) {
// get productId from request
String productId = request.getQueryString();
if (productId != null) {
// get selected product
selectedProduct = productFacade.find(Short.parseShort(productId));
// place selected product in session scope
session.setAttribute("selectedProduct", selectedProduct);
}}
包装控制器;
进口购物车。购物车;
导入愿望列表。愿望列表;
进口实体。类别;
进口实体、产品;
导入java.io.IOException;
导入java.util.Collection;
导入javax.ejb.ejb;
导入javax.servlet.ServletConfig;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.http.HttpSession;
导入session.CategoryFacade;
导入session.ProductFacade;
公共类ControllerServlet扩展了HttpServlet{
@EJB
私人分类法分类法;
@EJB
私人产品门面;
@凌驾
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
字符串userPath=request.getServletPath();
HttpSession session=request.getSession();
类别选择类别;
产品选择产品;
收集类别产品;
//如果需要类别页面
if(userPath.equals(“/category”)){
//从请求中获取categoryId
String categoryId=request.getQueryString();
if(categoryId!=null){
//获取所选类别
selectedCategory=categoryFacade.find(Short.parseShort(categoryId));
//将所选类别放入会话范围
session.setAttribute(“selectedCategory”,selectedCategory);
//获取所选类别的所有产品
categoryProducts=selectedCategory.getProductCollection();
//将类别产品置于会话范围内
setAttribute(“categoryProducts”,categoryProducts);
}
//如果需要产品页面
if(userPath.equals(“/product”)){
//从请求中获取productId
字符串productId=request.getQueryString();
if(productId!=null){
//获取所选产品
selectedProduct=productFacade.find(Short.parseShort(productId));
//将所选产品放入会话范围
session.setAttribute(“selectedProduct”,selectedProduct);
}}
根据请求编辑
Product.java:
package entity;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author PC
*/
@Entity
@Table(name = "product")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p"),
@NamedQuery(name = "Product.findById", query = "SELECT p FROM Product p WHERE p.id = :id"),
@NamedQuery(name = "Product.findByName", query = "SELECT p FROM Product p WHERE p.name = :name"),
@NamedQuery(name = "Product.findByPrice", query = "SELECT p FROM Product p WHERE p.price = :price"),
@NamedQuery(name = "Product.findByDescription", query = "SELECT p FROM Product p WHERE p.description = :description"),
@NamedQuery(name = "Product.findByLastUpdate", query = "SELECT p FROM Product p WHERE p.lastUpdate = :lastUpdate")})
public class Product implements Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "price")
private BigDecimal price;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Lob
@Size(min = 1, max = 16777215)
@Column(name = "name")
private String name;
@Lob
@Size(max = 2147483647)
@Column(name = "description")
private String description;
@Basic(optional = false)
@NotNull
@Column(name = "last_update")
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;
@JoinColumn(name = "category_id", referencedColumnName = "id")
@ManyToMany(mappedBy = "productCollection")
private Collection<Category> categoryCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
private Collection<OrderedProduct> orderedProductCollection;
/**
*
*/
public Product() {
}
/**
*
* @param id
*/
public Product(Integer id) {
this.id = id;
}
/**
*
* @param id
* @param name
* @param price
* @param lastUpdate
*/
public Product(Integer id, String name, BigDecimal price, Date lastUpdate) {
this.id = id;
this.name = name;
this.price = price;
this.lastUpdate = lastUpdate;
}
/**
*
* @return
*/
public Integer getId() {
return id;
}
/**
*
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
*/
public String getName() {
return name;
}
/**
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
*/
public String getDescription() {
return description;
}
/**
*
* @param description
*/
public void setDescription(String description) {
this.description = description;
}
/**
*
* @return
*/
public Date getLastUpdate() {
return lastUpdate;
}
/**
*
* @param lastUpdate
*/
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
/**
*
* @return
*/
@XmlTransient
public Collection<Category> getCategoryCollection() {
return categoryCollection;
}
/**
*
* @param categoryCollection
*/
public void setCategoryCollection(Collection<Category> categoryCollection) {
this.categoryCollection = categoryCollection;
}
/**
*
* @return
*/
@XmlTransient
public Collection<OrderedProduct> getOrderedProductCollection() {
return orderedProductCollection;
}
/**
*
* @param orderedProductCollection
*/
public void setOrderedProductCollection(Collection<OrderedProduct> orderedProductCollection) {
this.orderedProductCollection = orderedProductCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Product)) {
return false;
}
Product other = (Product) object;
return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
}
@Override
public String toString() {
return "entity.Product[ id=" + id + " ]";
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
<table style="text-align: left; width: 100%; height: 172px;" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td colspan="1" rowspan="6" style="vertical-align: top;">product_gallery<br></td>
<td colspan="1" rowspan="6" style="vertical-align: top;"><img class="img" src="${initParam.productBigImagePath}${selectedProduct.name}.jpg"><br></td>
<td style="vertical-align: top;">${selectedProduct.name}<br></td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td style="vertical-align: top;">$ ${selectedProduct.price}</td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td style="vertical-align: top;"><br></td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td style="vertical-align: top;"><br></td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td colspan="2" rowspan="1" style="vertical-align: top;">${selectedProduct.description}</td>
</tr>
<tr>
<td style="vertical-align: top;">
<form action="addToWishlist" method="post"><br><br>
<input name="productId" value="${selectedProduct.id}" type="hidden">
<input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit">
</form><br>
</td>
<td style="vertical-align: top;">
<form action="addToCart" method="post"><br><br>
<input name="productId" value="${selectedProduct.id}" type="hidden">
<input class="submit" value="<fmt:message key='AddToCart'/>"type="submit">
</form>
</td>
</tr>
<tr>
<td style="vertical-align: top;"><br></td>
<td style="vertical-align: top;"><br></td>
<td colspan="2" rowspan="1" style="vertical-align: top;">
<ul>
<li style="background-color: rgb(198, 255, 201); width:100%; text-align:center; border-radius:2em;">
<a href="${value}"><fmt:message key='ContinueShopping'/></a>
</li>
</ul>
<br>
</td>
</tr>
</tbody>
</table>
包实体;
导入java.io.Serializable;
导入java.math.BigDecimal;
导入java.util.Collection;
导入java.util.Date;
导入javax.persistence.Basic;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.Lob;
导入javax.persistence.ManyToMany;
导入javax.persistence.namedquerys;
导入javax.persistence.NamedQuery;
导入javax.persistence.OneToMany;
导入javax.persistence.Table;
导入javax.persistence.Temporal;
导入javax.persistence.TemporalType;
导入javax.validation.constraints.NotNull;
导入javax.validation.constraints.Size;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.XmlTransient;
/**
*
*@author-PC
*/
@实体
@表(name=“产品”)
@XmlRootElement
@命名查询({
@NamedQuery(name=“Product.findAll”,query=“从产品p中选择p”),
@NamedQuery(name=“Product.findById”,query=“从产品p中选择p,其中p.id=:id”),
@NamedQuery(name=“Product.findByName”,query=“从产品p中选择p,其中p.name=:name”),
@NamedQuery(name=“Product.findByPrice”,query=“从产品p中选择p,其中p.price=:price”),
@NamedQuery(name=“Product.findByDescription”,query=“从产品p中选择p,其中p.description=:description”),
@NamedQuery(name=“Product.findByLastUpdate”,query=“从产品p中选择p,其中p.lastUpdate=:lastUpdate”)}
公共类产品实现可序列化{
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=45)
@列(name=“price”)
私人价格;
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@NotNull
@高球
@尺寸(最小值=1,最大值=16777215)
@列(name=“name”)
私有字符串名称;
@高球
@尺寸(最大值=2147483647)
@列(name=“description”)
私有字符串描述;
@基本(可选=假)
@NotNull
@列(name=“上次更新”)
@时态(TemporalType.TIMESTAMP)
私人日期最新更新;
@JoinColumn(name=“category\u id”,referencedColumnName=“id”)
@ManyToMany(mappedBy=“productCollection”)
私人收藏类别收藏;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“产品”)
私人收藏orderedProductCollection;
/**
*
*/
公共产品(){
}
/**
*
*@param-id
*/
公共产品(整数id){
this.id=id;
}
/**
*
*@param-id
* @
<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="40px">
<c:forEach var="product" items="${categoryProducts}" varStatus="iter">
<td>
<tbody>
<tr>
<td style="vertical-align: middle; text-align: center;" class="cell">
<a href="product?${product.id}">
<img class="img" alt="" src="${initParam.productImagePath}${product.name}.jpg" />
<div class="caption">
<br>view details</div>
</a>
<br>
</td>
<td style="vertical-align: middle; width: 140px; text-align: center; ">${product.name}
<br>
</td>
<td style="vertical-align: middle; width: 120px; text-align: center; line-height:100%;">$${product.price}
<br>
</td>
<td style="vertical-align: middle; width: 136px; text-align: center; line-height:20%;">
<form id="wishlistForm" action="addToWishlist" method="post">
<br>
<br>
<input name="productId" value="${product.id}" type="hidden">
<input class="submit" onclick="addedWishlist()" value="<fmt:message key='AddToWishlist'/>" type="submit">
</form>
<br>
</td>
<td style="vertical-align: middle; width: 136px; text-align: center; line-height:20%;">
<form id="cartForm" action="addToCart" method="post">
<br>
<br>
<input name="productId" value="${product.id}" type="hidden">
<input class="submit" onclick="addedCart()" value="<fmt:message key='AddToCart'/>" type="submit">
</form>
<br>
</td>
</tr>
</tbody>
</c:forEach>
</table>
Product selectedProduct;
...
session.setAttribute("selectedProduct", selectedProduct);
Collection<Product> categoryProducts;
...
session.setAttribute("categoryProducts", categoryProducts);
if (productId != null) {
// get selected product
selectedProduct = productFacade.find(Short.parseShort(productId));
// place selected product in session scope
session.setAttribute("selectedProduct", selectedProduct);
}
<table style="text-align: left; width: 100%; height: 172px;" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td colspan="1" rowspan="6" style="vertical-align: top;">product_gallery<br></td>
<td colspan="1" rowspan="6" style="vertical-align: top;"><img class="img" src="${initParam.productBigImagePath}${selectedProduct.name}.jpg"><br></td>
<td style="vertical-align: top;">${selectedProduct.name}<br></td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td style="vertical-align: top;">$ ${selectedProduct.price}</td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td style="vertical-align: top;"><br></td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td style="vertical-align: top;"><br></td>
<td style="vertical-align: top;"><br></td>
</tr>
<tr>
<td colspan="2" rowspan="1" style="vertical-align: top;">${selectedProduct.description}</td>
</tr>
<tr>
<td style="vertical-align: top;">
<form action="addToWishlist" method="post"><br><br>
<input name="productId" value="${selectedProduct.id}" type="hidden">
<input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit">
</form><br>
</td>
<td style="vertical-align: top;">
<form action="addToCart" method="post"><br><br>
<input name="productId" value="${selectedProduct.id}" type="hidden">
<input class="submit" value="<fmt:message key='AddToCart'/>"type="submit">
</form>
</td>
</tr>
<tr>
<td style="vertical-align: top;"><br></td>
<td style="vertical-align: top;"><br></td>
<td colspan="2" rowspan="1" style="vertical-align: top;">
<ul>
<li style="background-color: rgb(198, 255, 201); width:100%; text-align:center; border-radius:2em;">
<a href="${value}"><fmt:message key='ContinueShopping'/></a>
</li>
</ul>
<br>
</td>
</tr>
</tbody>
</table>
// if product page is requested
} else if (userPath.equals("/viewProduct")) {
String productId = request.getQueryString();
if (productId != null) {
// get selected product
selectedProduct = productFacade.find(Integer.parseInt(productId));
// place selected product in session scope
session.setAttribute("selectedProduct", selectedProduct);
}
userPath = "/product";