gpt4 book ai didi

java - 在 com.spring.schoolmanagement.model.Subject 类型上找不到属性 'id'

转载 作者:行者123 更新时间:2023-12-01 11:48:13 25 4
gpt4 key购买 nike

我试图将两个对象放入 JSP View 中,但是通过 getter 获取值时工作正常,而通过对象的属性引用值时会引发错误。

JSP 代码片段是:

<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>

模型类:

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 + "}";
}
}

Controller 类:

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";
}
}

请指出我在哪里犯了错误。

最佳答案

更改 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)命名约定。

关于java - 在 com.spring.schoolmanagement.model.Subject 类型上找不到属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984081/

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