Press enter to see results or esc to cancel.


How to populate options of h:selectOneMenu from database? JAVA JSF

  1. The Project Structure in netbeans is shown
  2. Mysql Table Structure
    CREATE TABLE `categories` (
    	`sl_no` INT(11) NOT NULL AUTO_INCREMENT,
    	`category_name` VARCHAR(50) NOT NULL DEFAULT '0',
    	PRIMARY KEY (`sl_no`)
    )
    

    MySql Table Data

  3. index.xhtml
    <?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:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            <h:form>
                Name of Student<h:inputText value="#{form_data.student_name}"></h:inputText><br></br>
                Category
                <h:selectOneMenu value="#{form_data.category}">
                    <f:selectItems value="#{form_data.get_category_name()}"></f:selectItems>
                </h:selectOneMenu><br></br>
                <h:commandButton value="Submit" action="success"></h:commandButton>
            </h:form>
        </h:body>
    </html>
    
  4. success.xhtml
    <?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:h="http://xmlns.jcp.org/jsf/html">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            #{form_data.student_name}<br></br>
            #{form_data.category}
        </h:body>
    </html>
  5. Form_Data.java
    package com;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    @ManagedBean(name = "form_data")
    @RequestScoped
    public class Form_Data {
     	private String student_name;
     	private String category;
     	private List<String> category_list=new ArrayList<>();
     	public List<String> get_category_name(){
     	try {
     	 	Connection connection=null;
     	 	Class.forName("com.mysql.jdbc.Driver");
     	 	connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
     	 	PreparedStatement ps=null;
     	 	ps=connection.prepareStatement("select * from categories");
     	 	ResultSet rs=ps.executeQuery();
     	 	while(rs.next()){
     	 	 	category_list.add(rs.getString("category_name"));
     	 	}
     	} catch (Exception e) {
     	 	 System.out.println(e);
     	}
     	return category_list;
     	}
     	public Form_Data() {
     	}
     	public String getStudent_name() {
     	 	return student_name;
     	}
     	public String getCategory() {
     	 	return category;
     	}
     	public List<String> getCategory_list() {
     	 	return category_list;
     	}
     	public void setStudent_name(String student_name) {
     	 	this.student_name = student_name;
     	}
     	 public void setCategory(String category) {
     	 	this.category = category;
     	}
     	public void setCategory_list(List<String> category_list) {
     	 	this.category_list = category_list;
     	}
    }
  6. index.xhtml
  7. After Submit success.xhtml

    Download the project here JSF_Hibernate_loginProject_1

Comments

Leave a Comment