2014-09-12 70 views
0

我有一个jsf页面,其中包含一个selectmanylistbox,其值是由我管理的函数提供的对象数组。这些目的是根据下面的简单类:jsf提交表单无效的值

public class Category { 
private String categoryId; 
private String categoryName; 
private String[] templates; 

public Category(String categoryId, String categoryName) { 
    this.categoryId = categoryId; 
    this.categoryName = categoryName; 
} 

public Category(String categoryId, String categoryName, String[] templates) { 
    this.categoryId = categoryId; 
    this.categoryName = categoryName; 
    this.templates = templates; 
} 

public String getCategoryId() { 
    return categoryId; 
} 

public void setCategoryId(String categoryId) { 
    this.categoryId = categoryId; 
} 

public String getCategoryName() { 
    return categoryName; 
} 

public void setCategoryName(String categoryName) { 
    this.categoryName = categoryName; 
} 

public String[] getTemplates() { 
    return templates; 
} 

public void setTemplates(String[] templates) { 
    this.templates = templates; 
} 

这里是托管bean函数提供所述阵列,用于selectonelistbox:

public Category[] getCategoryValues() { 

    categoryValues = new Category[4]; 

    String[] temp = new String[3]; 
    temp[0] = "Line 1"; 
    temp[1] = "Line 2"; 
    temp[2] = "Line 3"; 
    categoryValues[0] = new Category("1001", "Category 1", temp); 
    temp = new String[3]; 
    temp[0] = "Line 4"; 
    temp[1] = "Line 5"; 
    temp[2] = "Line 6"; 
    categoryValues[1] = new Category("1002", "Category 2", temp); 

    temp = new String[3]; 
    temp[0] = "Line 7"; 
    temp[1] = "Line 8"; 
    temp[2] = "Line 9"; 

    categoryValues[2] = new Category("1003", "Category 3", temp); 

    temp = new String[3]; 
    temp[0] = "Line 10"; 
    temp[1] = "Line 11"; 
    temp[2] = "Line 12"; 

    categoryValues[3] = new Category("1004", "Category 4", temp); 

    return categoryValues; 

} 

下面是该selectmanylistbox JSF的代码:

<h:selectOneListbox value="#{category.selectedCategoryId}" 
    onchange="submit()"> 

     <f:selectItems value="#{category.categoryValues}" var="cat" 
      itemLabel="#{cat.categoryName}" itemValue="#{cat.categoryId}" /> 
    </h:selectOneListbox> 

此外,我使用以下事件来重新加载页面,只要选择一个类别:

<f:event listener="#{category.intialize()}" type="preRenderView" /> 

重新载入页面的目的是使用selectedcategory作为selectmanycheckbox项目的基础,显示selectedcategory中的数组作为复选框。这里是相应的jsf代码:

<h:selectManyCheckbox value="#{category.targetTemplates}"> 

     <f:selectItems value="#{category.selectedCategoryTemplates}" /> 
    </h:selectManyCheckbox> 

其中CategoryTemplates是simplay一个字符串对象的数组。

现在,类别选择和页面重新载入工作正常,并且selectmanycheckbox项目正确显示,并且所选类别中的字符串作为选项正确显示。当我尝试选择一些复选框值并提交它们时,我收到了一个inavlid值错误消息,并且没有提交完成。 selectmanycheckbox使用的值在页面加载时可用,那么可能是什么问题?

下面是实际无为initialize()方法,我只是用它来重新加载页面的目的:

public void intialize() { 

} 

下面是复选框的HTML代码:

<table> 
<tr> 
<td> 
<input name="j_idt4:j_idt8" id="j_idt4:j_idt8:0" value="Line 1" type="checkbox" /><label 
for="j_idt4:j_idt8:0" class=""> Line 1</label></td> 
<td> 
<input name="j_idt4:j_idt8" id="j_idt4:j_idt8:1" value="Line 2" type="checkbox" /><label 
for="j_idt4:j_idt8:1" class=""> Line 2</label></td> 
<td> 
<input name="j_idt4:j_idt8" id="j_idt4:j_idt8:2" value="Line 3" type="checkbox" /><label 
for="j_idt4:j_idt8:2" class=""> Line 3</label></td> 
</tr> 
</table> 

在控制台上实际的错误信息是:

j_idt4:j_idt8:Überprüfungsfehler:WERT ISTungültig),细节=(j_idt4:j_idt8:Überprüfungsfehler:WERT ISTù ngültig。)]

及其在德国和意味着验证失败,值无效

+0

请显示错误消息和category.intialize()方法。看起来preRenderView在最终的POST后被再次调用,从而改变了值。根据用户选择更新部分输出的要求更好地使用ajax处理... – Yamada 2014-09-12 15:35:07

+0

我已编辑帖子,添加了导致错误的HTML代码以及错误消息(其德文版本)。我也考虑过Ajax,但由于项目需求而无法使用 – user1107888 2014-09-12 15:43:55

+0

您对initialize()函数是正确的,它在提交时也会被调用。 – user1107888 2014-09-12 15:47:38

回答

0

检查托管bean其提供selectonelistbox(方法getCategoryValues)阵列的范围。它必须像在初始请求期间(在提交表单之前,在选择值期间)一样,在后续请求(在提交表单期间)返回相同的列表。