【发布时间】:2015-12-20 19:12:03
【问题描述】:
我几天前在 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。一个建议是不要在同一个类中同时使用@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>
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>
【问题讨论】:
-
@Named,@ManagedBean,@Scope("session")?@ManagedBean是一个半弃用的 JSF 工件。当 bean 由另一个 bean 管理框架(如 Spring 或 CDI)管理时,事情只会与它的用法混淆,因为它们之间没有透明的交互。 -
你有什么错误吗?一般测试用例(最简单的)是否按预期工作,然后可以验证 CDI 是否正确安装在 Tomcat 上?
-
正如 PrimeFaces 论坛中提到的那样,普通的 jsf selectOne 失败了,对吧? forum.primefaces.org/viewtopic.php?f=3&t=44064 因此与 PrimeFaces 无关。
-
To Tiny:我没有看到任何错误消息。我已经使用各种 Primefaces 功能(如 Menubar、DataTable 等)在几乎相同的配置中运行了很多项目,并且一切顺利。到目前为止,只有这一项(OneRadio)给了我一个问题。这是非常令人费解的。我放弃了@ManagedBean,但没有任何改善。
-
致 Kukeltje:是的,h: 和 p: 都给出相同的 null。
标签: jsf primefaces selectoneradio