2

我知道这已经被讨论了很多,我也尝试了大部分解决方案,但我仍然得到这个错误:

    sourceId=comboNewTaskParent[severity=(ERROR 2), summary=(comboNewTaskParent: Validation Error: Value is not valid), detail=(comboNewTaskParent: Validation Error: Value is not valid)]

这是 HTML 的代码:

    <h:outputLabel value="Parent task" for="comboNewTaskParent" />
                            <div class="formRight">
                                <h:selectOneMenu id="comboNewTaskParent" value="#{taskController.parentTask}" converter="#{taskConverter}"
                                    <f:selectItems value="#{comboTaskByProject}" var="task" itemValue="#{task}" itemLabel="#{task.taskName}" />
                                </h:selectOneMenu>
                            </div>

这是我的实体 bean 的代码:

    package com.projectportal.entity;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.List;


/**
 * The persistent class for the Task database table.
 * 
 */
@Entity
@Table(name="Task")
public class Task implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(unique=true, nullable=false, length=36)
private String taskId;

@Column(length=1000)
private String taskDesc;

@Column(nullable=false)
private int taskDurationHour;

@Temporal(TemporalType.TIMESTAMP)
@Column(nullable=false)
private Date taskEstimated;

@Column(nullable=false, length=200)
private String taskName;

@Column(nullable=false)
private float taskPercentComplete;

@Temporal(TemporalType.TIMESTAMP)
@Column(nullable=false)
private Date taskStartDate;

//bi-directional many-to-one association to Priority
@ManyToOne
@JoinColumn(name="priorityId", nullable=false)
private Priority priority;

//bi-directional many-to-one association to Project
@ManyToOne
@JoinColumn(name="projectId")
private Project project;

//bi-directional many-to-one association to Status
@ManyToOne
@JoinColumn(name="statusId", nullable=false)
private Status status;

//bi-directional many-to-one association to Task
@ManyToOne
@JoinColumn(name="parentTaskId")
private Task parentTask;

//bi-directional many-to-one association to Task
@OneToMany(mappedBy="parentTask")
private List<Task> childTasks;

//bi-directional many-to-one association to Task
@ManyToOne
@JoinColumn(name="preTaskId")
private Task preTask;

//bi-directional many-to-one association to Task
@OneToMany(mappedBy="preTask")
private List<Task> dependentTasks;

//bi-directional many-to-one association to UserXTask
@OneToMany(mappedBy="task")
private List<UserXTask> userXtasks;

public Task() {
}

public String getTaskId() {
    return this.taskId;
}

public void setTaskId(String taskId) {
    this.taskId = taskId;
}

public String getTaskDesc() {
    return this.taskDesc;
}

public void setTaskDesc(String taskDesc) {
    this.taskDesc = taskDesc;
}

public int getTaskDurationHour() {
    return this.taskDurationHour;
}

public void setTaskDurationHour(int taskDurationHour) {
    this.taskDurationHour = taskDurationHour;
}

public Date getTaskEstimated() {
    return this.taskEstimated;
}

public void setTaskEstimated(Date taskEstimated) {
    this.taskEstimated = taskEstimated;
}

public String getTaskName() {
    return this.taskName;
}

public void setTaskName(String taskName) {
    this.taskName = taskName;
}

public float getTaskPercentComplete() {
    return this.taskPercentComplete;
}

public void setTaskPercentComplete(float taskPercentComplete) {
    this.taskPercentComplete = taskPercentComplete;
}

public Date getTaskStartDate() {
    return this.taskStartDate;
}

public void setTaskStartDate(Date taskStartDate) {
    this.taskStartDate = taskStartDate;
}

public Priority getPriority() {
    return this.priority;
}

public void setPriority(Priority priority) {
    this.priority = priority;
}

public Project getProject() {
    return this.project;
}

public void setProject(Project project) {
    this.project = project;
}

public Status getStatus() {
    return this.status;
}

public void setStatus(Status status) {
    this.status = status;
}

public Task getParentTask() {
    return this.parentTask;
}

public void setParentTask(Task parentTask) {
    this.parentTask = parentTask;
}

