gpt4 book ai didi

java - JSF 2.0 "Conversion Error setting value ' courseID' for 'null Converter' .!!我怎样才能避免这种错误?

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:41 26 4
gpt4 key购买 nike

我正在使用 PrimeFaces、JPA、Hibernate 和 JSF 2.0 开发 Web 应用程序。

我有两个实体 StudentsCourse具有多对一的关系。

@Entity
@Table(name = "STUDENTS")
public class Students extends MainUsers implements Serializable {

private String firstName;
private String lastName;
private long mobile;
private String jobTitle;
private int duration;
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;
@Temporal(TemporalType.TIMESTAMP)
private Date endDate;

@ManyToOne
@JoinColumn(name = "course_id", nullable = true)
private Course company;

// Getters and setters.
}
@Entity
@Table(name = "COURSE")
@NamedQueries({
@NamedQuery(name = "course.findCourseByCourseId", query = "select c from Course c where c.Id =:id"),
@NamedQuery(name = "course.findCourseByCourseName", query = "select c from Course c where c.courseName =:courseName") })
public class Course implements Serializable {

public static final String FIND_COURSE_BY_COURSE_ID = "course.findByCourseId";
public static final String FIND_COURSE_COURSE_NAME = "course.findByCourseName";

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int Id;
private String courseName;
@OneToMany(targetEntity = Students.class, mappedBy = "course", fetch = FetchType.LAZY)
private List<Students> students;

// Getters and setters.

@Override
public int hashCode() {
return getId();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Course) {
Course course = (Course) obj;
return course.getId() == Id;
}

return false;
}

}

我有一个注册页面,其中包含学生的姓名、电子邮件、密码和类(class)。为了选择类(class),我使用了这个

<h:outputText value="Course :" />
<p:selectOneMenu value="#{courseMB.course}">
<f:selectItem itemLabel="Select one Course" itemValue="" />
<f:selectItems value="#{courseMB.allCourses}" var="crs"
itemLabel="#{crs.courseName}" itemValue="#{crs.id}" />
<f:converter converterId = "courseConverter"/>
</p:selectOneMenu>

要创建学生,我在 StudentMB 类中有以下方法:

public void createStudent() {       
String test = student.getEmail();

if (getStudentFacade().isStudentExist(test)){
System.out.println(test);
} else {
try {
student.setActivation_key(hashKey());
student.setRole(Role.STUDENT);
student.setActive(0);
getStudentFacade().createStudent(student);
displayInfoMessageToUser("Created with success");
SendEmail.Send(username, password, student.getEmail(), title, linkToSend());
loadStudent();
resetStudent();
} catch (Exception e) {
displayErrorMessageToUser("Opps, we could not create the user. Try again");
e.printStackTrace();
}
}
}

我真的不知道如何将类(class)放入 StudentMB 并将其保存到数据库中。当前的方法给了我一个错误

Conversion Error setting value 'courseID' for 'null Converter'.

我尝试使用转换器,但参数中出现空字符串。

@FacesConverter(value="courseConverter", forClass = com.model.Course.class)
public class CourseConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
CourseFacade courseFacade = new CourseFacade();
int courseId;

try {
courseId = Integer.parseInt(submittedValue);
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Type the name of a Course and select it (or use the dropdown)",
"Type the name of a Course and select it (or use the dropdown)"));
}

return courseFacade.findCourse(courseId);
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
Course course = null;
if (modelValue instanceof Course){
course = (Course) modelValue;
StringBuilder asString = new StringBuilder();
asString.append(course.getId()+"-");
asString.append(course.getCourseName());
return asString.toString();

}
return "";
}

}

这是怎么造成的,如何解决?

最佳答案

您需要将选择项值设置为类(class)本身,而不是其id

替换

itemValue="#{crs.id}"

itemValue="#{crs}"

否则转换器只会在 getAsString() 中将 #{crs.id} 获取为 modelValue,从而返回空字符串。该空字符串又通过 getAsObject() 提交回服务器,从而导致异常。

您应该在 if (modelValue instanceof Course)else 部分抛出 ConverterException,以便立即清楚该转换器不适合给定的模型值。

另请参阅:

关于java - JSF 2.0 "Conversion Error setting value ' courseID' for 'null Converter' .!!我怎样才能避免这种错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13661196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com