【问题标题】:JavaBean values don't appear on POST requestJavaBean 值不会出现在 POST 请求中
【发布时间】:2012-08-31 07:32:42
【问题描述】:

我有一个共享 JavaBean 实例的 servlet 和 JSP - servlet 初始化它们(因为 bean 需要构造函数参数)并将它们存储在会话中,然后转发到一个 JSP,它获取用户输入并通过一个形成 POST 请求。在 servlet 的 doPost 方法中,它再次检索 JavaBean,但它的所有由表单设置的值都是空的。如果我在表单上使用 GET 方法,则填充值,并且在调试时我可以看到 JavaBean 属性值实际上已设置。那么为什么我的doPost 方法会从会话中检索具有空值的bean?有趣的是,通过每个对象的构造函数参数传入的字段实际上是正确设置的,无论是 GET 还是 POST - 只是那些通过表单管理的属性没有设置。

缩写代码示例

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    HttpSession session = request.getSession();
    Customer customer = (Customer)session.getAttribute("customer");
    if (customer == null)
    {
        customer = new Customer(.....);
        session.setAttribute("customer", customer);
    }
    Address address = (Address)session.getAttribute("address");
    if (address == null)
    {
        address = new Address(.....);
        session.setAttribute("address", address);
    }
    forward("/checkout.jsp", request, response);
}
...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    PrintWriter out = response.getWriter();
    Customer customer = (Customer)request.getSession().getAttribute("customer");
    if (customer != null)
    {
        out.println("Customer: " + customer.toString());
    }
    Address address = (Address)request.getSession().getAttribute("address");
    if (address != null)
    {
        out.println("Address: " + address.toString());
    }
}

JSP

<jsp:useBean id="customer" class="com.mycompany.myproject.Customer" scope="session" />
<jsp:setProperty name="customer" property="*"/>
<jsp:useBean id="address" class="com.mycompany.myproject.Address" scope="session" />
<jsp:setProperty name="address" property="*"/>

...

<form method="POST" action="${pageContext.request.contextPath }/checkout">
    <table>
        <tr>
            <td>Title</td>
            <td><input type="text" name="title" value="${ customer.title }" /></td>
        </tr>
        <tr>
            <td>Given name</td>
            <td><input type="text" name="givenName" value="${ customer.givenName }" /></td>
        </tr>
        ...
    </table>
    <input type="submit" value="Place order" />
</form>

当我在此表单中输入数据并单击“下订单”时,toString() 调用的输出将 customeraddress 的所有字段显示为空。

【问题讨论】:

  • 如何以及在代码中的何处设置这 2 个对象的属性?
  • 据我所知(我承认我是 JSP 的新手),它们设置在 &lt;jsp:setProperty name="address" property="*"/&gt; 中。但是现在这开始变得有意义了,因为这段代码只在 GET 上执行 - 因为 POST 在返回之前通过 servlet,它没有设置任何值。

标签: jsp servlets javabeans


【解决方案1】:

因此,我想要的似乎是不可能的 - &lt;jsp:setProperty ... property="*" /&gt; 是我需要的,但在我提供另一个 JSP 之前它不可用,我想在 之后 访问 servlet 中的 bean。所以我可以执行以下操作之一:

  • 在我的 servlet 中使用 bean 之前,重定向到另一个 JSP 以填充我的值
  • 手动设置值
  • 使用反射来设置它们(可能是可扩展性的赢家)
  • 可能使用类似BeanUtils

【讨论】:

    猜你喜欢
    • 2014-01-26
    • 2018-08-16
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 2021-06-07
    • 2021-07-30
    相关资源
    最近更新 更多