h:selectOneMenu不能与jsf2一起使用

时间:2011-06-27 11:21:25

标签: jsf richfaces seam

最近我们迁移到jboss-6.0.0.Final wiith richfaces-3.3.3.Final和jsf-2.0。在部署之后,如果selectItems具有属性'noSelectionLabel',则<h:selectOneMenu/>无法正常工作,甚至将<s:convertEnum/>传递给组件。例如

        <h:selectOneMenu id="gender" value="#{user.gender}">
            <s:selectItems noSelectionLabel="Select Gender"/>
            <f:selectItems value="#{user.genderTypes}" />
            <s:convertEnum />
        </h:selectOneMenu>


public Map<String, Gender> getGenderTypes() {
    Gender[] values = Gender.values();
    Map<String, Gender> genderTypes = new LinkedHashMap<String, Gender>(values.length);

    for (Gender gender : values) {
        genderTypes.put(gender.toString(), gender);
    }

    return genderTypes;
}

如果没有为此元素提供值,则在页面提交时出现错误

  

找不到“常量'选择性别'   关于enum性别“

。 然后我分析了JSF 2在Mojarra-2.0下的初始化。我在web.xml中将此更改为Mojarra-1.2,如此

<context-param>
      <param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
      <param-value>Mojarra-1.2</param-value> </context-param>

在此之后我得到了部署错误

  

引起:   java.lang.NoClassDefFoundError:   组织/ RichFaces的/上下文/ PartialViewConextImpl

我的web.xml

<?xml version="1.0" ?>
<web-app 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-app_2_5.xsd"
    version="2.5">

    <!-- RichFaces -->

    <context-param>
        <param-name>org.richfaces.SKIN</param-name>
        <param-value>blueSky</param-value>
    </context-param>

    <!-- Suppress spurious stylesheets -->

    <context-param>
        <param-name>org.richfaces.CONTROL_SKINNING</param-name>
        <param-value>enable</param-value>
    </context-param>

    <context-param>
        <param-name>org.richfaces.CONTROL_SKINNING_CLASSES</param-name>
        <param-value>enable</param-value>
    </context-param>

    <!-- Added to force facelets to ignore comments -->

    <context-param>
        <param-name>facelets.SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>

    <!--
        Change load strategy to DEFAULT to disable sending scripts/styles as
        packs
    -->

    <context-param>
        <param-name>org.richfaces.LoadStyleStrategy</param-name>
        <param-value>ALL</param-value>
    </context-param>

    <context-param>
        <param-name>org.richfaces.LoadScriptStrategy</param-name>
        <param-value>ALL</param-value>
    </context-param>

    <!-- Seam -->

    <listener>
        <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <url-pattern>/seam/resource/*</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>Seam Filter</filter-name>
        <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Seam Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Facelets development mode (disable in production) -->

    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>false</param-value>
    </context-param>

    <!-- JSF -->

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>

    <!-- Session Timeout -->

    <context-param>
        <param-name>org.ajax4jsf.handleViewExpiredOnClient</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>org.ajax4jsf.DEFAULT_EXPIRE</param-name>
        <param-value>86400</param-value>
    </context-param>

    <!-- This param is needed to change the JSF configuration of our WAR -->
    <context-param>
          <param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
          <param-value>Mojarra-1.2</param-value>
     </context-param>

    <!-- Faces servlet -->

    <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>*.seam</url-pattern>
    </servlet-mapping>

    <security-constraint>
        <display-name>Restrict raw XHTML Documents</display-name>
        <web-resource-collection>
            <web-resource-name>XHTML</web-resource-name>
            <url-pattern>*.xhtml</url-pattern>
        </web-resource-collection>
        <auth-constraint />
    </security-constraint>

    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

</web-app>

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

从JSF 1.2开始,有一个builtin enum converter。您不需要自定义转换器。要表示没有值的默认选择,只需使用<f:selectItem>而不使用itemValue。此外,由于JSF 2.0,您不再需要SelectItem[]List<SelectItem>Map<K, V> <f:selectItems>。只支持T[]List<T>。甚至还有一个var属性,因此可以使用T的属性设置项标签和值。

所以,这个观点:

<h:selectOneMenu value="#{bean.gender}" required="true">
    <f:selectItem itemLabel="Select gender" />
    <f:selectItems value="#{bean.genders}" />
</h:selectOneMenu>

使用此模型:

private Gender gender; // +getter +setter

public Gender[] getGenders() {
    return Gender.values();
}

应该没有太多麻烦工作。

答案 1 :(得分:0)

在这种情况略有不同的情况下我也注意到了这一点。如果您明确地将转换器分配给inputelement并将null作为值(noSelecrtion),则会出于某种奇怪的原因将标签而不是值发送到转换器。我通过使用@FacesConverter的forClass属性或使用JSF默认转换器解决了这个问题(enum对我来说没问题)。