所有人!
所以我在使用httpsession时遇到了一个小问题,我不知道如何修复它。这是问题所在。
我有一个Person类,它有一个set<Product>,我像这样在request.getSession中设置了person类。
String email = getRequest().getParameter("email");String credential = getRequest().getParameter("credential");
validEmail(email);
validCredential(credential);
if(existError()){
setValues(email, credential);;
forward(login);
return;
}
PersonService ps = serviceFactory.getService(PersonService.class);
boolean success = ps.validatePasswordAndEmail(email, credential);
if(success){
Person person = ps.getPersonByEmail(email);
getSession().setAttribute("person", person); here you can see the object person into the session
forward("/packed.jsp");
}else{
addError("Email or Credential is not valid!");
setValues(email, credential);
forward(login);
return;
}之后,如果用户输入了正确的信息,他们将转到另一个名为packed.jsp的文件,并被记录下来。
我的问题是当我编辑登录到系统的产品人员时。让我在这里展示我的代码
名为packed.jsp的页面有一个按钮,登录的用户单击该按钮,然后转到下面的servlet
public class EditProductAction extends Action {
private String editProduct = "/edit_product.jsp";
private String packed = "/packed.jsp";
@Override
public void process() throws Exception {
Integer id = Integer.parseInt(getRequest().getParameter("id"));
String brand = getRequest().getParameter("brand");
String model = getRequest().getParameter("model");
String name = getRequest().getParameter("name");
String quantity = getRequest().getParameter("quantity");
String color = getRequest().getParameter("color");
String info = getRequest().getParameter("info");
ProductService ps = serviceFactory.getService(ProductService.class);
Product product = ps.getProductByID(id);
if(name == null){
getRequest().setAttribute("product",product);
forward(editProduct);
return;
}
product.setBrand(brand);
product.setModel(model);
product.setName(name);
product.setQuantity(Integer.parseInt(quantity));
product.setColor(color);
product.setInfo(info);
ps.update(product);
getResponse().sendRedirect(getRequest().getContextPath()+packed);
}}
正如你所看到的,一切正常,但当我回到packed.jsp页面时,它没有显示产品已被修改,但在数据库中工作正常,我的意思是产品已被修改。这里是packed.jsp
<div class="mainDiv">
<table border="1px" width="80%">
<tr>
<th>BRAND</th>
<th>MODEL</th>
<th>NAME</th>
<th>QUANTITY</th>
<th>COLOR</th>
<th>INFO</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:choose>
<c:when test="${empty person.products}">
<tr>
<td colspan="8" align="center"><span>You don't have any product yet!</span></td>
</tr>
</c:when>
<c:otherwise>
<c:forEach var="p" items="${person.products}">
<c:url var="urlDel" value="servlet">
<c:param name="id" value="${p.id}" />
</c:url>
<c:url var="urlEdit" value="EditProduct.action">
<c:param name="id" value="${p.id}" />
</c:url>
<tr>
<td>${p.brand}</td>
<td>${p.model}</td>
<td>${p.name}</td>
<td>${p.quantity}</td>
<td>${p.color}</td>
<td>${p.info}</td>
<td><a href="${urlEdit}"><img src="<%=request.getContextPath()%>/images/edit.png"></a></td>
<td><a href="${urlDel}"><img src="<%=request.getContextPath()%>/images/delete.png"></a></td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
我想要更改产品,然后当我回到packed.jsp时,我希望看到产品经过修改。非常感谢大家。
发布于 2016-05-21 12:05:45
我找到了一个解决方案,用这些对象创建另一个类,然后把它放到会话中。它起作用了。
https://stackoverflow.com/questions/37242897
复制相似问题