值不绑定到来自h:selectManyCheckbox的String数组

时间:2013-06-24 13:05:46

标签: jsf jsf-1.2

我正在尝试添加一个简单的selectManycheckbox示例,但这些值没有绑定到bean。我正在

INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=form1:subscriptions[severity=(ERROR 2), summary=(Conversion Error setting value 'asis base appl' for '#{HomePage.outputType}'.), detail=(Conversion Error setting value 'asis base appl' for '#{HomePage.outputType}'.)]

这是我的jsf页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="css/style.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Downloader</title>
</head>
<body>
<f:view>

    <h:form id="form1">

        <h:panelGrid border="1" columns="2"
            style='color: black; font-size: 16px; font-family: "Times New Roman", Serif; font-weight: bold'>
            <h:outputLabel value="Select values"></h:outputLabel>
            <h:selectManyCheckbox id="subscriptions" value="#{HomePage.outputType}" layout="pageDirection">
                <f:selectItem id="item1" itemLabel="ASIS" itemValue="asis"/>
                <f:selectItem id="item3" itemLabel="BASE" itemValue="base"/>
                <f:selectItem id="item7" itemLabel="APPLIED" itemValue="appl"/>
            </h:selectManyCheckbox>

            </h:panelGrid>
            <h:commandButton type="submit" action="#{HomePage.genOutput}" value="Submit"></h:commandButton>
    </h:form>
</f:view>
</body>
</html>

这是我的bean类HomePage.java:

package com.MH;
import com.MH.WebPageSaver;
public class HomePage{
    private String[] outputType;

    /*various getter setters*/


    public String genOutput() throws Exception{
     if (outputType != null)
      {
        System.out.println("if");
            WebPageSaver w = new WebPageSaver();
            try{
                System.out.println("inside try");
                            /*independent function below works fine*/
                String msg = w.generateOutput(outputType);
                return msg;
            }
            catch (Exception e){
                System.out.println(e);
                return "error";
            }
        }
        else 
        {
            System.out.println(outputType);
            System.out.println("else");
            return "error";
        }
    }


}

无论msg如何,导航规则都在同一页面上。

    <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_1_2.xsd"
    version="1.2">
    <managed-bean>
        <managed-bean-name>HomePage</managed-bean-name>
        <managed-bean-class>com.MH.HomePage</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    <navigation-rule>
        <from-view-id>/HomePage.jsp</from-view-id>
        <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/HomePage.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/HomePage.jsp</from-view-id>
        <navigation-case>
        <from-outcome>error</from-outcome>
        <to-view-id>/HomePage.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

请让我知道我在这里失踪了什么?

1 个答案:

答案 0 :(得分:0)

据我所知,我的代码没有出现转换错误。我唯一能想到的是你可能错误地定义了setOutputType方法。它应该是

public void setOutputType(String[] outputType) {
        this.outputType = outputType;
}

因为h:selectManyCheckBox需要在提交表单时设置String数组。

我想在这里彻底,所以这是我的HomePage.java

package com.MH;
public class HomePage implements java.io.Serializable {
    private String[] outputType;

    /*various getter setters*/
    public String[] getOutputType() {
        return outputType;
    }

    public void setOutputType(String[] outputType) {
        this.outputType = outputType;
    }

    public String genOutput() throws Exception{
     if (outputType != null)
      {
        System.out.println("if");
            WebPageSaver w = new WebPageSaver();
            try{
                System.out.println("inside try");
                            /*independent function below works fine*/
                String msg = w.generateOutput(outputType);
                return msg;
            }
            catch (Exception e){
                System.out.println(e);
                return "error";
            }
        }
        else 
        {
            System.out.println(outputType);
            System.out.println("else");
            return "error";
        }
    }
}

与你的没什么不同。来自我generateOutput(String[])的{​​{1}}只是返回WebPageSaver成功。我甚至做了一个简单的test.jsp来查看是否正在设置值。 (注意:它很草率)

String

我编辑了你的一个导航案例

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="css/style.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Downloader</title>
</head>
<body>
<f:view>
    <h:outputText value="#{HomePage.outputType[0]}"/>
    <h:outputText value="#{HomePage.outputType[1]}"/>
    <h:outputText value="#{HomePage.outputType[2]}"/>
</f:view>
</body>
</html>

<强>评论

不确定为什么<from-outcome>success</from-outcome> <to-view-id>/test.jsp</to-view-id> </navigation-case> 中有throws Exception,因为您已经在mehod中捕获了genOuput。不需要它(除非WebPageSaver的构造函数抛出某些东西)

此外,会话范围应该实现java.io.Serializable。不是必需的,但是一个好的做法

Why is it considered a good practice to make session scoped objects Serializable?