public List<Task> getChildTasks() {
    return this.childTasks;
}

public void setChildTasks(List<Task> childTasks) {
    this.childTasks = childTasks;
}

public Task getPreTask() {
    return this.preTask;
}

public void setPreTask(Task preTask) {
    this.preTask = preTask;
}

public List<Task> getDependentTasks() {
    return this.dependentTasks;
}

public void setDependentTasks(List<Task> dependentTasks) {
    this.dependentTasks = dependentTasks;
}

public List<UserXTask> getUserXtasks() {
    return this.userXtasks;
}

public void setUserXtasks(List<UserXTask> userXtasks) {
    this.userXtasks = userXtasks;
}

}

控制器:

    public @Model class TaskController {

@Inject private EntityManager em;
@Inject Identity identity;
@Inject Logger log;
@Inject Event<Task> taskEventSrc;

@Named
@Produces
private List<Task> requestTaskList;

private Task parentTask;

private Task newTask;

@Produces
@Named
public Task getNewTask(){
    return this.newTask;
}
/**
 * 
 */
public TaskController() {
    // TODO Auto-generated constructor stub
}

@PostConstruct
public void loadSelfTasks(){

    // Init
    newTask = new Task();

    // Get user from DB.
    User user = em.find(User.class, identity.getUser().getId());
    requestTaskList = new ArrayList<Task>();

    // Loop user's tasks.
    for(UserXTask userTask : user.getUserXtasks()){
        requestTaskList.add(userTask.getTask());
    }

    log.info("Tasks for user: " + user.getFirstname() + " loaded.");
}

/**
 * Create task.
 * @throws Exception
 */
public void createTask() throws Exception{
    log.info("Persistencing task: " + newTask.getParentTask().getTaskId());
    em.persist(newTask);
    taskEventSrc.fire(newTask);

    newTask = new Task();
}
/**
 * @return the parentTask
 */
public Task getParentTask() {
    return parentTask;
}
/**
 * @param parentTask the parentTask to set
 */
public void setParentTask(Task parentTask) {
    this.parentTask = parentTask;
}

}

当然还有转换器:

    @Named
/**
 * @author lastcow
 *
 */

公共类TaskConverter实现转换器{

@Inject EntityManager em;
@Inject Logger log;

/* (non-Javadoc)
 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
 */
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)     {
    log.info("=========== Convert to Object " + value);
    if(value.equals("0")){
        return null;
    }
    Task t =  em.find(Task.class, value);
    log.info("======== Got : " + t.getTaskName());
    return t;
}

/* (non-Javadoc)
 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
 */
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    log.info("=========== Convert to String " + value);
    return ((Task)value).getTaskId();
}

}

从记录的内容来看,转换正在正常工作,但是当我尝试提交表单时,总是抛出“验证错误:值无效”错误,我已经在这里敲了将近 2 天。

任何人请给一些建议。

顺便说一句,我尝试在 Task.java 中放置 equals 和 hashCode,也不起作用。提前致谢。

4

2 回答 2

11

验证错误:值无效

当所选项目的equals()方法没有返回true任何可用项目时,将引发此错误<f:selectItem(s)>。因此,这在技术上可能只有两个原因:

  1. equals()您的课程Task方法丢失或损坏。
  2. 与表单显示的<f:selectItems value="#{comboTaskByProject}">初始请求相比,在表单提交的回发请求期间发生了不兼容的变化。

要解决原因 #1,请确保您了解如何equals()正确实施。您可以在此处找到启动示例:实现 equals 合同的正确方法

要修复原因 #2,请确保#{comboTaskByProject}在回发期间永不卡顿。最好的办法是将它放在视图范围或更广泛的范围内,或者确保用于填充该列表的基于请求的条件通过例如使用保留在回发请求中<f:viewParam>

也可以看看:

于 2012-12-07T19:57:23.503 回答
1

我不确定JSF您使用的是哪个版本。据我所知,HTML中的转换器应该是这样使用的converter="javax.faces.DateTime"。这部分是在转换器类中或转换器类中javax.faces.DateTime定义的转换器名称。faces-config.xml@FacesConverter

于 2012-12-07T14:50:05.217 回答