Ben, I see you have gotten a few responses, but I'll try to clarify what
they're trying to say.
Let's say you have an ActionBean with a Foo property named foo. It has a
view() handler that forwards to the form and an update() handler that
handles the form post, updates the object and saves it.
public FooActionBean extends BaseActionBean {
private Foo foo; // add getter & setter
public Resolution view() {
return new ForwardResolution("/WEB-INF/jsp/foo/form.jsp");
}
public Resolution update() {
getDAO().save(getFoo());
getDAO().commit();
getContext().getMessages().add(new LocalizableMessage("foo.updated",
foo.getDescription()));
return new RedirectResolution(getClass(), "view").flash(this);
}
}
With a little bit of setup, it really can be that simple. Let's say your Foo
class has an ID property of type Long that serves as the primary key.
public class Foo {
@Id private Long id; // add getter
...
}
If you set up a TypeConverter<Foo> for Stripes, you can pass in a parameter
"foo=123" on your request, and Stripes will pass the "123" value to the type
converter, expecting to get a Foo object back. Your type converter would
parse the Long value from the String input, look up the entity by ID and
return it.
public FooTypeConverter implements TypeConverter<Foo> {
...
public Foo convert(String input, Class<? extends Foo> type,
Collection<ValidationError> errors) {
try {
long id = Long.parseLong(input);
return getDAO().get(id);
} catch (Exception e) {
log.error("Could not convert input '" + input + "' to Foo");
errors.add(new LocalizableError("foo.id.invalid", input));
return null;
}
}
}
You can likewise create a Formatter<Foo> that returns
String.valueOf(foo.getId()). Once you have this type converter and formatter
registered, Stripes can easily take the "foo=123" parameter, look up the
entity with ID 123 and set it on your ActionBean. So when you link to your
Foo editing form, your link would look like
<s:link beanclass="com.yourcompany.stripes.action.FooActionBean"
event="view"><s:param name="foo" value="${foo}" />Edit</s:link>
Which, if you are working with foo #123, generates a link like
/myapp/Foo.action?foo=123
Then when your view() event fires, the foo property has already been looked
up and set on your ActionBean. The form, then, needs to keep track of
exactly which foo it is editing. So you place inside the form a hidden input
named "foo," which will be included in the post to the update() handler.
<s:form beanclass="${actionBean.class}">
<s:hidden name="foo">
...
</s:form>
Since "foo" is present as a request parameter, with the ID of the Foo you
are editing, the input's value will be set correctly. When you submit the
form, you again send a "foo=123" parameter as part of the post, the type
conversion takes place, the entity is looked up, and the foo property is set
on your ActionBean again. All nested properties of foo that you edited in
your form are applied to the entity that was retrieved from the database.
You then save the entity, add a message indicating what you did, and follow
the redirect-after-post pattern to display the form again. (Of course, you
can redirect to anywhere you like. This is just a simple example.)
To summarize: create a type converter and formatter for each of your
entities. It will make developing with Stripes a breeze.
Having said all this, Stripersist will do much of this for free. Take a look
at it if you're using JPA. Also, as you get more comfortable you'll want to
look at @StrictBinding to control which properties and nested properties can
be modified by a request.
-Ben
On Mon, Feb 9, 2009 at 4:34 AM, Ben <[email protected]> wrote:
> This feels like it is a stupid question but then sometimes they need to be
> asked.
>
> I have a POJO with various fields and a few collections. I would like to
> edit
> some of the fields in a form. I have created an ActionBean with the object
> embeded and exposed through a get/set, loaded an instance of the object,
> ForwardResolution'ed to the JSP and then used object.field in the form with
> a
> POST back to the ActionBean - so far so good.
>
> What I understod from the documentation was that Stripes would update the
> existing object (i.e. the one that was loaded in the ActionBean) with the
> values
> in the form but what it is actually doing is instanciating a new object and
> populating that from the form. This basically means that I either have to
> have
> every field as an editable or hidden field on the form or load another copy
> of
> the pre-existing object and 'merge' the changes in - which as far as I can
> tell
> is what the Bugzooky app does.
>
> I guess my question is, can I change the behaviour so I dont have to do
> either
> of those things and get stripes to simply update an existing object rather
> than
> create a new one? Or should I stop complaining and get on with it? ;)
>
> TIA and appologies for what is probably a very basic and stupid question.
>
> Ben
>
> PS In case it makes a difference, I am using JDK 1.6.0.11 / Stripes 1.5 /
> Hibernate 3.3.1 / Tomcat 6
>
>
------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users