voyent
List box values - surving page refresh  XML
Forum Index -> General Help
Author Message
xsteviex

Joined: 29/Apr/2007 00:00:00
Messages: 16
Offline


Hi - I'm new to JSF in general, and Icefaces.

I'm trying to write a simple test application involving 3 HtmlSelectOneMenu items for a continent, country, region type drill-down.

Simple scenario is:

- page loads - listbox one is visible (default "please select" item is selected")

- user selects value - listbox two is visible with values based on selection from box 1 selection (default "please select" item is selected")

- user selects value for list 3 - listbox three is visible and has values based on box 2 selection (default "please select" item is selected")

Event handling should also take account of user changing a higher level box - the lower level box should get new values, or become invisible in the case of box 3 if the box 1 value is reset.

- user changes value in box 1 - box 3 disappears, box 2 gets new values and defaults to "please select" item

- user changes value in box 2 - box 3 gets new values and defaults to "please select" item

Attached code works OK, so I'm sort of getting there.

But on refreshing the page (not part of the normal flow for Ajax, but it will happen), then currently the listbox state gets mangled. Box 1 shows last selected item - eg, North America. But boxes 2 and 3 show "please select" - although their contents are correct (USA, Canada) and (Alabama, Alaska).

When I refresh the page, then I want either the whole thing to get reset - only box one visible with "please select" selected. Or all 3 boxes should remember their previously selected state.

I'm sure this is a fairly typical scenario. I'd appreciate any pointers on how to do this correctly.

Thanks.

Code:
 public class AreaForm implements InitializingBean {
 
 	private static final long serialVersionUID = 8886935393782914371L;
 
 	protected final Log log = LogFactory.getLog(getClass());
 
 	private AreaManger areaManager = null;
 
 	private HtmlSelectOneMenu menuOne;
 
 	private HtmlSelectOneMenu menuTwo;
 
 	private HtmlSelectOneMenu menuThree;
 
 	private Effect menuEffect;
 
 	private boolean flag;
 	
 	private String selectedOne; 
 	private String selectedTwo; 
 	private String selectedThree;
 
 	public void afterPropertiesSet() throws Exception {
 		log.debug("\n\n properties set in AreaForm managed bean set \n\n");
 	}
 
 	private List<SelectItem> createSelectItems(List<Area> areas) {
 		log.debug("creating selected items");
 		List<SelectItem> list = new ArrayList<SelectItem>();
 		for (Iterator<Area> iter = areas.iterator(); iter.hasNext();) {
 			Area area = iter.next();
 			SelectItem si = new SelectItem(String.valueOf(area.getId()), area
 					.getName());
 			list.add(si);
 		}
 		return list;
 	}
 
 	public AreaManger getAreaManager() {
 		return areaManager;
 	}
 
 	@SuppressWarnings("unchecked")
 	public HtmlSelectOneMenu getMenuOne() {
 		log.debug("getting level one menu");
 		if (menuOne == null) {
 			menuOne = new HtmlSelectOneMenu();
 		}
 		if (menuOne.getChildren().size() == 0) {
 			List<SelectItem> list = createSelectItems(areaManager.getLevelOne());
 			list.add(0, new SelectItem("99999", "Please select..."));
 			UISelectItems items = new UISelectItems();
 			items.setValue(list);
 			menuOne.getChildren().clear();
 			menuOne.getChildren().add(items);
 		}
 		log.debug("level one menu " + menuOne.getValue());
 		return menuOne;
 	}
 
 	public HtmlSelectOneMenu getMenuTwo() {
 		log.debug("getting level two menu");
 		if (menuTwo == null) {
 			menuTwo = new HtmlSelectOneMenu();
 			menuTwo.setVisible(false);
 		} 
 		log.debug("level two menu " + menuTwo.getValue());
 		return menuTwo;
 	}
 
 	public HtmlSelectOneMenu getMenuThree() {
 		log.debug("getting level three menu");
 		if (menuThree == null) {
 			menuThree = new HtmlSelectOneMenu();
 			menuThree.setVisible(false);
 		}
 		log.debug("level three menu " + menuThree.getValue());
 		return menuThree;
 	}
 
 	public void menuOneValueChanged(ValueChangeEvent event) {
 		selectedOne = (String) event.getNewValue();
 		log.debug("menu one changed : " + selectedOne);
 		resetListMenu(menuTwo, selectedOne);
 		resetListMenu(menuThree, null);
 	}
 
 	public void menuTwoValueChanged(ValueChangeEvent event) {
 		selectedTwo = (String) event.getNewValue();
 		log.debug("menu two changed : " + selectedTwo);
 		resetListMenu(menuThree, selectedTwo);
 	}
 
 	public void menuThreeValueChanged(ValueChangeEvent event) {
 		selectedThree = (String) event.getNewValue();
 		log.debug("menu three changed : " + selectedThree);
 	}
 
 	public Effect getMenuEffect() {
 		invokeMenuEffect();
 		return menuEffect;
 	}
 
 	public void setMenuEffect(Effect effect) {
 		invokeMenuEffect();
 	}
 
 	public String invokeMenuEffect() {		
 		menuEffect = new Highlight();		
 		flag = !flag;
 		return null;
 	}
 
 	@SuppressWarnings("unchecked")
 	private void resetListMenu(HtmlSelectOneMenu menu, String id) {
 		log.debug("resetting menu :: " + id + " :: " + menu);
 		menu.getChildren().clear();
 		List<Area> subAreas = areaManager.getSubAreas(id);
 		if (subAreas.isEmpty()) {
 			log.debug("invisible...");
 			menu.setVisible(false);
 		} else {
 			List<SelectItem> list = createSelectItems(subAreas);
 			list.add(0, new SelectItem("99999", "Please select..."));
 			UISelectItems items = new UISelectItems();
 			items.setValue(list);
 			menu.getChildren().add(items);
 			log.debug("visible...");
 			menu.setVisible(true);
 		}
 	}
 
 	public void setAreaManager(AreaManger areaManager) {
 		log.debug("setting area manager : " + areaManager);
 		this.areaManager = areaManager;
 	}
 
 	public void setMenuOne(HtmlSelectOneMenu menu) {
 		log.debug("setting level one menu " + menu);
 		this.menuOne = menu;
 	}
 
 	public void setMenuTwo(HtmlSelectOneMenu menu) {
 		log.debug("setting level two menu " + menu);
 		this.menuTwo = menu;
 	}
 
 	public void setMenuThree(HtmlSelectOneMenu menu) {
 		log.debug("setting level three menu " + menu);
 		this.menuThree = menu;
 	}
 
 	public void submit() {
 		log.debug("form summitted");
 	}
 }



Code:
 
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
 	xmlns:ui="http://java.sun.com/jsf/facelets"
 	xmlns:h="http://java.sun.com/jsf/html"
 	xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en"
 	xmlns:ice="http://www.icesoft.com/icefaces/component">
 <head>
 	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 	<title>Ajax example - icefaces</title>
 	<link href="" rel="stylesheet" type="text/css" />
 </head>
 <f:view>
 <body>
 <h1>Ajax example with icefaces</h1>
 <ice:form id="frm1">
 		<div id="lev1">
 			<ice:selectOneMenu id="level1" 
 				binding="#{areaForm.menuOne}"
 				valueChangeListener="#{areaForm.menuOneValueChanged}"
 				partialSubmit="true" 
 				onchange="submit()">
 				<f:selectItems id="si1" value="#{areaForm.menuOne}" />
 			</ice:selectOneMenu>
 		</div>
 
 		<div id="lev2">
 			<ice:selectOneMenu id="level2" 				
 				binding="#{areaForm.menuTwo}"
 				valueChangeListener="#{areaForm.menuTwoValueChanged}"
 				partialSubmit="true" 
 				onchange="submit()"
 				effect="#{areaForm.menuEffect}">
 				<f:selectItems id="si2" value="#{areaForm.menuTwo}" />
 			</ice:selectOneMenu>
 		</div>
 
 		<div id="lev3">
 			<ice:selectOneMenu id="level3" 				
 				binding="#{areaForm.menuThree}"
 				valueChangeListener="#{areaForm.menuThreeValueChanged}"
 				partialSubmit="true" 
 				onchange="submit()"
 				effect="#{areaForm.menuEffect}">
 				<f:selectItems id="si3" value="#{areaForm.menuThree}" />
 			</ice:selectOneMenu>
 		</div>
 
 	</ice:form>
 
 	<h:form id="dwr">
 		<h:commandLink action="dwr" id="dwrLink" value="Ajax DWR example" />
 	</h:form>
 	
 </body>
 </f:view>
 </html>
 
 



Note - I see this in my logs, so it looks like the selected value of boxes 2 and 3 are being maintained on the server

- level two menu 62
- level three menu 70

but the listboxes display "please select" - value "99999".


Code:
DEBUG [http-8080-3] AreaForm.setMenuOne(160) | setting level one menu com.icesoft.faces.component.ext.HtmlSelectOneMenu@b125bd
 DEBUG [http-8080-3] AreaForm.setMenuTwo(165) | setting level two menu com.icesoft.faces.component.ext.HtmlSelectOneMenu@24d517
 DEBUG [http-8080-3] AreaForm.setMenuThree(170) | setting level three menu com.icesoft.faces.component.ext.HtmlSelectOneMenu@2219b4
 DEBUG [http-8080-3] AreaForm.getMenuOne(66) | getting level one menu
 DEBUG [http-8080-3] AreaForm.getMenuOne(78) | level one menu 577
 DEBUG [http-8080-3] AreaForm.getMenuOne(66) | getting level one menu
 DEBUG [http-8080-3] AreaForm.getMenuOne(78) | level one menu 577
 DEBUG [http-8080-3] AreaForm.getMenuTwo(83) | getting level two menu
 DEBUG [http-8080-3] AreaForm.getMenuTwo(88) | level two menu 62
 DEBUG [http-8080-3] AreaForm.getMenuTwo(83) | getting level two menu
 DEBUG [http-8080-3] AreaForm.getMenuTwo(88) | level two menu 62
 DEBUG [http-8080-3] AreaForm.getMenuThree(93) | getting level three menu
 DEBUG [http-8080-3] AreaForm.getMenuThree(98) | level three menu 70
 DEBUG [http-8080-3] AreaForm.getMenuThree(93) | getting level three menu
 DEBUG [http-8080-3] AreaForm.getMenuThree(98) | level three menu 70
 DEBUG [http-8080-3] AreaForm.setMenuOne(160) | setting level one menu com.icesoft.faces.component.ext.HtmlSelectOneMenu@b125bd
 DEBUG [http-8080-3] AreaForm.setMenuTwo(165) | setting level two menu com.icesoft.faces.component.ext.HtmlSelectOneMenu@24d517
 DEBUG [http-8080-3] AreaForm.setMenuThree(170) | setting level three menu com.icesoft.faces.component.ext.HtmlSelectOneMenu@2219b4
 DEBUG [http-8080-3] AreaForm.getMenuOne(66) | getting level one menu
 DEBUG [http-8080-3] AreaForm.getMenuOne(78) | level one menu 577
 DEBUG [http-8080-3] AreaForm.getMenuOne(66) | getting level one menu
 DEBUG [http-8080-3] AreaForm.getMenuOne(78) | level one menu 577
 DEBUG [http-8080-3] AreaForm.getMenuTwo(83) | getting level two menu
 DEBUG [http-8080-3] AreaForm.getMenuTwo(88) | level two menu 62
 DEBUG [http-8080-3] AreaForm.getMenuTwo(83) | getting level two menu
 DEBUG [http-8080-3] AreaForm.getMenuTwo(88) | level two menu 62
 DEBUG [http-8080-3] AreaForm.getMenuThree(93) | getting level three menu
 DEBUG [http-8080-3] AreaForm.getMenuThree(98) | level three menu 70
 DEBUG [http-8080-3] AreaForm.getMenuThree(93) | getting level three menu
 DEBUG [http-8080-3] AreaForm.getMenuThree(98) | level three menu 70
 
philip.breau


Joined: 08/May/2006 00:00:00
Messages: 2989
Offline


Hi,

The values you're setting in the valueChangeListeners are probably being overwritten in the next JSF phase. This is due to the way the JSF Lifecycle functions and ordering of the particular phases in the lifecyle. Check out this post for workarounds: http://www.icefaces.org/JForum/posts/list/4655.page .

Philip

.
xsteviex

Joined: 29/Apr/2007 00:00:00
Messages: 16
Offline


Thanks. Seems to be working OK now that I've remembered to change the backing bean scope session.
 
Forum Index -> General Help
Go to:   
Powered by JForum 2.1.7ice © JForum Team