5 Replies Latest reply on Jan 8, 2007 2:37 AM by None None

    Public members as bean properties within EL?

    None None Master

      I have a class that looks like this:

      public class Foo {
       public String name;
       public String favoriteColor;
      }
      


      When I try to access this in EL, using something like #{foo.name} it gives me an exception because it says that "name" is not a bean property. I can fix the problem by adding getters and setters, of course, but for this class, I would prefer to just use public members. Is such a thing possible?

      I realize that public members are usually considered bad Java, but in this case, it really is a data transfer object, used much like a C struct, and it does make sense for them to be public.


        • 1. Re: Public members as bean properties within EL?
          Norman Richards Master

          No, I don't believe you can do this. Does JavaBeans make any provision for field access?

          • 2. Re: Public members as bean properties within EL?
            None None Master

            The problem I was running into was that unfortunately JAXB looks at both private fields and getter methods to determine properties, so if I have:

            class Foo {
             private String name;
             public String getName() { return name; }
            }
            


            JAXB was giving me an error that there were two fields with the same name. I then tried putting @XmlTransient on either the getter or the private member, and that messed everything up. So I ended up removing the private fields by calling them private String _name, and then everything worked.

            What's cool here is that I have a class that has Hibernate validators, so I can process it easily within forms, and Seam SFSBs can do stuff to it once it's valid, and that stuff can include marshalling it and sending it off... and it's all based on one single class file, which happens to have a bunch of annotations on it. I go from a Web form straight to an XML stream, with Seam SFSBs acting as light-weight intermediaries, and with Facelets displaying nice validation messages as needed. It is in fact pretty cool. I guess if I wanted to be even more cool I could also make that object into an @Entity so it could also be persisted.


            • 3. Re: Public members as bean properties within EL?
              None None Master

              Btw this is all with Java 6 (mustang) built-in JAXB. Yes I'm doing everything cutting-edge here. When I started this project I had never used J2EE before, and I started with pre-release Java 6, JBoss Jems Beta 2, and Seam 1.0.0 CR or something. I have had to absorb a ton of different techniques, and have had to strugle to even get the right configs in the right place, and now it's working and I'm seeing productivity gains from it.

              • 4. Re: Public members as bean properties within EL?
                Christian Bauer Master

                 

                "SmokingAPipe" wrote:

                class Foo {
                 private String name;
                 public String getName() { return name; }
                }
                


                JAXB was giving me an error that there were two fields with the same name.


                That would mean JAXB is not JavaBeans compliant. It's either properties (with getter and setter methods) or fields, not both.


                • 5. Re: Public members as bean properties within EL?
                  None None Master

                   

                  "christian.bauer@jboss.com" wrote:
                  That would mean JAXB is not JavaBeans compliant. It's either properties (with getter and setter methods) or fields, not both.


                  I didn't spend a lot of time delving into it, but that's definitely what was happening in my class. I just now made up a very simple test class and didn't see that problem. So I'm not sure what was causing that, but when I used _name for the private members, things started working. Who knows. This class had static inner classes for various things, so maybe that's related.