[jboss-user] [JBoss Seam] - Problems using JSF Converters in Seam app

smartbinary do-not-reply at jboss.com
Tue Oct 3 16:26:16 EDT 2006


I'm sure this is something simple, but this is my first go around w/using Converters within a Seam app.

-----

I simply want to convert a name field entered in the UI to 'Firstname Lastname'.  For instance, if a user enters 'Smart, Todd', I want to convert that into 'Todd Smart'.  I realize there are easy ways to do this within the getter/setter for the name field ... this is an exercise to better understand how to use JSF Converters in Seam -- I eventually want to 'ajaxify' these converter sequences for more complex requirements.

So...I created FullName and FullNameConverter classes:


  | package org.jboss.seam.example.hibernate;
  | 
  | import java.io.Serializable;
  | 
  | public class FullName implements Serializable {
  | 
  |     private String firstName;
  |     private String lastName;
  |     
  |     public FullName(String unparsedName) {
  |         unparsedName = unparsedName.trim();
  |         String[] splitName = unparsedName.split(" ");
  |         if(splitName.length == 2 && unparsedName.indexOf(",") == -1) {
  |             setFirstName(splitName[0]);
  |             setLastName(splitName[1]);
  |         } else if(splitName.length == 2) {
  |             setFirstName(splitName[1]);
  |             setLastName(splitName[0].replace(",", ""));            
  |         }            
  |     }
  |     
  |     public String getFirstName() {
  |         return firstName;
  |     }
  |     
  |     public void setFirstName(String firstName) {
  |         this.firstName = firstName;
  |     }
  |     
  |     public String getLastName() {
  |         return lastName;
  |     }
  |     
  |     public void setLastName(String lastName) {
  |         this.lastName = lastName;
  |     }
  |     
  |     public String toString() {
  |         return this.firstName + " " + this.lastName;
  |     }
  | }
  | 


  | package org.jboss.seam.example.hibernate;
  | 
  | import java.io.Serializable;
  | 
  | import javax.faces.component.UIComponent;
  | import javax.faces.context.FacesContext;
  | import javax.faces.convert.Converter;
  | import javax.faces.convert.ConverterException;
  | 
  | import org.jboss.seam.InterceptionType;
  | import org.jboss.seam.ScopeType;
  | import org.jboss.seam.annotations.Intercept;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.Scope;
  | 
  | @Scope(ScopeType.SESSION)
  | @Name("fullNameConverter")
  | @Intercept(InterceptionType.ALWAYS)
  | public class FullNameConverter implements Serializable, Converter {
  | 
  |     public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
  |             throws ConverterException {
  |         
  |         FullName fullName;
  |         fullName = new FullName(arg2);
  |         
  |         return fullName;
  |     }
  | 
  |     public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
  |             throws ConverterException {
  |         String returnString;
  |         
  |         FullName fullName;
  |         fullName = (FullName) arg2;
  |         
  |         returnString = (fullName==null ? null : fullName.toString());
  |         
  |         return returnString;
  |     }
  | 
  | }
  | 

I'm using the AppUser class (from the Seam Hibernate3 Booking Example).  Specifically, it's name property:


  |    @NotNull
  |    @Length(max=100)
  |    public String getName()
  |    {
  |       return name;
  |    }
  |    public void setName(String name)
  |    {
  |       this.name = name;
  |    }
  | 

Finally, I've updated the register.xhtml from the same booking example to use the FullNameConverter for the name field:


  | 	<div class="entry">
  | 		<div class="label"><h:outputLabel for="name">Name (first last):</h:outputLabel></div>
  | 		<div class="input">
  | 			<h:inputText id="name" value="#{appUser.name}" required="true" converter="#{fullNameConverter}" >
  | 			</h:inputText>
  | 			<br/>
  | 			<span class="errors"><h:message for="name" /></span>
  | 		</div>
  | 	</div>
  | 

...............

When I run through the app (with my debugger attached), I find that ... when the form is submitted ... the FullNameConverter.getAsObject() is called (which does receive the inputted value).  This is followed by a call to the AppUser.getName() method.  The result is that the getName does NOT have the converted value.  Of course, this is to be expected....as the FullNameConverter.getAsString() would need to be called to return the converted value.

Oddly enough, when the register.xhtml file is initially executed (before the user has entered values), I show (debugger breakpoints) that FullNameConverter.getAsString() is called, followed by a call to AppUser.getName().

Anyway...I'm sure this is something simple -- or foolish :) -- that I'm doing and/or assuming regarding using Converters within Seam.  Please clue me in...


-- 
Regards, 

Todd

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3975871#3975871

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3975871



More information about the jboss-user mailing list