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!

h:selectOneMenu - get HashMap keys

user10484841Mar 15 2012 — edited Mar 15 2012
I have this JSF page which generates settings and values:
<div id="settingsdiv" style="width:350px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">
                    <h:form>
                    <h:panelGrid columns="2">
                        <h:panelGroup>User Session Timeout</h:panelGroup>
                        <h:panelGroup>
                            <h:selectOneMenu value="#{ApplicationController.settingValue('MaxUsersActive')}">
                                <f:selectItem itemValue="one" itemLabel="Option one" />
                                <f:selectItem itemValue="two" itemLabel="Option two" />
                                <f:selectItem itemValue="three" itemLabel="Option three" />
                                <f:selectItem itemValue="custom" itemLabel="Define custom value" />
                                <f:ajax render="input" />
                            </h:selectOneMenu>
                            <h:panelGroup id="input">
                                <h:inputText value="#{ApplicationController.settingValue('MaxUsersActive')}" rendered="#{ApplicationController.settingValue('MaxUsersActive') == 'custom'}" required="true" />
                            </h:panelGroup>
          
                        </h:panelGroup>
                        
                        <h:panelGroup>Maximum allowed users</h:panelGroup>
                        <h:panelGroup>#{ApplicationController.settingValue('MaxUsersActive')}</h:panelGroup>                                                                     
                    </h:panelGrid>                         
                        <h:commandButton value="Submit" action="#{ApplicationController.submit}"/>
                    </h:form> 
                  
 
                              
</div>   
I have this managed bean which is used to get the settings and the values from the database.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;   

import org.glassfish.osgicdi.OSGiService;

@Named("ApplicationController")
@SessionScoped
public class Application implements Serializable {

    /* This Hash Map will be used to store setting and value */
    private HashMap<String, String> settingsMap = null;
        
    public Application(){     
    }   
    
    /* Call the Oracle JDBC Connection driver */
    @Resource(name = "jdbc/Oracle")
    private DataSource ds;
    
    private String session = "custo";
    
    public String getsession() {
        return session;
    }
    
    public void setsession(String session) {
        this.session = session;
    }


    /* Get setting value using key */
    public String settingValue(String key)
    {
        try
        {
            return (String) settingsMap.get(key);
            
        }
        catch(Exception x) { return "error - " + x.getMessage(); }
        
    }
    
    /* Hash Map declaration */
    public HashMap<String, String> getSetting(String key)
    {
        return settingsMap;        
    }

    /* Get a Hash Map with settings and values. The table is genarated right 
     * after the constructor is initialized. 
     */
    @PostConstruct
    public void initSettings() throws SQLException
    {        
        settingsMap = new HashMap<String, String>();
        
        if(ds == null) {
                throw new SQLException("Can't get data source");
        }
        /* Initialize a connection to Oracle */
        Connection conn = ds.getConnection(); 

        if(conn == null) {
                throw new SQLException("Can't get database connection");
        }
        /* With SQL statement get all settings and values */
        PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS");

        try
        {
            //get data from database        
            ResultSet result = ps.executeQuery();
            while (result.next())
            {
               settingsMap.put(result.getString("SettingName"), result.getString("SettingValue"));
            }            
        }
        finally
        {
            ps.close();
            conn.close();         
        }        
    }
    
    
    public void SettingsDBUpdate(){
        
        
    }
    
      
}
When I run the page I get this error:
serverError: class javax.faces.component.UpdateModelException /Application.xhtml @36,110 value="#{ApplicationController.settingValue('MaxUsersActive')}": Illegal Syntax for Set Operation

I want to get from the hash map the value of the setting. But it seems that h:selectOneMenu is designed to get the value only with setter/getter. Is there a way to fix this problem?

Best Wishes
Peter

Edited by: user10484841 on Mar 15, 2012 8:07 AM

Edited by: EJP on 16/03/2012 09:13: added {noformat}
{noformat} tags instead of your h4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2012
Added on Mar 15 2012
2 comments
1,762 views