Matt
Matt

Reputation: 4785

Is it possible to use immutable class in Struts2 form?

Situation: I am currently moving from using Struts 1 to Struts 2. In Struts 1, it was necessary for form classes to follow the JavaBean specification. Since my business data objects are all immutable, this required creating a JavaBean version of many classes, and methods mapping between the two.

Does Struts2 support using immutable objects for forms?

If so, how do I configure it such that the immutable object is instantiated from its builder using the form fields?

Upvotes: 1

Views: 128

Answers (1)

Dave Newton
Dave Newton

Reputation: 160261

The object being populated from the request can't be immutable, because OGNL calls setters on it–that's just how OGNL (and most ELs) work.

I figure you have two options: you could either do something with a custom parameters interceptor, or create a constructor or builder that takes a bean used for the form.

Without any real thought, I'd probably do the latter, although I might create a quick tool to generate the form classes if there are a lot of them. An interceptor would be more elegant, but it'd probably need to use reflection.

Upvotes: 2

Related Questions