Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
JSP - A mailing list about Java Server Pages specification and reference
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Oracle Connection Pooling in 3 2 2
Servlet : Session invalidate
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Tomcat and webapplication specific java library path
Running a Simple JMS Example
Mapping in workers2 properties
org apache jasper JasperException
Cannot find message resources under key org apache struts action
   MESSAGE
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
url string for connecting jboss to oracle
Value attribute of <html:checkbox
javax servlet ServletException: BeanUtils populate
HTTP Status 404 The requested resource is not available
5 0 18: Windows XP Pro vs Windows 2000
 
- selectItems strange behavior

- selectItems strange behavior

2007-08-09       - By rodrigotico

 Back
Hello all,

I am trying to get a object from a <h:selectOneMenu> component and getting a
strange behavior.

My entities are:

Client  >1:1>  Address  >n:1>  City  <n:1>  State

In my user interface I have a selectMenu with the states registered on the
database and another selectMenu with the cities of the state selected. When the
user select the state I fill the cities box. This is working very good.

The problem is when I try to press the button of the form to save the client:
If the the city box don??t have a city selected, the action is called as
expected.
If the city box has a city selected, the form is reloaded without call the
action method of the button.

I don??t know what to do. I am trying to inject client.address.city, is that
possible?

I??m putting here all the code of a test project that I created to test it.

Thanks.

My Session Bean (insertClient)


 | @(protected)
 | @(protected)("insertClient")
 | public class InsertClientAction implements InsertClient {
 |
 |   @(protected) Client client;
 |   @(protected) StateDAO stateDAO;
 |   private ArrayList<State> states;
 |   private ArrayList<City> cities;
 |
 |   public String viewClient() {
 |     stateDAO.createInitDB();
 |     return "/client.xhtml";
 |   }
 |  
 |   public void saveClient() {
 |     System.out.println("Client??s Name: " + client.getName());
 |     System.out.println("Client??s Street: " + client.getAddress().getStreet()
);
 |     System.out.println("Client??s City: " + client.getAddress().getCity()
.getName());
 |     System.out.println("Client??s State: " + client.getAddress().getCity()
.getState().getName());
 |   }
 |  
 |   public ArrayList<State> getStates() {
 |     states = new ArrayList<State>(stateDAO.getAll());
 |     return states;
 |   }
 |  
 |   public ArrayList<City> getCities() {
 |     return cities;
 |   }
 |  
 |   public void updateCities(ValueChangeEvent event) {
 |     State state;
 |     try {
 |       state = (State) event.getNewValue();
 |     } catch (Exception e) {
 |       return;
 |     }
 |     if (state!=null) {
 |       System.out.println("State: " + state.getName());
 |       cities = new ArrayList<City>(state.getCities());
 |     }
 |   }
 | }
 |

Session Bean Interface


 | @(protected)
 | public interface InsertClient {
 |
 |   public String viewClient();
 |   public void saveClient();
 |   public Collection <State> getStates();
 |   public Collection <City> getCities();
 |   public void updateCities(ValueChangeEvent event);
 |  
 | }
 |

My User Interface: (client.xhtml)



 |     <h:panelGrid columns="2">
 |       <h:outputText value="Name:"></h:outputText>
 |       <h:inputText  value="#{client.name}"></h:inputText>
 |       <h:outputText value="Street:"></h:outputText>
 |       <h:inputText  value="#{client.address.street}"></h:inputText>
 |      
 |       <h:outputText value="State:"></h:outputText>
 |
 |     <h:selectOneMenu value="#{state}" id="state"  valueChangeListener="#
{insertClient.updateCities}">
 |       <s:selectItems value="#{insertClient.states}" var="state" label="#{state
.name}" noSelectionLabel="Please Select..."/>
 |       <a:support event="onchange" reRender="city" ></a:support>
 |       <s:convertEntity />
 |     </h:selectOneMenu>
 |
 |       <h:outputText value="City:"></h:outputText>
 |     <h:selectOneMenu value="#{client.address.city}" id="city">
 |       <s:selectItems value="#{insertClient.cities}" var="city" label="#{city
.name}" noSelectionLabel="Please Select..." />
 |       <s:convertEntity />
 |     </h:selectOneMenu>
 |      
 |   <h:commandButton action="#{insertClient.saveClient}" value="ok h:"></h
:commandButton>
 |   <s:button action="#{insertClient.saveClient}" value="ok s:"></s:button>
 |      
 |   </h:panelGrid>
 |

Client Entity:


 | @(protected)
 | @(protected)("client")
 | @(protected)
 | public class Client {
 |  
 |   private Long id;
 |   private String name;
 |   private Address address;
 |  
 |   @(protected)
 |   @(protected)(strategy=GenerationType.AUTO)
 |   public Long getId() {
 |     return id;
 |   }
 |  
 |   public void setId(Long id) {
 |     this.id = id;
 |   }
 |
 |   public Client() {
 |     address = new Address();
 |   }
 |  
 |   @(protected)(nullable=false, length=200)
 |   public String getName() {
 |     return name;
 |   }
 |
 |   public void setName(String name) {
 |     this.name = name;
 |   }
 |
 |   @(protected) (cascade=CascadeType.ALL)
 |   public Address getAddress() {
 |     return address;
 |   }
 |
 |   public void setAddress(Address address) {
 |     this.address = address;
 |   }
 | }
 |

