ViewParam + Ajax通话问题

问题描述

我仍然存在与ViewParam相关的JSF 2.3的问题,我使用WildFly 21(于2020年10月发布)对其进行了测试,并且该错误仍然存​​在。 该代码在以下存储库中: 码头示例:https://github.com/erickdeoliveiraleal/primefaces-test/tree/viewparam Wildfly示例https://gitlab.com/erickdeoliveiraleal/simpleproject

请注意,Wildfly已经在Mojarra 2.3存储库中合并了最近完成的所有修补程序。

我在官方回购中发布了一个问题,但未能成功解决该问题:https://github.com/eclipse-ee4j/mojarra/issues/4734

第二次单击按钮时,发生异常,使用mojarra 2.2,myfaces 2.2或myfaces 2.3并非如此

XHTML:

chalice connect-to-existing-project --name XXX

Bean

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">

<f:Metadata>
    <f:viewParam id="id" name="id" value="#{testView.testClass.id}" />
</f:Metadata>

<h:form>

    <h:commandButton>
        <f:ajax execute="@form"  />
    </h:commandButton>

    <h:selectOneMenu value="#{testView.testClass}">
        <f:selectItem itemValue="null" itemLabel="Sel. classe" />
        <f:selectItems value="#{testView.testClasses}" var="cl"
            itemValue="#{cl}" itemLabel="#{cl.id}" />
    </h:selectOneMenu>

</h:form>

</html>

实体类

package org.primefaces.test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.postconstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class TestView implements Serializable {

    private TestClass testClass;
    private List<TestClass> testClasses;

    @postconstruct
    public void init() {
        testClass = new TestClass();
        testClasses = new ArrayList<>();
        testClasses.add(new TestClass(1));
    }

    public TestClass getTestClass() {
        return testClass;
    }

    public void setTestClass(TestClass testClass) {
        this.testClass = testClass;
    }

    public List<TestClass> getTestClasses() {
        return testClasses;
    }

    public void setTestClasses(List<TestClass> testClasses) {
        this.testClasses = testClasses;
    }

}

转换器

package org.primefaces.test;

public class TestClass {
    Integer id;

    public TestClass() {
    }

    public TestClass(Integer a) {
        id = a;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        TestClass other = (TestClass) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Classe [id=" + id + "]";
    }
}

您还需要将INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL设置为true

package org.primefaces.test;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

//@FacesConverter(value = "TestClassConverter")    
@FacesConverter(forClass = TestClass.class)
public class TestClassConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext facesContext,UIComponent uiComponent,String value) {
        if (value != null && !value.isEmpty()) {
            return (TestClass) uiComponent.getAttributes().get(value);
        }
        return null;
    }

    @Override
    public String getAsstring(FacesContext facesContext,Object value) {
        if (value instanceof TestClass) {
            TestClass entity= (TestClass) value;
            if (entity != null && entity instanceof TestClass && entity.getId() != null) {
                uiComponent.getAttributes().put( entity.getId().toString(),entity);
                return entity.getId().toString();
            }
        }
        return "";
    }
}

例外

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
  </context-param>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)