我想在表单中放置一个对象列表:选择并将选定的Discipline值链接到路径:提交时Grade对象的“subject”。
这就是我现在的做法,将grade.discipline.id链接到discipline.id。它正在工作,我在服务器上使用了调试,并看到了成绩对象的规则如何获得规则对象。但是我想知道是否有“出书的方式”,而不仅仅是我的即兴创作。
addGrade.jsp:
<form:form commandName="grade">
<table>
<tr>
<th>Value = </th>
<td><form:input path="value"/></td>
<td><form:errors path="value"/></td>
</tr>
<tr>
<th>Select discipline: </th>
<td colspan = "2">
<form:select path="discipline.id">
<c:forEach items="${disciplines}" var="d">
<option value="${d.id}">${d.name}</option>"
</c:forEach>
</form:select>
</td>
<td><form:errors path="discipline"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
GradeController.java
@RequestMapping(value = "/addGrade", method = RequestMethod.GET)
public String addGradeGet (Model model, HttpSession session){
//this.populateDefaultModel(session);
List<Discipline> disciplines = disciplineService.findAll();
Student student = (Student)session.getAttribute("student");
if(student==null)
return "redirect:/showStudent.html";
model.addAttribute("disciplines",disciplines);
model.addAttribute("currentStudent",student);
model.addAttribute("grade",new Grade());
return "addGrade";
}
@RequestMapping(value = "/addGrade", method = RequestMethod.POST)
public String addGrade(@Valid @ModelAttribute ("grade") Grade grade,
BindingResult result, Model model){
List<Discipline> disciplines = disciplineService.findAll();
model.addAttribute("disciplines",disciplines);
/*if we found a grade of the current student
that has the same discipline as the new grade that we try to add
then we are not allowed to add it
*/
Student student = (Student) model.asMap().get("currentStudent");
grade.setStudent(student);
if(gradeService.findByStudentAndDiscipline(grade)!=null){
result.rejectValue("discipline", "duplicate");
}
if(!(result.hasErrors()) ){
gradeService.save(grade);
return "redirect:/students.html";
}
else{
return "addGrade";
}
}
对象Discipline:
@Entity
public class Discipline {
@Id
@GeneratedValue
private Long id;
@NotNull
private String name;
@OneToMany(mappedBy="discipline", cascade=CascadeType.ALL,fetch=FetchType.LAZY,orphanRemoval=true)
private List<Grade> grades = new ArrayList<Grade>();
// getters and setters .....
}
对象等级:
@Entity
public class Grade {
@Id
@GeneratedValue
private Long id;
@Range(min=1, max=10)
@NotNull
private int value;
@ManyToOne
private Student student;
@ManyToOne
private Discipline discipline;
// getters and setters ....
}
我知道我应该使用:
<form:select path="discipline">
<form:options items="${disciplines}"/>
</form:select>
我确实尝试过,将discipHashMap放在纪律属性中:
List<Discipline> disciplines = disciplineService.findAll();
Map<Discipline,String> disciplinesHashMap = new LinkedHashMap<Discipline,String>();
for (Discipline temp:disciplines){
disciplinesHashMap.put(temp, temp.getName());
}
model.addAttribute("disciplines",disciplinesHashMap);
我得到了包含所有正确标签的页面但是当我尝试提交时,我在GradeService中的一个方法中获取了一个nullPointer异常,该方法正在搜索我的成绩对象的学科值,在POST方法中调用的方法addGrade(也在服务器上的调试中我看到成绩对象的学科字段为null)...所以,这意味着我没有设法将学科值链接到对象:
java.lang.NullPointerException
com.app.service.GradeServiceImpl.findByStudentAndDiscipline(GradeServiceImpl.java:31)