p:selectOneRadio 和 h:selectOneRadio 产生空值

p:selectOneRadio and h:selectOneRadio produces null values

我几天前在 primefaces 论坛上发布了这个问题,但到目前为止我还没有找到解决方案。 我一直在尝试使用 OneRadio,但我一直只从 itemValue(s) 获得空值。这是一个大型项目的一部分,所以我只提取了相关代码并制作了一个简单的动态 Web 项目 (Eclipse Mars.1),其中包含一个视图和一个用于测试案例的简单 bean。无论使用 ajax 还是 commandButton,都会出现空值而不是 itemValue。非常感谢您帮助解决这个令人费解的现象:

这是观点:

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

这是支持 bean。一个建议是 不要 在同一个 class 中同时使用 @Named 和 @ManagedBean,我尝试了这个更正,但没有任何改进。

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);
      }
}

我正在使用 Tomcat 8、Maven 和 Spring,这些是配置文件:

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>

面孔-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>

对不起我的错误。正如 BalusC 所指出的,我没有将单选按钮绑定到 been 属性。我只是省略了 EL 表达式 所以,而不是:

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

正确的行显然是:

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

而且有效。