Компонент JSF CDI не будет читать параметр и go на следующую страницу JSF - PullRequest
0 голосов
/ 01 августа 2020

В настоящее время на странице, где перечислены кинотеатры с кнопкой для показа фильмов, показанных в каждом кинотеатре (Theaters.x html):

например:

Театр 1 Кнопка «Показать фильмы» Кнопка «Показать фильмы» в Театре 2 и c ....

При нажатии кнопки она должна показывать от go до Movies.xhtml, где должны отображаться фильмы и их время сеанса с помощью метода listMovies() в ListOfMovies.java который является компонентом JSF CDI с областью сеанса. Он берет Theaterid из базы данных в качестве параметра, но, похоже, он не обрабатывает его правильно, или я вызываю что-то не так.

Theaters.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>Theaters</title>
    </h:head>
    <h:body>
        <h:form>
            <h:dataTable value="#{mainBean.theatersList}" var="theater">
                <h:column>
                    <h:outputText value="#{theater.theatername}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{theater.address}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{theater.city}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{theater.states}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{theater.zip}"/>
                </h:column>
                <h:column>
                    <h:commandButton value="Show Movies and Showtimes" action="#{listOfMovies.listMovies()}">
                        <f:param name="theaterid" value="#{listOfMovies.theater.theaterid}"/>
                    </h:commandButton>
                </h:column>
            </h:dataTable>
            
            <h:commandButton value="Enter another zip" action="index.xthml"/>
        </h:form>
    </h:body>
</html>

ListOfMovies.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cdi;

import ejb.TheaterEJB;
import entity.Movie;
import entity.Theater;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;

/**
 *
 * @author rmpei
 */
@Named(value = "listOfMovies")
@SessionScoped
public class ListOfMovies implements Serializable {
    
    
    @EJB
    private TheaterEJB theaterEJB;
    private Theater theater;
    private Movie movie;
    
    
    public ListOfMovies(){}

   
    public Theater getTheater() {
        return theater;
    }

    public void setTheater(Theater theater) {
        this.theater = theater;
    }

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }
    
    public String listMovies() {
        try {
            FacesContext fc = FacesContext.getCurrentInstance();
            Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
            int theaterid = Integer.parseInt(params.get("theaterid"));
            theater = theaterEJB.getTheaterByID(theaterid);
            return "Movies.xhtml";
        }
        catch(Exception e) {
            return "Sorry, there was an error.";
        }
    }
    
    public List<Movie> getMoviesList() {
        if(theater != null) {
            return theaterEJB.getMovies(theater.getTheaterid());
        }
        else {
            return null;
        }
    }
    
}

Movies.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>Movies</title>
    </h:head>
    <h:body>
        <h:form>
            <h:dataTable styleClass="movie" value="#{listOfMovies.getMoviesList()}" var="movies">
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Poster" styleClass="header"/>
                    </f:facet>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Movie Title"/>
                    </f:facet>
                    <h:outputText value="#{movies.title}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Genre"/>
                    </f:facet>
                    <h:outputText value="#{movies.genre}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Description"/>
                    </f:facet>
                    <h:outputText value="#{movies.description}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Rating"/>
                    </f:facet>
                    <h:outputText value="#{movies.rating}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Showing Time 1"/>
                    </f:facet>
                    <h:outputText value="#{movies.showing1}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Showing Time 2"/>
                    </f:facet>
                        <h:outputText value="#{movies.showing2}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Showing Time 3"/>
                    </f:facet>
                    <h:outputText value="#{movies.showing3}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Showing Time 4"/>
                    </f:facet>
                    <h:outputText value="#{movies.showing4}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Showing Time 5"/>
                    </f:facet>
                    <h:outputText value="#{movies.showing5}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Purchase Ticket"/>
                    </f:facet>
                    <h:commandButton value="Purchase Ticket" action="#{purchaseTicket.purchaseTicket(movies)}"/>
                </h:column>
            </h:dataTable>
        </h:form>
    </h:body>
</html>
...