无法从jsf页面中的bean获得价值

时间:2019-03-18 08:05:52

标签: jsf

尝试在使用jsp之后处理jsf,因此具有挑战性。所以问题是我无法获取Java bean类的值。当我尝试在控制台中进行打印时,一切正常,但无法在页面

中获得结果

这是我的主要jsf页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
<h:head>
    <meta charset="UTF-8" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <h:outputStylesheet library="styles" name="style.css"/>
    <title>Test</title>    
</h:head>
<body>
<f:view> 
<h:form>
        <h:panelGrid columns="2">
            <h:outputLabel value="First name" for="firstName"/>
            <h:inputText id="firstName" value="#{userBean.firstName}"/>

            <h:outputLabel value="Last name" for="lastName"/>
            <h:inputText id="lastName" value="#{userBean.lastName}"/>

            <h:commandButton action="pages/user2?faces-redirect=true" value="send"/>
        </h:panelGrid>
    </h:form>
</f:view> 
</body>
</html>

我的面孔配置类似

<?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config version="2.1"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">
<managed-bean>
        <managed-bean-name>userBean</managed-bean-name>
        <managed-bean-class>beans.User</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
<application>
    <resource-bundle>
        <base-name>locales.messages</base-name>
        <var>msg</var>
    </resource-bundle>
</application>

</faces-config>

我的User.java

package beans;

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
        System.out.println("firstname is "+ firstName);
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
        System.out.println("lastName is "+ lastName);
    }
}

enter image description here

我的user2页面必须显示结果

<f:view> 
    name is : <h:outputText value="#{userBean.firstName}" />
</f:view>

服务器是tomcat,是日蚀。不知道如何解决,请帮忙!

1 个答案:

答案 0 :(得分:-3)

您已经在类中将firstname和lastName声明为私有字段,并且要访问它们,您必须具有两个字段的getter方法。因此,使用这些方法显示数据。控制台显示数据是因为您是从bean类编写的。

 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:ui="http://java.sun.com/jsf/facelets">
 <h:head>
  <meta charset="UTF-8" />
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
 <h:outputStylesheet library="styles" name="style.css"/>
 <title>Test</title>    
 </h:head>
 <body>
 <f:view> 
 <h:form>
    <h:panelGrid columns="2">
        <h:outputLabel value="First name" for="firstName"/>
        <h:inputText id="firstName" value="#{userBean.getFirstName}"/>

        <h:outputLabel value="Last name" for="lastName"/>
        <h:inputText id="lastName" value="#{userBean.getLastName}"/>
 <h:commandButton action="pages/user2?facesredirect=true"value="send"/>
    </h:panelGrid>
</h:form>
</f:view> 
</body>
</html>

希望您能得到我所说的更改您的代码