Property [id] not found on type [java.lang.String]

2019-09-20 14:57发布

when I am displaying data on studentbyid.jsp page using controller, then throw an error -> There was an unexpected error (type=Internal Server Error, status=500). Property [id] not found on type [java.lang.String]


@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 Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

There was an unexpected error (type=Internal Server Error, status=500). Property [id] not found on type [java.lang.String]

2条回答
干净又极端
2楼-- · 2019-09-20 15:39

I think, probably cause is incorrect jstl expression in your jsp
Try to replace this <c:forEach var="student" items="students"> To this <c:forEach var="student" items="${students}">

查看更多
虎瘦雄心在
3楼-- · 2019-09-20 15:44

As students is pointing to list of student objects but not a string. So wrap it between ${}

items="${students}">
查看更多
登录 后发表回答