<h:selectOneMenu>不会使用手动填充的值生成<options>

时间:2019-10-22 19:00:42

标签: jsf

我有一个查看页面addFilm.xhtml,在其中必须提示用户输入电影的mpaa评分。我正在使用<h:selectOneMenu>来生成一个保管箱:

        <h:selectOneMenu value="#{film.mpaa}">
            <f:selectItem itemValue="G" itemLabel="G" />
            <f:selectItem itemValue="PG" itemLabel="PG" />
            <f:selectItem itemValue="PG-13" itemLabel="PG-13" />
                <f:selectItem itemValue="R" itemLabel="R" />
        </h:selectOneMenu> 

其中film是我的托管bean,而mpaa是其属性之一。在浏览器上显示 only the label

在检查元素时,我可以看到jsf仅生成了组合框,而没有生成选项

<select name="createFilmForm:j_id707420064_2a2a5e1a" size="1"></select>

为什么要这样做呢?

下面的编辑是完整的查看页面:

<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1" http-equiv="X-UA-Conpatible" />
    <link rel="stylesheet" type="text/css" href="./lib/bootstrap/css/bootstrap.min.css" /> 
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script src="https://kit.fontawesome.com/a076d05399.js"></script>
    <title>Add new Film</title>
    <style type="text/css">
        .btnWidth {
            width: 80px;
        }   
        body{
            overflow-y:scroll !important;
        }  
    </style>
</h:head>
<h:body>
    <h:form id="createFilmForm" class="form-horizontal">
        <div class="form-group">
            <div class="col-sm-4"></div>
            <div class="col-sm-4">
                <h2>Add new Film</h2>
            </div>
        </div>
        <hr/>
        <div class="form-group">


        <div class="form-group">
            <h:outputLabel class="control-label col-sm-4">Title</h:outputLabel>
            <div class="col-sm-4">
                <h:inputText id="film-title" value="#{film.title}" class="form-control" validatorMessage="Title is required">
                    <f:validateRequired/>
                </h:inputText>
            </div>
        </div>
        <div class="form-group">
            <h:outputLabel class="control-label col-sm-4">Year</h:outputLabel>
            <div class="col-sm-4">
                <h:inputText id="year-id" value="#{film.year}" class="form-control" validatorMessage="Year is required">
                    <f:validateRequired/>
                </h:inputText>
            </div>
        </div>   


        <div class="form-group">
            <h:outputLabel class="control-label col-sm-4">MPAA</h:outputLabel>
            <div class="col-sm-4">
                <h:selectOneMenu value="#{film.mpaa}">
                    <f:selectItem itemValue="G" itemLabel="G" />
                    <f:selectItem itemValue="PG" itemLabel="PG" />
                    <f:selectItem itemValue="PG-13" itemLabel="PG-13" />
                        <f:selectItem itemValue="R" itemLabel="R" />
                </h:selectOneMenu> 
            </div>
        </div>         


        <div class="form-group">
            <div class="col-sm-4"></div>
            <div class="col-sm-4">
                <div class="col-sm-2">
                    <h:commandButton value="Save" action="#{film.saveFilmDetails()}" class="btn btn-success btnWidth" />
                </div>
                <div class="col-sm-1">
                </div>
                <div class="col-sm-2">
                    <h:link outcome="index" value="View film List" class="btn btn-primary" />
                </div>
            </div>
        </div>
    </h:form>
</h:body>
</html>

ManagedBean:

    package project.cinema.poo;

    import java.sql.SQLException;


    import java.util.ArrayList;
    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import project.cinema.poo.Film;
    import controllers.DBO;

    @ManagedBean(name="film")
    @SessionScoped

    public class FilmBean {

        public String title;
        private String duration;
        private String mpaa;
        private String year;
        private String id;
        public List<Film> films = new ArrayList<Film>();

        //Recuperer la liste des films
        public List<Film> getFilms() throws SQLException{
            return DBO.retrieveFromDB();
        }

        //inserer un film
        public String saveFilmDetails() throws SQLException {
            System.out.println(this.getTitle());
            return DBO.saveInDB(this);
        }

        public String deleteFilm(String filmId) throws SQLException {
            return DBO.deleteFromDB(filmId);
        }

        public String updateFilm(Film film) {
            return DBO.editFromDB(film);
        }

        public static String editFilmRecord(String filmId) {
            Film editRecord = new Film();
            editRecord = DBO.prepareEditObj(filmId);
            System.out.println("editFilmRecord() : Film Id: " + editRecord.getId());
            return "/editFilm.xhtml?faces-redirect=true";
        }
}

我的dbo控制器类:

package controllers;
import java.sql.Connection;



import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import project.cinema.poo.DbConn;
import project.cinema.poo.Film;
import project.cinema.poo.FilmBean;

public class DBO {
    public static Statement stmtObj;
    public static Connection connObj;
    public static ResultSet result;
    public static PreparedStatement pstmt;

    private static List<Film> films = new ArrayList<Film>();


    public static List<Film> retrieveFromDB() throws SQLException
    {
        films.clear();
        DbConn lc = new DbConn();
        connObj = lc.getLocalConnection();
        stmtObj = connObj.createStatement();
        String sql1="SELECT f_id, f_title, f_mpaa, f_year FROM tbl_films";
        System.out.println("****"+sql1);
        result = stmtObj.executeQuery(sql1);
        while(result.next()) {
            films.add(new Film(result.getString(1), result.getString(2), result.getString(3), result.getString(4)));
        }
        lc.setConnectionClose();
        return films;       
    }


    public static String saveInDB(FilmBean newFilmObj) throws SQLException {

        int saveResult = 0;
        System.out.println(newFilmObj.getTitle());
        String navigationResult = "";
        try { 
            DbConn lc = new DbConn();
            connObj = lc.getLocalConnection();
            stmtObj= connObj.createStatement();
            pstmt = connObj.prepareStatement("INSERT INTO tbl_films(f_title, f_mpaa, f_year) values (?, ?, ?)");         
            pstmt.setString(1, newFilmObj.getTitle());
            pstmt.setString(2, newFilmObj.getMpaa());
            pstmt.setString(3, newFilmObj.getYear());
            saveResult = pstmt.executeUpdate();
            connObj.close();
        } catch(Exception sqlException) {
            sqlException.printStackTrace();
        }
        if(saveResult !=0) {
            navigationResult = "index.xhtml?faces-redirect=true";
        } else {
            navigationResult = "addFilm.xhtml?faces-redirect=true";
        }
        return navigationResult;
    }



    public static String deleteFromDB(String filmId) throws SQLException {
        DbConn lc = new DbConn();
        connObj = lc.getLocalConnection();
        System.out.println("deleteFilmInDB() : Film Id: " + filmId);
        try {
            pstmt = connObj.prepareStatement("delete from tbl_films where f_id = "+ filmId);  
            pstmt.executeUpdate();          
        } 
        catch(Exception sqlException){
            sqlException.printStackTrace();
            System.out.println(sqlException);
        }      
        lc.setConnectionClose();
        return "/index.xhtml?faces-redirect=true";
    }



}

数据库将是:

CREATE TABLE tbl_films (
f_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
f_title VARCHAR(255), 
f_year VARCHAR(8), 
f_mpaa VARCHAR(6), 
);

我正在使用eclipse 2019-06

0 个答案:

没有答案