java - JSF 2 View 验证上的 JBoss AS 7.1.1.Final : serverError: class java. lang.IllegalStateException

标签 java validation jsf jsf-2 jboss7.x

我在使用纯 JSF 2 页面时遇到问题。加载页面时,会显示两个输入文本和一个选择。每个右侧都有一个验证字段。这两个输入是必填字段,而选择始终有一个选择,因此它根本不验证(至少不应该验证)。

这是屏幕截图:

enter image description here

这是单击“创建”按钮时显示的内容。重新点击“创建”按钮时,会出现以下弹出窗口:

enter image description here

好的,没有代码就什么都没有,这是 XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
  <h:head>
  </h:head>
  <h:body>
    <h:form>
      <h:panelGrid columns="3" id="base-data-grid">
        <h:outputLabel value="Name:*" for="name-input" />
        <h:inputText value="#{testBean.name}"
                     requiredMessage="Name required!"
                     id="name-input">
          <f:validateRequired />
        </h:inputText>
        <h:message for="name-input" style="color: red;" />
        <h:outputLabel value="Code:*" for="code-input" />
        <h:inputText value="#{testBean.code}"
                     requiredMessage="Code required!"
                     id="code-input">
          <f:validateRequired />
        </h:inputText>
        <h:message for="code-input" style="color: red;" />
        <h:outputLabel value="Location:" for="location-select" />
        <h:selectOneMenu value="#{testBean.location}"
                         converter="#{testConverter}"
                         id="location-select">
          <f:selectItems value="#{testBean.locations}"
                         var="l"
                         itemValue="#{l}"
                         itemLabel="#{l.name}" />
        </h:selectOneMenu>
        <h:message for="location-select" style="color: red;" />
      </h:panelGrid>
      <h:panelGrid columns="1">
        <h:commandButton value="Create"
                           action="#{testBean.create}">
          <f:ajax execute="base-data-grid" render="base-data-grid lalala" />
        </h:commandButton>
      </h:panelGrid>
      <h:messages id="lalala" />
    </h:form>
  </h:body>

</html>

选择的位置类别(只是名称):

public class Location
{
    private String name;
    public Location(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
}

转换器(作为 CDI 版本):

import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@RequestScoped
public class TestConverter implements Converter
{
    private static final Logger log = LoggerFactory.getLogger(TestConverter.class);

    @Inject
    private TestBean testBean;

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String name)
    {
        log.info(getClass().getSimpleName() + ".getAsObject: " + name);

        return testBean.getLocationFor(name);
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
    {
        log.info(getClass().getSimpleName() + ".getAsString: " + obj);

        return ((Location)obj).getName();
    }
}

和测试 bean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class TestBean implements Serializable
{
    private static final Logger log = LoggerFactory.getLogger(TestBean.class);

    private String name;
    private String code;

    private Location location;
    private List<Location> locations;

    @PostConstruct
    public void init()
    {
        locations = new ArrayList<Location>();

        locations.add(new Location("Berlin"));
        locations.add(new Location("London"));
        locations.add(new Location("New York"));
        locations.add(new Location("Moscow"));
        locations.add(new Location("Bejing"));

        location = locations.get(2);
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public Location getLocation()
    {
        return location;
    }

    public void setLocation(Location location)
    {
        this.location = location;
    }

    public List<Location> getLocations()
    {
        return locations;
    }

    public void setLocations(List<Location> locations)
    {
        this.locations = locations;
    }

    public void create()
    {
        log.info("Creating new whatever...");
    }

    // for converter to mimic DB query
    public Location getLocationFor(String name)
    {
        for ( Location location : locations )
        {
            if ( location.getName().equals(name) )
            {
                return location;
            }
        }

        return null;       
    }

}

正如您所看到的,该页面没有太多应该做的事情。验证两个输入字段,显示选择并调用 testBean.create(如果全部验证正常)。

但是,一旦验证运行一次,随后每次单击“创建”按钮都会导致该服务器错误。

注意,如果您完全删除 f:selectItems 或 h:selectOneMenu,页面将按预期工作。这就是整件事实际上如此奇怪的原因......

我不知道这里发生了什么。 有人知道出了什么问题吗?

我在这里附加了一个 JBoss AS 7 测试应用程序:https://community.jboss.org/thread/202501 (重复帖子,抱歉需要很多帮助)。

请看一下这个极其奇怪的 IllegalStateException。 JBAS server.log 中甚至没有任何内容...

PS:Mojarra版本当然是2.1.7(JBoss AS 7.1.1.Final自带的)

最佳答案

您的问题是 Location 需要可序列化
(实现java.io.Serialized接口(interface))

f:validateRequired 标记导致 ajax 执行必需的验证。

在此 ajax 请求的 RestoreView 阶段,由于位置不可序列化,运行时遇到 InstantiationException。

老实说,我不知道为什么没有记录包括堆栈跟踪在内的错误。

关于java - JSF 2 View 验证上的 JBoss AS 7.1.1.Final : serverError: class java. lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11444410/

相关文章:

javascript - 使用 DHTML 事件 onmouseover 更改 JSF 命令按钮的图像

jsf - 监听来自 Autocomplete (Primefaces) 组件的 onSelect 事件

java - 将具有 json-format-string 的对象序列化为 json

php - Laravel 基于多列数据值的唯一性验证

c# - 在 C# 中使用堆栈和队列的括号检查器

JavaScript 验证 - 页面仍在处理中

java - 如何创建选项卡 - Jquery 或 JSF

java - 使用 Toast 处理 Fragment 类之外的异常

java - 创建 <Fish> arrayList 深拷贝

java - 使用压缩的缓冲图像转换