1
votes

I posted this question on the primefaces forum a couple of days ago but so far I have not got a solution. I have been trying to use OneRadio but I have been getting only null values from itemValue(s). This is a part of a large project so I extracted only relevant code and made a simple Dynamic Web Project (Eclipse Mars.1) with a view and a simple bean that test the case. The null values instead of itemValue appear regardless of using ajax or commandButton. I would appreciate your help in solving this puzzling phenomenon:

This is the view:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
 xmlns:p="http://primefaces.org/ui"
 xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
<ui:remove>
      <meta http-equiv="Content-Type"
         content="text/html; charset=ISO-8859-1" />/>
   <title>Project</title>
   </ui:remove>
</h:head>
<body>
   <h:form>
      <p:layout style="width:190px; min-height:250px;">
       <p:layoutUnit position="center">
        <h:outputLabel for="strStatus" value="Project Status" />
        <p:selectOneRadio id="strStatus" value="testRadio.strStatus">
           <f:selectItem itemLabel="Active" itemValue="true" />
           <f:selectItem itemLabel="Inactive" itemValue="false" />
           <p:ajax event="click" listener="#{testRadio.onUpdate}" />
        </p:selectOneRadio>
        <p:commandButton style="width: 170px" value="Update Status"
           actionListener="#{testRadio.onUpdate}"/>
     </p:layoutUnit>
  </p:layout>
   </h:form>
</body>
</html>

This is the backing bean. One suggestion was not to use both @Named and @ManagedBean in the same class and I tried this correction but with no improvement.

package com.projectmanage.ui;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;
import org.springframework.context.annotation.Scope;

   @Named
   @ManagedBean
   @Scope("session")
   public class TestRadio {

   private String strStatus;


   public String getStrStatus() {
     return strStatus;
   }

   public void setStrStatus(String strStatus) {
     this.strStatus = strStatus;
  }

   public void onUpdate(){
      String statUpdate = getStrStatus();
         System.out.println("Update Status: " +statUpdate);
      }
}

I am using Tomcat 8, Maven, and Spring and these are the configuration files:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0       http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>RadioTest</groupId>
  <artifactId>RadioTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>
  <build>
<sourceDirectory>src</sourceDirectory>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
      <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
  </plugin>
</plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>4.0</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>all-themes</artifactId>
        <version>1.0.10</version>
    </dependency>
  </dependencies>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 <display-name>RadioTest</display-name>
 <welcome-file-list>
 <welcome-file>index.xhtml</welcome-file>
 <welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
  <servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>/faces/*</url-pattern>
   <url-pattern>*.xhtml</url-pattern>
 </servlet-mapping>
 <context-param>
   <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
  </context-param>
 <context-param>
   <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>overcast</param-value>
</context-param>
  <listener>
<listener-class>
    org.springframework.web.context.ContextLoaderListener
</listener-class>
 </listener>
 <listener>
<listener-class>
    org.springframework.web.context.request.RequestContextListener
</listener-class>
 </listener>
 <listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
    <el-   resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.projectmanage" />

</beans>
1
@Named, @ManagedBean, @Scope("session")? @ManagedBean is a semi-deprecated JSF artifact. Things will only get confused with its usage, when beans are managed by another bean management framework like Spring or CDI as they do not transparently interact with each other. - Tiny
Do you get any error? Do general test-cases (simplest ones) work as expected which may then verify that CDI is properly installed on Tomcat? - Tiny
And as mentioned in tbe PrimeFaces forum, a plain jsf selectOne fails to, right? forum.primefaces.org/viewtopic.php?f=3&t=44064 Hence not PrimeFaces related. - Kukeltje
To Tiny: I do not see any error messages. I have run quite a few projects in practically the same configuration using various primefaces features such as Menubar, DataTable and others and all go smoothly. So far only this one item (OneRadio) gives me a problem. It is very puzzling. I dropped @ManagedBean but no improvement. - Adam
To Kukeltje: Yes, both h: and p: give the same null. - Adam

1 Answers

1
votes

Sorry for my mistake. I did not bind the radio button to a been property as pointed out by BalusC. I just omitted EL expression So, instead of:

<p:selectOneRadio id="strStatus" value="testRadio.strStatus">

the correct line is obviously:

<p:selectOneRadio id="strStatus" value="#{testRadio.strStatus}">

And it works.