【问题标题】:Java Bean getter not returning valuesJava Bean getter不返回值
【发布时间】:2013-06-13 08:28:22
【问题描述】:

我正在尝试为家庭作业设计一个简单的 java bean/xhtml 设置。实现看起来很简单,但是我无法让 GlassFish 从 Java bean 中提取信息并将其显示在 HTML 中。

我编写了代码,然后创建了一个 war 文件并将其加载到 GlassFish 自动部署中。

这里是 index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
      <title>System information</title>
   </h:head>
   <h:body>
      <h:form>
        <p>           
            This system's java version is #{properties.getJavaVersion}
        </p>
        <p>
        This sytem's OS Name is #{properties.getOsName}
        </p>
        <p>
        This System's OS version is #{properties.getOsVersion}
        </p>
     </h:form>
  </h:body>
</html>

这是我的 properties.java 文件,位于 root/WEB-INF/classes/com/stansbury/

package com.stansbury;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ViewScoped
@ManagedBean

public class Properties {

public String javaVersion;
public String osName;
public String osVersion;

public String getJavaVersion() {
    javaVersion = System.getProperty("java.version");
    return javaVersion;
}

public String getOsName() {
    osName = System.getProperty("os.name");
    return osName;
}

public String getOsVersion() {
    osVersion = System.getProperty("os.version");
    return osVersion;
}

}

这是我位于 root/WEB-INF/ 中的 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
 <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
 </welcome-file-list>
 <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
 </context-param>
</web-app>

最后但并非最不重要的是我的 faces-config.xml 文件位于 root/META-INF:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
   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_0.xsd"
version="2.0">
</faces-config>

我在屏幕上看到的是“系统的操作系统名称是”,然后是空白。其他两个值也是一样的。我创建了一个传统应用程序来确保 System.getProperties 在我的计算机上运行。我还用不同的字符串初始化了 properties.VALUES,只是为了确保。在过去的六个小时里,我一直在用头撞墙。同样,根据我研究过的不同教程、教科书和 youtube 视频,这一切似乎都应该是有意义的。任何见解都会很棒!

【问题讨论】:

    标签: java jsf jakarta-ee jsf-2


    【解决方案1】:

    这是使用EL 访问bean 属性的错误方法:

    This system's java version is #{properties.getJavaVersion}
    

    应该是

    This system's java version is #{properties.javaVersion}
    

    由于 getJavaVersion() 是 getter ,因此 EL 将寻找 javaVersion 属性。同样适用于其他领域。如果你有一个 bean 类:

    public class Foo {
       int bar;
    
       public int getBar(){
         return bar;
       }
    }
    

    bar 属性应作为#{bean.bar} 访问。

    【讨论】:

    • 睡了一觉后,我决定尝试几下
    • 我应用了这些设置,但仍然无法正常工作。我决定尝试一个完全准备就绪的设置,我逐字逐句使用教科书作者,我将其打包成 WAR 文件并加载到 Glassfish 中。这可能是我打包文件的方式吗?我只需输入命令“jar csv getProperties.war”。根目录下的命令。我对随书附送的包裹做了同样的过程。由于没有一个 getter 返回任何东西,因此没有一个 war 包正确部署。是否有我需要在我的主机系统上配置的基本设置。
    • 对不起,我使用的命令是:jar cvf getProperties.war .
    • 我最终下载了 NetBeans 并使用它而不是 Eclipse。一切正常。谢谢大家。
    【解决方案2】:

    您必须使用EL language 才能访问bean 字段。 这意味着要调用 bean 的 getter,您必须使用 getter 方法但没有标准前缀,即 get/is。 因此你应该在 xhtml 中使用:

    properties.javaVersion
    properties.osName
    properties.osVersion
    

    请注意,xhtml 不直接从字段中读取值,它只读取 getters 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 2019-10-09
      相关资源
      最近更新 更多