Craig
Craig

Reputation: 1

Java Bean getter not returning values

I am trying to design a simple java bean/xhtml setup for a homework assignment. The implementation seems simple enough, however I can not get GlassFish to pull the info from the java bean and display it in the HTML.

I wrote my code, and then created a war file and loaded that into GlassFish autodeploy.

Here is 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>

Here is my properties.java file located in 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;
}

}

Here is my web.xml file located in root/WEB-INF/

<?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>

Last but not least my faces-config.xml file located 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>

What I see on my screen is "The System's os name is" and then blank. It is the same for the other two values. I created a traditional application to ensure that System.getProperties was working on my computer. I also initialized the properties.VALUES with different strings, just to make sure. I have been banging my head on a wall for the past six hours on this one. Again, this all seems like it should make sense based off different tutorials, textbooks and youtube video's I have researched. Any insight would be great!

Upvotes: 0

Views: 1134

Answers (2)

AllTooSir
AllTooSir

Reputation: 49362

This is wrong way to access the bean properties using EL:

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

It should be

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

Since getJavaVersion() is the getter , hence EL will look for javaVersion property. Same applies to other fields. If you have a bean class :

public class Foo {
   int bar;

   public int getBar(){
     return bar;
   }
}

The bar property should be accessed as #{bean.bar}.

Upvotes: 1

Sqeezer
Sqeezer

Reputation: 1277

You must use EL language in order to access beans fields. That means that to invoke bean's getters you must use getter method but without standard prefix, which is get/is. Therefore you should use in xhtml:

properties.javaVersion
properties.osName
properties.osVersion

Please note xhtml doesn't read values from the fields directly, it reads only getters methods.

Upvotes: 0

Related Questions