struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From JOSE L MARTINEZ-AVIAL <jlm...@gmail.com>
Subject Re: Struts2 json-plugin - issue setting nested bean?
Date Wed, 29 Oct 2014 07:21:17 GMT
So, you are using the JSONInterceptor, right? Share your package definition
so we can check that, it is not clear from your mail that you are using it.
And actually, the log line:

DEBUG: Setting params testValue => [ yes ] contact[firstName] => [ Bean ]
contactId => [ 100 ]

seems to indicate that the request is not being sent in JSON format. Could
you check that with firebug or a HTTP proxy or sniffer? Also, the
content/type of the request needs to be application/json, otherwise the
JSONInterceptor won't do anything.

2014-10-28 22:35 GMT-04:00 Burton Rhodes <burtonrhodes@gmail.com>:

> Actually, I just notice that if I use the jquery method serialize() as
> opposed to serializeArray() it works out of the box.
>
> On Tue, Oct 28, 2014 at 9:31 PM, Burton Rhodes <burtonrhodes@gmail.com>
> wrote:
>
> > That's it... thank you!  However, the docs mention that the JSON string
> > must be "well formed" and it specifically demonstrates that my first
> > attempt should have worked in the "nestedBean" example (see below).  Plus
> > jQuery serializes the form that why by default accommodating for the dot
> > notation ;-)
> >
> > I guess what I'm really asking is, do you think this was implemented
> > incorrectly and should be coded differently to be more intuitive and
> > account for more "standard" serialization of a html form?  What I love
> > about Struts is most things with it just work - this seems to be one of
> the
> > few areas where it doesn't.
> >
> > http://struts.apache.org/release/2.3.x/docs/json-plugin.html
> >
> > Specifically where it mentions "nestedBean" and the description below the
> > code example "There must be a "setNestedBean" whose argument type can be
> > any class, that has a "setName" method taking as argument an "String"." :
> >
> > {
> >    "doubleValue": 10.10,
> >    "nestedBean": {
> >       "name": "Mr Bean"
> >    },
> >    "list": ["A", 10, 20.20, {
> >       "firstName": "El Zorro"
> >    }],
> >    "array": [10, 20]
> > }
> >
> > The action must have a "setDoubleValue" method, taking either a "float"
> or
> > a "double" argument (the interceptor will convert the value to the right
> > one). There must be a "setNestedBean" whose argument type can be any
> class,
> > that has a "setName" method taking as argument an "String". There must
> be a
> > "setList" method that takes a "List" as argument, that list will contain:
> > "A" (String), 10 (Long), 20.20 (Double), Map ("firstName" -> "El Zorro").
> > The "setArray" method can take as parameter either a "List", or any
> numeric
> > array.
> >
> > On Tue, Oct 28, 2014 at 6:45 PM, JOSE L MARTINEZ-AVIAL <jlmagc@gmail.com
> >
> > wrote:
> >
> >> Unless you use the json inteceptor to map the request boy to the action,
> >> you need to send the contact firstName as "contact.firstName", so Struts
> >> will invoke
> >>
> >> setContact(new Contcat());
> >> getContact().setFirstName("John");
> >>
> >> -- JSON TEST SUBMISSION --
> >> $.ajax({
> >>   ... other ajax settings ...
> >>  // manually insert data for testing
> >>  data    :
> {"testValue":"yes","contact.firstName":"John","contactId":100},
> >> });
> >>
> >>
> >>
> >> 2014-10-28 18:19 GMT-04:00 Burton Rhodes <burtonrhodes@gmail.com>:
> >>
> >> > I am having issues setting nested parameters on an object using the
> >> > json-plugin.  I've created a basic example that I'm currently testing.
> >> > When debugging at the "return SUCCESS" line, both the testValue and
> >> > contactId variables are set correctly.  The contact variable, however,
> >> is
> >> > null.  The relevant console lines line is "WARN : Parameter
> >> > [contact[firstName]] didn't match acceptedPattern pattern!" - but I'm
> >> not
> >> > sure how to fix that as it *should* match the setContact() setter in
> my
> >> > Action.  Any thoughts on what is going on here?  Thanks in advance....
> >> >
> >> > -- ACTION --
> >> > public class ContactJsonAction {
> >> >
> >> >     private Integer contactId;
> >> >     private Contact contact;
> >> >     private String testValue;
> >> >
> >> >     public String contactUpdate() throws Exception {
> >> >             return SUCCESS;
> >> >     }
> >> >
> >> >    // Getters & Setters below....
> >> > }
> >> >
> >> > -- JSON TEST SUBMISSION --
> >> > $.ajax({
> >> >   ... other ajax settings ...
> >> >  // manually insert data for testing
> >> >  data    : {"testValue":"yes","contact":{"firstName":"John"
> >> > },"contactId":100},
> >> > });
> >> >
> >> > -- LOGS --
> >> > DEBUG: Creating an DefaultActionProxy for namespace [/app/json] and
> >> action
> >> > name [ContactDetail_update]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: cannot find method [prepareContactUpdate] in action
> >> > [com.afs.web.action.json.ContactJsonAction@b7a6ee]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: cannot find method [prepareDoContactUpdate] in action
> >> > [com.afs.web.action.json.ContactJsonAction@b7a6ee]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: Setting params NONE
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: Setting params testValue => [ yes ] contact[firstName] =>
[
> Bean
> >> ]
> >> > contactId => [ 100 ]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: Parameter [contactId] was accepted and will be appended to
> >> action!
> >> > Oct 28, 17:15:34 (CommonsLogger.java:56)
> >> > WARN : Parameter [contact[firstName]] didn't match acceptedPattern
> >> pattern!
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: Parameter [testValue] was accepted and will be appended to
> >> action!
> >> > Oct 28, 17:15:34 (CommonsLogger.java:76)
> >> > DEBUG: Retrieving convert for class [class
> >> > com.afs.web.action.json.ContactJsonAction] and property [contactId]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: field-level type converter for property [contactId] = none
> found
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: global-level type converter for property [contactId] =
> >> > com.afs.web.converter.MyNumberConverter@1611a05
> >> > Oct 28, 17:15:34 (CommonsLogger.java:76)
> >> > DEBUG: Retrieving convert for class [class
> >> > com.afs.web.action.json.ContactJsonAction] and property [testValue]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: field-level type converter for property [testValue] = none
> found
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: global-level type converter for property [testValue] = none
> found
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> > DEBUG: falling back to default type converter
> >> > [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@a5eac1]
> >> > Oct 28, 17:15:34 (CommonsLogger.java:72)
> >> >
> >>
> >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message