Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Populate Form field values for onchange event of h:selectOneMenu ?

843844May 3 2008 — edited May 20 2008
Hi,

I have developed a small JSF application using MyEclipse,Tomcat 6.0 and MySQL 5.0.51.

I am stuck with the onchange event of <h:selectOneMenu .. >. The problem is when I select a new User ID (on 'modifyUserDetails.jsp' page) the form fields should be loaded with that User details.

When I select a new user id, the control goes to the getUserDetails(ValueChangeEvent event) method and successfully prints the user details to the console. But the details are not populated on the same jsp page.

What should I do / change inorder to achieve this??

the modifyUserDetails.jsp page :-
************************************

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Test App - Modify User Details</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<f:loadBundle basename="com.test.bundle" var="rscBundle"/>
<f:view>
<h:form id="modifyUserFrm">
<h:panelGrid id="modifyUserPanelGrid" columns="2">
<f:facet name="header">
<h:outputLabel id="modifyUserPanelGridHeader" value="#{rscBundle.modifyUserDetails}"/>
</f:facet>
<h:outputLabel id="userIdLbl" for="userId" value="#{rscBundle.userId}"/><h:selectOneMenu id="userId" onchange="submit()" valueChangeListener="#{modifyUserBean.getUserDetails}"> <f:selectItems value="#{modifyUserBean.userIds}"/></h:selectOneMenu>
<h:outputLabel id="userNameLbl" for="userName" value="#{rscBundle.userName}"/><h:inputText id="userName" value="#{modifyUserBean.userName}" maxlength="30" size="28"/>
</h:panelGrid>
<h:panelGrid id="datePickerPanelGrid" columns="3">
<h:outputLabel id="userDobLbl" for="dob" value="#{rscBundle.userDob}"/><h:inputText id="dob" value="#{modifyUserBean.dob}" maxlength="10" size="25" disabled="false"/><input type="button" id="dateEllipsis" name="dateEllipsis" value=".." title="Date Picker" onClick="javascript:displayDatePicker('createUserFrm:dob',false,'dmy','/');">
</h:panelGrid>
<h:panelGrid id="raminingPanelGrid" columns="2">
<!-- <h:outputLabel id="userSexLbl" for="sex" value="#{rscBundle.userSex}"/><h:selectOneRadio id="sex" value="#{createUserBean.sex}"><f:selectItem itemValue="male" itemLabel="Male"/><f:selectItem itemValue="female" itemLabel="Female"/></h:selectOneRadio> -->
<h:outputLabel id="userAddressLbl" for="address" value="#{rscBundle.userAddress}"/><h:inputTextarea id="address" value="#{modifyUserBean.address}" cols="22" rows="4"/>
<h:outputLabel id="userPhoneLbl" for="phone" value="#{rscBundle.userPhone}"/><h:inputText id="phone" value="#{modifyUserBean.phone}" maxlength="10" size="28" />
<h:outputLabel id="userEmailLbl" for="email" value="#{rscBundle.userEmail}"/><h:inputText id="email" value="#{modifyUserBean.email}" maxlength="30" size="28" />
<h:outputLabel id="userRoleLbl" for="role" value="#{rscBundle.userRole}"/><h:selectOneMenu id="role" value="#{modifyUserBean.role}"><f:selectItem itemValue="" itemLabel="[ select ]"/><f:selectItem itemValue="admin" itemLabel="ADMIN"/><f:selectItem itemValue="general" itemLabel="GENERAL USER"/></h:selectOneMenu>
<h:commandButton id="submitBtn" value="#{rscBundle.updateBtnLbl}"/><h:commandButton id="clearBtn" type="reset" value="#{rscBundle.clearBtnLbl}"/>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>



and the corresponding backing bean - ModifyUserBean .java :-
**********************************************************************

package com.test.backingbeans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.org.apache.commons.beanutils.PropertyUtils;
import com.test.bizz.User;
import com.test.bizz.UserDetails;

public class ModifyUserBean {
private int intUserId;
private String strUserName;
private String strPassword;
private String strDob;
private String strSex;
private String strAddress;
private String strPhone;
private String strEmail;
private String strRole;
private List<SelectItem> userIds;

public int getUserId() {
return intUserId;
}
public void setUserId(int intUserId) {
this.intUserId = intUserId;
}
public String getUserName() {
return strUserName;
}
public void setUserName(String strUserName) {
this.strUserName = strUserName;
}
public String getPassword() {
return strPassword;
}
public void setPassword(String strPassword) {
this.strPassword = strPassword;
}
public String getDob() {
return strDob;
}
public void setDob(String strDob) {
this.strDob = strDob;
}
public String getSex() {
return strSex;
}
public void setSex(String strSex) {
this.strSex = strSex;
}
public String getAddress() {
return strAddress;
}
public void setAddress(String strAddress) {
this.strAddress = strAddress;
}
public String getPhone() {
return strPhone;
}
public void setPhone(String strPhone) {
this.strPhone = strPhone;
}
public String getEmail() {
return strEmail;
}
public void setEmail(String strEmail) {
this.strEmail = strEmail;
}
public String getRole() {
return strRole;
}
public void setRole(String strRole) {
this.strRole = strRole;
}

public List<SelectItem> getUserIds(){
userIds = new ArrayList<SelectItem>();
UserDetails userDetails=new UserDetails();
try{
ArrayList tempArrayList=userDetails.getUserIds();
Iterator it=tempArrayList.iterator();
userIds.add(new SelectItem("[ select ]"));
while(it.hasNext()){
userIds.add(new SelectItem(it.next()));
}
}
catch(Exception e){
System.out.println("Backing Bean - Error Getting User Ids :"+e.getMessage());
}
return userIds;
}

public void getUserDetails(ValueChangeEvent event){
UserDetails userDetails=new UserDetails();
try{
if (null != event.getNewValue()){
String temp=(String)event.getNewValue();
User user=userDetails.getUserDetails(temp);
FacesContext context=FacesContext.getCurrentInstance();
HttpServletRequest request=(HttpServletRequest)context.getExternalContext().getRequest();
HttpSession session=request.getSession();
ModifyUserBean modifyUserBean=(ModifyUserBean)session.getAttribute("modifyUserBean");
PropertyUtils.copyProperties(modifyUserBean, user);
System.out.println(getUserId()+","+getUserName()+","+getAddress()+","+getDob());
session.setAttribute("modifyUserBean", modifyUserBean);
}
}
catch(Exception e){
System.out.println("Backing Bean - Error . . . . . ###");
}
}

public String modifyUser(){

return "failure";
}
}


Regards,
Shiva
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2008
Added on May 3 2008
10 comments
638 views