首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpSession Java

HttpSession Java
EN

Stack Overflow用户
提问于 2016-05-16 03:42:10
回答 1查看 189关注 0票数 0

所有人!

所以我在使用httpsession时遇到了一个小问题,我不知道如何修复它。这是问题所在。

我有一个Person类,它有一个set<Product>,我像这样在request.getSession中设置了person类。

String email = getRequest().getParameter("email");String credential = getRequest().getParameter("credential");

代码语言:javascript
复制
    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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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时,我希望看到产品经过修改。非常感谢大家。

EN

回答 1

Stack Overflow用户

发布于 2016-05-21 12:05:45

我找到了一个解决方案,用这些对象创建另一个类,然后把它放到会话中。它起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37242897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档