java - HTTP 状态 500 - javax.el.PropertyNotFoundException : Property 'first_name' not found on type java. lang.String

标签 java mysql spring hibernate jsp

我不知道我的代码有什么问题,我正在尝试在满足特定条件时从 mysql 数据库中检索值并将其显示在表格中。这是我的代码

学生表格.jsp

<c:url var="actionUrl" value="process" />
<form id="student-form" method="post" action="${actionUrl }">
  <select name="column">
    <option>*</option>
    <option>last_name</option>
    <option>first_name</option>
    <option>cp_num</option>
    <option>birthday</option>
    <option>grade</option>
  </select>&nbsp

  <select name="condition">
    <option>=</option>
    <option><</option>
    <option>></option>
    <option><=</option>
    <option>>=</option>
    <option>!=</option>
  </select>&nbsp

  <select id="gradeID" name="grades">//grades have a dropdown list of 50-100
  </select>&nbsp

   <button id=submit>Submit</button><br/><br/>
   </form>

  <table border="1" id="studentTable">
    <thead>
      <tr>
        <th align=center>First Name</th>
        <th align=center>Last Name</th>
        <th align=center>Cellphone Number</th>
        <th align=center>Birthday</th>
        <th align=center>Grade</th>
       </tr>
     </thead>

<tbody>
    <c:forEach items="${studentList}" var="student" >
        <tr>
            <td align=center><c:out value="${student.first_name}" /></td>
            <td align=center><c:out value="${student.last_name}" /></td>
            <td align=center><c:out value="${student.cp_num}" /></td>
            <td align=center><c:out value="${student.birthday}" /></td>
            <td align=center><c:out value="${student.grade}" /></td>
        </tr>
    </c:forEach>
   </tbody>

学生.java

@Entity
@Table(name = "student")
public class Student 
{
  private Long sid;
  private String last_name;
  private String first_name;
  private String cp_num;
  private String birthday;
  private int grade;

  @Id
  public Long getSid() 
  {
    return sid;
  }

  public void setSid(Long sid) 
  {
    this.sid = sid;
  }

  @Column
  public String getLast_name() 
  {
    return last_name;
  }

  public void setLast_name(String last_name) 
  {
    this.last_name = last_name;
  }

  @Column
  public String getFirst_name() 
  {
    return first_name;
  }

  public void setFirst_name(String first_name) 
  {
    this.first_name = first_name;
  }

  @Column
  public String getCp_num() 
  {
    return cp_num;
  }

  public void setCp_num(String cp_num) 
  {
    this.cp_num = cp_num;
  }

  @Column
  public String getBirthday() 
  {
    return birthday;
  }

  public void setBirthday(String birthday) 
  {
    this.birthday = birthday;
  }

  @Column
  public int getGrade() 
  {
    return grade;
  }

  public void setGrade(int grade) 
  {
    this.grade = grade;
  }
}

Controller

@Controller
@RequestMapping("/StudentInformation")
public class StudentController 
{
  @Autowired
  private StudentDao studentDao;

  @RequestMapping(value = "/StudentForm")
  public String showForm() 
  {
    return "StudentForm";
  }

  @RequestMapping(value = "/process", method = RequestMethod.POST)
  public String process(@RequestParam(value="column", defaultValue="") String column,
        @RequestParam(value="condition", defaultValue="") String condition,
        @RequestParam(value="grades", defaultValue="") int grades, Map<String, Object> map)
  {
    map.put("studentList", studentDao.findData(column, condition, grades));
    return "StudentForm";
  }
}

StudentDaoImpl

@Repository
public class StudentDaoImpl implements StudentDao 
{
  @Autowired
  private SessionFactory sessionFactory;
  @Autowired
  private Student student;

  private SessionFactory getSessionFactory() 
  {
    return sessionFactory;
  }

  @SuppressWarnings("unchecked")
  public List<Student> findData(String column, String condition, int grades)
  {
    String hql = "SELECT x."+ column +" FROM Student x WHERE x.grade"+ condition +":grades";
    Query query = getSessionFactory().openSession().createQuery(hql);
    query.setParameter("grades", grades);
    List<Student> result = query.list();

    return (List<Student>)result;
  }
}

学生道

public interface StudentDao 
{
  public List<Student> findData(String column, String condition, int grades);
}

错误代码

HTTP Status 500 - javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String

type Exception report

message javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property 'first_name' not found on type java.lang.String
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:172)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

javax.el.PropertyNotFoundException: Property 'first_name' not found on type  java.lang.String
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:266)
javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
javax.el.BeanELResolver.property(BeanELResolver.java:353)
javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
org.apache.jsp.WEB_002dINF.views.StudentForm_jsp._jspx_meth_c_005fout_005f0(StudentForm_jsp.java:290)
org.apache.jsp.WEB_002dINF.views.StudentForm_jsp._jspx_meth_c_005fforEach_005f0(StudentForm_jsp.java:241)
org.apache.jsp.WEB_002dINF.views.StudentForm_jsp._jspService(StudentForm_jsp.java:135)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:172)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

最佳答案

您可以通过多种方式解决此问题:-

1) 更改 Map<String,Object> 中的最后一个参数至 ModelMap model

@RequestMapping(value = "/process", method = RequestMethod.POST)
public ModelAndView process(@RequestParam(value="column", defaultValue="") String column,
        @RequestParam(value="condition", defaultValue="") String condition,
        @RequestParam(value="grades", defaultValue="") int grades, 
        ModelMap model)
  {
    model.addAttribute("studentList", studentDao.findData(column, condition, grades));
    return "StudentForm";
  }

2) 第二个选项是返回 ModelAndView按照这个 What is Model in ModelAndView from Spring MVC?

关于java - HTTP 状态 500 - javax.el.PropertyNotFoundException : Property 'first_name' not found on type java. lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976814/

相关文章:

java - Java 中的对象引用有多大,它究竟包含什么信息?

java - 在 H2 中持久化 ManyToMany 实体会引发异常

java - 开始使用 Spring 构建 java 应用程序

mysql - 插入另一个表中的所有用户

java - 可以通过 OSGI 与运行在不同服务器上的组件进行通信

java - Android 搜索栏值更改按钮

java - 在 Activity 中添加后退按钮

java - 如何在 servlet 中识别调用来自 href 链接点击?

php - 表单重复插入所有用户的数据

php - 在 WHILE 之前拉取连接表数据