JSF java.lang.IllegalArgumentException:无法将 5 类型的 class java.lang.Integer 转换为 class

问题描述 投票:0回答:2

我收到转换器的新错误

javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert 5 of type class java.lang.Integer to class com.jpa.entity.Groups
    at javax.faces.component.UIInput.updateModel(UIInput.java:839)
    at javax.faces.component.UIInput.processUpdates(UIInput.java:722)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1108)
    at javax.faces.component.UIForm.processUpdates(UIForm.java:270)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1108)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1108)
    at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1239)
    at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException: Cannot convert 5 of type class java.lang.Integer to class com.jpa.entity.Groups
    at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:397)
    at com.sun.el.parser.AstValue.setValue(AstValue.java:194)
    at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:286)
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:124)
    at javax.faces.component.UIInput.updateModel(UIInput.java:805)

5 是 GroupsID 的值

我在 http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html 阅读了教程http://www.javabeat.net/articles/15-introduction-to-jsf-core-tags-library-2.html 但我该如何转换它?因为 equals 和 hashcode 方法我在 Groups 类中被重写了

小组班

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jpa.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author KencyWindy
 */
@Entity
@Table(name = "groups")
@NamedQueries({
    @NamedQuery(name = "Groups.findAll", query = "SELECT g FROM Groups g"),
    @NamedQuery(name = "Groups.findByGroupid", query = "SELECT g FROM Groups g WHERE g.groupid = :groupid"),
    @NamedQuery(name = "Groups.findByGroupname", query = "SELECT g FROM Groups g WHERE g.groupname = :groupname")})
public class Groups implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "groupid")
    private Integer groupid;
    @Basic(optional = false)
    @Column(name = "groupname")
    private String groupname;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "groups")
    private List<TblUser> tblUserList;

    public Groups() {
    }

    public Groups(Integer groupid) {
        this.groupid = groupid;
    }

    public Groups(Integer groupid, String groupname) {
        this.groupid = groupid;
        this.groupname = groupname;
    }

    public Integer getGroupid() {
        return groupid;
    }

    public void setGroupid(Integer groupid) {
        this.groupid = groupid;
    }

    public String getGroupname() {
        return groupname;
    }

    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public List<TblUser> getTblUserList() {
        return tblUserList;
    }

    public void setTblUserList(List<TblUser> tblUserList) {
        this.tblUserList = tblUserList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (groupid != null ? groupid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Groups)) {
            return false;
        }
        Groups other = (Groups) object;
        if ((this.groupid == null && other.groupid != null) || (this.groupid != null && !this.groupid.equals(other.groupid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.jpa.entity.Groups[groupid=" + groupid + "]";
    }

}

Tbl用户类

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jpa.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author KencyWindy
 */
@Entity
@Table(name = "tbl_user")
@NamedQueries({
    @NamedQuery(name = "TblUser.findAll", query = "SELECT t FROM TblUser t"),
    @NamedQuery(name = "TblUser.findByUserid", query = "SELECT t FROM TblUser t WHERE t.userid = :userid"),
    @NamedQuery(name = "TblUser.findByUsername", query = "SELECT t FROM TblUser t WHERE t.username = :username"),
    @NamedQuery(name = "TblUser.findByPassword", query = "SELECT t FROM TblUser t WHERE t.password = :password"),
    @NamedQuery(name = "TblUser.findByDob", query = "SELECT t FROM TblUser t WHERE t.dob = :dob"),
    @NamedQuery(name = "TblUser.findByAddress", query = "SELECT t FROM TblUser t WHERE t.address = :address")})
public class TblUser implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "userid")
    private Integer userid;
    @Basic(optional = false)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    @Basic(optional = false)
    @Column(name = "dob")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dob;
    @Basic(optional = false)
    @Column(name = "address")
    private String address;
    @JoinColumn(name = "igroup", referencedColumnName = "groupid")
    @ManyToOne(optional = false)
    private Groups groups;

    public TblUser() {
    }

    public TblUser(Integer userid) {
        this.userid = userid;
    }

    public TblUser(Integer userid, String username, String password, Date dob, String address) {
        this.userid = userid;
        this.username = username;
        this.password = password;
        this.dob = dob;
        this.address = address;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Groups getGroups() {
        return groups;
    }

    public void setGroups(Groups groups) {
        this.groups = groups;
    }

    public Integer getGroupID(){
        return this.groups.getGroupid();
    }

    public void setGroupID(Integer iGroup){
        this.groups = new Groups(iGroup);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userid != null ? userid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TblUser)) {
            return false;
        }
        TblUser other = (TblUser) object;
        if ((this.userid == null && other.userid != null) || (this.userid != null && !this.userid.equals(other.userid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.jpa.entity.TblUser[userid=" + userid + "]";
    }

}

方法组列表

>  public javax.faces.model.SelectItem[]
> getGroupList(){
>         SelectItem[] options = null;
> 
>             List<Groups> iGroup = userBean.retrieveAllGroup();
>             if(iGroup != null && iGroup.size() > 0){
>                 options = new SelectItem[iGroup.size()];
>                 int i = 0 ;
>                 for(Groups groups : iGroup){
>                     options[i++] = new SelectItem(groups.getGroupid(),
> groups.getGroupname());
>                 }
>             }
> 
>         return options;
>     }

JSF 文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>TODO supply a title</title>

    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h1><h:outputText value="Create/Edit"/></h1>
                <h:panelGrid columns="2">
                     <h:outputLabel value="Username" for="username" />
                    <h:inputText id="username" value="#{userController.user.username}" title="Username" required="true" requiredMessage="The Username field is required."/>
                    <h:outputLabel value="Password:" for="password" />
                    <h:inputText id="password" value="#{userController.user.password}" title="Password" required="true" requiredMessage="The Password field is required."/>
                    <h:outputLabel value="Dob:" for="dob" />
                    <h:inputText id="dob" value="#{userController.user.dob}" title="Dob" required="true" requiredMessage="The Dob field is required.">
                        <f:convertDateTime pattern="MM/dd/yyyy" />
                    </h:inputText>
                    <h:outputLabel value="Address:" for="address" />
                    <h:inputText id="address" value="#{userController.user.address}" title="Address" required="true" requiredMessage="The Address field is required."/>
                    <h:inputHidden/>
                    <h:selectOneMenu id="igroup" value="#{userController.user.groups}" title="Igroup" required="true" requiredMessage="The Igroup field is required.">
                        <!-- TODO: update below reference to list of available items-->
                        <f:selectItems value="#{userController.groupList}"/>
                        <f:converter converterId="javax.faces.Integer"/>
                    </h:selectOneMenu>
                </h:panelGrid>
                <h:commandButton value="Ok" action="#{userController.addUser}"/>
            </h:form>
        </f:view>


    </h:body>
</html>
jsf
2个回答
6
投票

我不知道你的 JSF 代码是什么样的,但我怀疑你正在使用

<h:selectOneMenu value="#{bean.group}">

而不是

<h:selectOneMenu value="#{bean.groupId}">

您的

SelectItem[]
包含
Integer groupId
作为项目值。
value
h:selectOneMenu
属性必须与此类型匹配。异常消息正是说明了这一点。该值作为
Integer
提交,但 Bean 需要
Group


0
投票

javax.servlet.ServletException:java.lang.IllegalArgumentException:无法将类型类 java.util.Date 的 04/12/16 01:00 转换为类 java.sql.Date javax.faces.webapp.FacesServlet.service(FacesServlet.java:671) io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)

© www.soinside.com 2019 - 2024. All rights reserved.