Search code examples
jsfjakarta-eeglassfishjavadb

No View in JSP Example


I have a problem with the view in JSP (Java EE) Only the heading is shown.

My Code:

Entitiy Class (Konto);

@Entity
public class Konto implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable=false)
    @NotNull(message="Kontonummer muss angegenben werden")
    @Pattern(regexp="[0-9][0-9][0-9][0-9]")
    private String kontonummer;

    @Column(nullable=false)
    @NotNull(message="Kontostand muss angegeben werden")
    @DefaultValue(value="0.0")
    private Double ktostd;

    @Column(nullable=false)
    @DecimalMin(value="0", message="Der Zins muss zw. 0 und 10 % liegen")
    @DecimalMax(value="0.1", message="Der Zins muss zw. 0 und 10 % liegen")
    private Double habenZins;

    @ManyToOne
    @JoinColumn(nullable=false)
    @NotNull(message="Besitzer muss angegeben werden")
    private Besitzer besitzer;

    public Besitzer getBesitzer() {
        return besitzer;
    }

    public void setBesitzer(Besitzer besitzer) {
        this.besitzer = besitzer;
    }

    public Double getHabenZins() {
        return habenZins;
    }

    public void setHabenZins(Double habenZins) {
        this.habenZins = habenZins;
    }

    public String getKontonummer() {
        return kontonummer;
    }

    public void setKontonummer(String kontonummer) {
        this.kontonummer = kontonummer;
    }

    public Double getKtostd() {
        return ktostd;
    }

    public void setKtostd(Double ktostd) {
        this.ktostd = ktostd;
    }





    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Konto)) {
            return false;
        }
        Konto other = (Konto) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "at.korn.entity.NewEntity[ id=" + id + " ]";
    }

}

Kontolist.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Kontoliste</h1>
        <h:form>
        <h:dataTable value="#{kontolist.kontos}" var="konto">
            <h:column>
                <f:facet name="header">
                    <h:outputText value="Kontonummer"></h:outputText>
                </f:facet>
                <h:outputText value="#{konto.kontonummer}"></h:outputText>
            </h:column>

        </h:dataTable>
            </h:form>
    </h:body>
</html>

KontoList Controller:

@ManagedBean
@SessionScoped
public class Kontolist {
    @EJB
    KontoFacadeLocal kontofacade;

    private List<Konto> kontos;
    /** Creates a new instance of kontolist */
    public Kontolist() {
        kontos = kontofacade.findAll();
    }

    public KontoFacadeLocal getKontofacade() {
        return kontofacade;
    }

    public void setKontofacade(KontoFacadeLocal kontofacade) {
        this.kontofacade = kontofacade;
    }

    public List<Konto> getKontos() {
        setKontos(kontofacade.findAll());
        return kontos;
    }

    public void setKontos(List<Konto> kontos) {
        this.kontos = kontos;
    }

}

Problem:

Only the header is shown. In the source from the browser is the same code without html injection (like value="#{konto.kontonummer}")


Solution

  • First of all, that is not a JSP file. That's a Facelets (XHTML) file. JSP is an ancient view technology. Facelets is the successor of JSP.

    So, your concrete problem is that the JSF tags are not been parsed? That can happen when the request URL did not match the URL pattern of the FacesServlet as definied in web.xml. If it is for example *.jsf, then you'd need to change the request URL from

    http://localhost:8080/contextname/kontolist.xhtml

    to

    http://localhost:8080/contextname/kontolist.jsf

    However, much better is to just change the URL pattern of the FacesServlet to *.xhtml so that you do not need to fiddle with virtual URLs and introduce security constraints to prevent the enduser from accidently or awaringly viewing the raw *.xhtml pages.

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    

    See also:


    Unrelated to the concrete problem, you've by the way a NullPointerException bug in your code. Replace

    public Kontolist() {
        kontos = kontofacade.findAll();
    }
    

    by

    @PostConstruct
    public void init() {
        kontos = kontofacade.findAll();
    }
    

    Injected dependencies are namely not available during construction. The getter and setter for the kontofacate are also entirely superfluous, I'd remove them to prevent future confusion and abuse.