Address Entity:


 | @(protected)
 | @(protected)("address")
 | public class Address {
 |
 |   private Long id;
 |   private String street;
 |   private City city;
 |
 |  
 |   @(protected)
 |   @(protected)(strategy=GenerationType.AUTO)
 |   public Long getId() {
 |     return id;
 |   }
 |  
 |   public void setId(Long id) {
 |     this.id = id;
 |   }
 |
 |   @(protected)(nullable=false, length=200)  
 |   public String getStreet() {
 |     return street;
 |   }
 |
 |   public void setStreet(String street) {
 |     this.street = street;
 |   }
 |
 |   @(protected)
 |   @(protected)(name="city_id")
 |   public City getCity() {
 |     return city;
 |   }
 |
 |   public void setCity(City city) {
 |     this.city = city;
 |   }
 |  
 | }
 |

City Entity:


 | @(protected)
 | @(protected)("city")
 | @(protected)
 | public class City implements Serializable {
 |
 |   private static final long serialVersionUID = -8910258028221144179L;
 |   private Long id;
 |   private String name;
 |   private State state;
 |  
 |  
 |   @(protected)
 |   @(protected)(strategy=GenerationType.AUTO)
 |   public Long getId() {
 |     return id;
 |   }
 |  
 |   public void setId(Long id) {
 |     this.id = id;
 |   }
 |
 |   public City() {
 |     state = new State();
 |   }
 |
 |   @(protected)(nullable=false, length=40)
 |   public String getName() {
 |     return name;
 |   }
 |
 |   public void setName(String name) {
 |     this.name = name;
 |   }
 |
 |   @(protected)
 |   @(protected)(name="state_id")
 |   public State getState() {
 |     return state;
 |   }
 |
 |   public void setState(State state) {
 |     this.state = state;
 |   }
 |
 | }
 |

State Entity:


 | @(protected)
 | @(protected)("state")
 | @(protected)
 | public class State implements Serializable {
 |
 |   private static final long serialVersionUID = -8286496968276572340L;
 |   private long id;
 |   private String name;
 |   private Collection<City> cities;
 |  
 |   @(protected)
 |   @(protected)(strategy=GenerationType.AUTO)
 |   public Long getId() {
 |     return id;
 |   }
 |  
 |   public void setId(Long id) {
 |     this.id = id;
 |   }
 |
 |   @(protected)(nullable=false, length=40)
 |   public String getName() {
 |     return name;
 |   }
 |
 |   public void setName(String name) {
 |     this.name = name;
 |   }
 |
 |   @(protected)
 |   public boolean equals(Object obj) {
 |     if (obj instanceof State) {
 |       if (((State)obj).getId()==getId())
 |         return true;
 |       return false;
 |     }
 |     return super.equals(obj);
 |   }
 |
 |   @(protected)
 |   public String toString() {
 |     return super.toString();
 |   }
 |  
 |   @(protected)(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
 |   @(protected)(name="state_id")
 |   public Collection<City> getCities() {
 |     return cities;
 |   }
 |
 |   public void setCities(Collection<City> cities) {
 |     this.cities = cities;
 |   }    
 |  
 | }
 |

The State DAO:


 | @(protected)("stateDAO")
 | @(protected)
 | public class StateDAO extends EntityHome<State> {
 |
 |   @(protected)
 |   public Collection<State> getAll() {        
 |     return getEntityManager().createQuery("from State order by id")
.getResultList();
 |   }
 |  
 |   @(protected)
 |   public void createInitDB() {
 |     EntityManager em = getEntityManager();
 |    
 |     State california = new State();
 |     california.setName("California");
 |     em.persist(california);
 |     State florida = new State();
 |     florida.setName("Florida");
 |     em.persist(florida);
 |    
 |     City city;
 |     city = new City();
 |     city.setName("Los Angeles");
 |     city.setState(california);
 |     em.persist(city);
 |    
 |     city = new City();
 |     city.setName("San Francisco");
 |     city.setState(california);
 |     em.persist(city);
 |
 |     city = new City();
 |     city.setName("Miami");
 |     city.setState(florida);
 |     em.persist(city);
 |
 |     city = new City();
 |     city.setName("Orlando");
 |     city.setState(florida);
 |     em.persist(city);
 |   }
 | }
 |

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

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

__ ____ ____ ____ ____ ____ ____ ____ ____ ____
jboss-user mailing list
jboss-user@(protected)
https://lists.jboss.org/mailman/listinfo/jboss-user

©2008 junlu.com - Jax Systems, LLC, U.S.A.