开发者

Handling data in ActionBean

how do I save data between two events in ActionBean? In the following example I create contact in addContact(), preserve it and save it to attribute contact. When i try to access that contact after redirectction, in addNumber(), its null.

private Contact contact;
...
public Resolution addNumber() {
    log.debug("addNumber() to contact={}", contact);
    return new ForwardResolution("/addNumber.jsp");
}

public Resolution addContact() {
    log.debug("addContact() - name={}", name);
    contact=contactFacade.create(name, surname));
    log.debug("addContact() OK - contact={}", contact)开发者_如何学JAVA;
    return new RedirectResolution(this.getClass(), "addNumber");
}

What am I doing wrong?


This should work:

public Resolution addContact() {
    log.debug("addContact() - name={}", name);
    contact=contactFacade.create(name, surname));
    log.debug("addContact() OK - contact={}", contact);

    // Add this actionBean into the flash scope to preserve its state after redirection
    return new RedirectResolution(this.getClass(), "addNumber").flash(this);
}

Source: http://www.stripesframework.org/display/stripes/State+Management#StateManagement-RedirectafterPost


RedirectResolution causes the users browser to navigate to a new URL, that is handled by a fresh instance of an ActionBean (even if it is the same ActioBean they were coming from)

My suggestion in this case is to redirect and add a parameter to the RedirectResolution so that the URL in the next request contains the contact id/key. You would do this by returning return new RedirectResolution(this.getClass(), "addNumber").addParameter("name", name);


I don't do Stripes, but are you familiar with how HTTP/JSP/Servlet works? That's what Stripes uses "under the hood". Especially the fact that a redirect will create a new request which would thus cause that all request scoped attributes get lost. Also the fact that the request scope has a shorter lifespan (from the moment that the client fires it until the client has received the last bit from the appropriate response) than the session scope (which lives from the first time that the server accesses the client-associated session until the session get timed-out or invalidated).

The symptoms of your problem makes me think that the ActionBean and/or the Contact is not session scoped while the code expects it to be. On the other hand, if you want to keep it request scoped (which is very reasonable, I'd prefer that as well), then you need to load/construct the same Contact on every request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