我试图在我的JSP视图中放置两个对象但是通过getter获取值工作正常并且在引用对象属性的值时抛出错误。
JSP Code Snipet是:
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr><h2 class="intro-text text-center">Manage Subject</h2><hr>
</div>
<div class="col-md-12">
<c:url var="addAction" value="/data-entry/subject/add" ></c:url>
<form:form action="${addAction}" commandName="subject">
<table>
<c:if test="${!empty subject.name}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td>
<form:label path="description">
<spring:message text="Description"/>
</form:label>
</td>
<td>
<form:input path="description" />
</td>
</tr>
<tr>
<td colspan="2">
<c:if test="${!empty subject.name}">
<input type="submit"
value="<spring:message text="Edit Subject"/>" />
</c:if>
<c:if test="${empty subject.name}">
<input type="submit"
value="<spring:message text="Add Subject"/>" />
</c:if>
</td>
</tr>
</table>
</form:form>
<br>
<h3>Subject List</h3>
<c:if test="${!empty subjectlist}">
<table class="tg">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${subjectlist}" var="subject">
<tr>
<td>${subject.getID()}</td>
<td>${subject.getName()}</td>
<td>${subject.getDescription()}</td>
<td><a href="<c:url value='/admin/subject/edit/${subject.getID()}' />" >Edit</a></td>
<td><a href="<c:url value='/admin/subject/remove/${subject.getID()}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</div>
</div>
</div>
&#13;
模特课程:
package com.spring.schoolmanagement.model;
public class Subject {
private int id;
private String name;
private String description;
public void setID(int id) {
this.id = id;
}
public int getID() {
return this.id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setDescription( String description ) {
this.description = description;
}
public String getDescription() {
return this.description;
}
@Override
public String toString(){
return "{ID=" + id + ",Name=" + name + ",Description=" + description + "}";
}
}
&#13;
控制器类:
package com.spring.schoolmangement;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spring.schoolmanagement.dao.SubjectDAOImpl;
import com.spring.schoolmanagement.model.Subject;
/**
* Handles requests for admin specific pages.
*/
@Controller
public class AdminController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
private SubjectDAOImpl subjectService;
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = {"/admin/", "/admin"}, method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "admin-dash-board";
}
@RequestMapping(value = "/admin/student")
public String student(Model model) {
return "admin-student";
}
@RequestMapping(value = "/admin/subject")
public String displaySubjects(Model model) {
model.addAttribute("subject", new Subject());
model.addAttribute("subjectlist", this.subjectService.getAll());
return "manage-subject";
}
@RequestMapping(value = "/admin/subject/edit/{id}")
public String course(@PathVariable("id") int id, Model model) {
model.addAttribute("subject", this.subjectService.getById(id));
model.addAttribute("subjectlist", this.subjectService.getAll());
return "manage-subject";
}
}
&#13;
请建议我犯错的地方。
答案 0 :(得分:1)
从以下位置更改您的getter / setter方法:
public void setID(int id) {
this.id = id;
}
public int getID() {
return this.id;
}
要
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
您的Bean缺少命名约定,应遵循JavaBean (read pdf section 8.8)命名约定。