You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Eric Lentz <er...@outfastsource.com> on 2012/12/03 17:19:02 UTC

Extending base class and accessing implementation's fields in Struts 2

I have
List<BaseObject> foo

The list is of type BaseObjectImpl

BaseObject has fields:
String a
String b

BaseObjectImpl has fields:
String c
String d

Now I'm in a JSP and want to iterate foo (using <s:iterator>), accessing
fields c and d (inside iterator using <s:property>, for example). How? As
far as Struts knows I'm dealing with BaseObject. Is there a way for me to
cast to BaseObjectImpl without creating a StrutsTypeConverter for every
object I'm extending? In my use case, there will be several and the
temptation is to just define the list with the implementation which foils
reusability patterns (e.g., List<BaseObjectImpl>).

Likewise, what to do when posting back to a field like this:
BaseObject bar  (which has BaseObjectImpl as its implementation) e.g.,
<s:textfield name="bar.c" />


Is there a totally different approach that would be better or is
StrutsTypeConverter the only good answer?

- Eric

Re: Extending base class and accessing implementation's fields in Struts 2

Posted by Dave Newton <da...@gmail.com>.
You're making OGNL calls against an object; it doesn't care about the
type--if you access foo.bar it'll call getBar() on foo, setBar() on
submission.

Dave

On Mon, Dec 3, 2012 at 11:19 AM, Eric Lentz <er...@outfastsource.com> wrote:
> I have
> List<BaseObject> foo
>
> The list is of type BaseObjectImpl
>
> BaseObject has fields:
> String a
> String b
>
> BaseObjectImpl has fields:
> String c
> String d
>
> Now I'm in a JSP and want to iterate foo (using <s:iterator>), accessing
> fields c and d (inside iterator using <s:property>, for example). How? As
> far as Struts knows I'm dealing with BaseObject. Is there a way for me to
> cast to BaseObjectImpl without creating a StrutsTypeConverter for every
> object I'm extending? In my use case, there will be several and the
> temptation is to just define the list with the implementation which foils
> reusability patterns (e.g., List<BaseObjectImpl>).
>
> Likewise, what to do when posting back to a field like this:
> BaseObject bar  (which has BaseObjectImpl as its implementation) e.g.,
> <s:textfield name="bar.c" />
>
>
> Is there a totally different approach that would be better or is
> StrutsTypeConverter the only good answer?
>
> - Eric



-- 
e: davelnewton@gmail.com
m: 908-380-8699
s: davelnewton_skype
t: @dave_newton
b: Bucky Bits
g: davelnewton
so: Dave Newton

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Extending base class and accessing implementation's fields in Struts 2

Posted by Lukasz Lenart <lu...@apache.org>.
2012/12/3 Eric Lentz <er...@outfastsource.com>:
> I'm almost there. I didn't suspect that it would work that way with the
> iterator. Should have tried it first! Thanks Dave.
>
> On the way back, such as in a post, if BaseObject is an interface or
> abstract, as I'm hoping for it to be, then Struts tries to instantiate the
> BaseObject type, which it can't, so it throws:
>
> [com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler] Could
> not create and/or set value back on to object
> java.lang.InstantiationException
> at
> sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
>  at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
> at java.lang.Class.newInstance0(Class.java:350)
>  at java.lang.Class.newInstance(Class.java:303)
> at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:130)
>  at
> com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.createObject(InstantiatingNullHandler.java:159)
> at
> com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.nullPropertyValue(InstantiatingNullHandler.java:137)
>  at
> com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper.nullPropertyValue(OgnlNullHandlerWrapper.java:21)
> at ognl.ASTProperty.getValueBody(ASTProperty.java:118)
> ...etc.
>
> Any ideas on solving that?

You can implement your own ObjectTypeDeterminer or you can try to play
with @Element


Regards
-- 
Ɓukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Extending base class and accessing implementation's fields in Struts 2

Posted by Eric Lentz <er...@outfastsource.com>.
I'm almost there. I didn't suspect that it would work that way with the
iterator. Should have tried it first! Thanks Dave.

On the way back, such as in a post, if BaseObject is an interface or
abstract, as I'm hoping for it to be, then Struts tries to instantiate the
BaseObject type, which it can't, so it throws:

[com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler] Could
not create and/or set value back on to object
java.lang.InstantiationException
at
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
at java.lang.Class.newInstance0(Class.java:350)
 at java.lang.Class.newInstance(Class.java:303)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:130)
 at
com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.createObject(InstantiatingNullHandler.java:159)
at
com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler.nullPropertyValue(InstantiatingNullHandler.java:137)
 at
com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper.nullPropertyValue(OgnlNullHandlerWrapper.java:21)
at ognl.ASTProperty.getValueBody(ASTProperty.java:118)
...etc.

Any ideas on solving that?

- Eric

On Mon, Dec 3, 2012 at 11:19 AM, Eric Lentz <er...@outfastsource.com>wrote:

> I have
> List<BaseObject> foo
>
> The list is of type BaseObjectImpl
>
> BaseObject has fields:
> String a
> String b
>
> BaseObjectImpl has fields:
> String c
> String d
>
> Now I'm in a JSP and want to iterate foo (using <s:iterator>), accessing
> fields c and d (inside iterator using <s:property>, for example). How? As
> far as Struts knows I'm dealing with BaseObject. Is there a way for me to
> cast to BaseObjectImpl without creating a StrutsTypeConverter for every
> object I'm extending? In my use case, there will be several and the
> temptation is to just define the list with the implementation which foils
> reusability patterns (e.g., List<BaseObjectImpl>).
>
> Likewise, what to do when posting back to a field like this:
> BaseObject bar  (which has BaseObjectImpl as its implementation) e.g.,
> <s:textfield name="bar.c" />
>
>
> Is there a totally different approach that would be better or is
> StrutsTypeConverter the only good answer?
>
> - Eric
>
>