当我使用控制器在studentbyid.jsp页面上显示数据时,然后引发错误->发生意外错误(类型=内部服务器错误,状态= 500)。 在类型[java.lang.String]
上找不到属性[id]
@Controller
public class StudentController {
@Autowired
private StudentService studentService ;
@GetMapping(value = "/students")
public String index(ModelMap modelMap){
modelMap.put("students" , studentService.findAll());
System.out.println(" Student for id =" + studentService.findAll());
return "index" ;
}
@GetMapping(value = "/studentid")
public String studentdetailed(@RequestParam("id") int id , ModelMap modelMap){
modelMap.put("students" , studentService.findById(id));
System.out.println(" Student for id =" + studentService.findById(id));
return "studentbyid" ;
}
@GetMapping(value = "/student")
public String databyid(){
return "student" ;
}
}
public interface StudentService {
public Iterable<Student> findAll() ;
public Optional<Student> findById(int id) ;
}
@Transactional
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository ;
@Override
public Iterable<Student> findAll() {
return studentRepository.findAll();
}
public Optional<Student> findById(int id) {
return studentRepository.findById(id);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Data</title>
</head>
<body>
<c:forEach var="student" items="students">
<c:out value="${student.id}"/>
<c:out value="${student.firstName}"/>
<c:out value="${student.lastName}"/>
<c:out value="${student.branch}"/>
<c:out value="${student.year}"/>
<c:out value="${student.mobileNumber}"/>
</c:forEach>
</body>
</html>
@Entity
@Table(name = "student")
public class Student {
@Id
@Column(name="id")
private int id ;
@Column(name="first_name")
private String firstName ;
@Column(name="last_name")
private String lastName ;
@Column(name="branch")
private String branch ;
@Column(name="year")
private int year ;
@Column(name="mobile_number")
private String mobileNumber ;
public Student() {
}
public Student(int id, String firstName, String lastName, String branch, int year, String mobileNumber) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.branch = branch;
this.year = year;
this.mobileNumber = mobileNumber;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", branch='" + branch + '\'' +
", year=" + year +
", mobileNumber='" + mobileNumber + '\'' +
'}';
}
}
Whitelabel错误页面 此应用程序没有针对/ error的显式映射,因此您将其视为后备。
发生意外错误(类型=内部服务器错误,状态= 500)。 在[java.lang.String]类型上找不到属性[id]
答案 0 :(得分:0)
我认为,可能是因为您的jsp中的jstl表达式不正确
尝试取代这个
<c:forEach var="student" items="students">
对此
<c:forEach var="student" items="${students}">
答案 1 :(得分:0)
students
指向学生对象列表,而不是字符串。因此将其包装在$ {}
items="${students}">