2

在我的例子中,我可以用一个对象预先填充我的 Stripes JSP 表单client,但是当我提交这个表单时,我的对象返回为 null。

我创建了第二个“临时”对象,它是一个并行副本,client并且保留了它的值,所以我看不到在请求中传递对象的问题

我的表格如下:

<s:form beanclass="com.jameselsey.salestracker.action.ViewClientAction">
            <s:hidden name="clientA" value="${actionBean.clientA}"/>
            <s:hidden name="clientId" value="${actionBean.clientId}"/>

            <table>
                <tr>
                    <td>Name : </td>
                    <td><s:text name="client.name"/></td>
                </tr>
                <tr>
                    <td>Sector : </td>
                    <td><s:text name="client.sector"/></td>
                </tr>
              <!-- omitted some attirbutes, not needed here -->
            </table>
        </s:form>

我的动作看起来像

public class ViewClientAction extends BaseAction
{

    @SpringBean
    ClientService clientService;// = new ClientService();
    private Integer clientId;
    private Client client;
    private Client clientA;

    public void setClient(Client client)
    {
        this.client = client;
    }
    public Integer getClientId()
    {
        return clientId;
    }

    public void setClientId(Integer clientId)
    {
        this.clientId = clientId;
    }

    public Client getClientA()
    {
        return clientA;
    }

    public void setClientA(Client clientA)
    {
        this.clientA = clientA;
    }



    public Client getClient()
    {
        return client;
    }

    @DefaultHandler
    public Resolution quickView()
    {
        clientA = clientService.getClientById(clientId);
        client = clientService.getClientById(clientId);
        return new ForwardResolution("/jsp/viewClientQuickView.jsp");
    }

    public Resolution save()
    {
        clientService.persistClient(client);
        return new ForwardResolution("/jsp/reports.jsp");
    }

    public Resolution viewClientInfo()
    {
        client = clientService.getClientById(clientId);
        return new ForwardResolution("/jsp/viewClientClientInfo.jsp");
    }
...

如果我在 处设置断点,clientService.persistClient(client);我可以看到它ClientA具有对象的所有原始值,但client仍为空。

我是否错过了将表单 bean 绑定到client我的操作中的对象的东西?

谢谢

4

2 回答 2

4

在您的 JSP 中添加这一行:

    <s:hidden name="client" value="${actionBean.client}"/>
于 2010-01-08T16:14:17.713 回答
2

我通过添加一个 @Before 方法来重新水化嵌套对象,从而使这个场景工作。在此之后,保存工作正常

@Before(stages = LifecycleStage.BindingAndValidation)
public void rehydrate() {
   if (context.getRequest().getParameter("save")!=null){
       this.domainObject = getHibernateSession().load(DomainObject.class, context.getRequest().getParameter("id"));
   }
}

public void save(){
    Session session=getHibernateSession();
    session.update(domainObject);
    session.commit();
    //...
}
于 2011-09-14T03:04:28.263 回答