2013-06-24 46 views
0

我想添加做一個簡單的selectManycheckbox示例,但值不綁定到該bean。我越來越值不綁定到字符串數組從h:selectManyCheckbox

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


} 

的導航規則是相同的頁面,而不管味精。

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

請讓我知道我在這裏失蹤了什麼?

回答

0

據我所知,我沒有得到您的轉換錯誤與我的代碼。我能想到的唯一的事情就是你可能定義了你的方法不對。它應該是

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[])WebPageSaver我的目的是簡單地返回String成功。我甚至做了一個簡單的test.jsp來查看是否設置了值並且它們是。 (注意:這是馬虎)

<%@ 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 ExceptiongenOuput,因爲你已經抓住它在mehod內。這是不需要的(除非你的構造函數爲WebPageSaver拋出一些東西)

此外,會話作用域應執行java.io.Serializable。不是必需的,但一個很好的做法

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

+0

OP使用JSF 1.2。你的答案假定JSF 2.0。重試JSF 1.2並發現地獄。 – BalusC

+0

@BalusC哈哈,一切都好。 – Andy

+0

ok Andy,讓我們假設代碼工作,我試圖顯示在jsp頁面中勾選的任何複選框。萊斯特所說的是正確的。但全文不在jsf1.2中。還有什麼可以解決的?我希望不會讓你困惑。 – satsamsk

相關問題