English 中文(简体)
Handling data in ActionBean
原标题:
  • 时间:2009-12-07 01:14:08
  •  标签:
  • java
  • stripes

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);
    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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签