gpt4 book ai didi

Spring-Hibernate-MySQL 应用程序中的 javax.el.PropertyNotFoundException : Property not found on type java. lang.String

转载 作者:行者123 更新时间:2023-11-29 21:09:18 28 4
gpt4 key购买 nike

我有一个 mysql 查询,如果我在 mysql 工作台的查询编辑器中运行它,它会返回多行。现在我想将查询结果推送到列表,将列表添加到模型,并在 View 中显示结果。但是,我收到错误。我做错了什么,如何解决这个问题?

来自 Controller 的代码片段:

  @Transactional
@RequestMapping(value = "/question-editor", method = RequestMethod.GET)
public ModelAndView viewQuestionsToEdit(@RequestParam("username")String user, ModelAndView model, Principal principal){

model.setViewName("question-editor");

int id2 = 0;
try
{
if(user != null)
id2 = Integer.parseInt(user);
}
catch (NumberFormatException e)
{
id2 = 0;
}

Question question = em.find(com.databaseproject.questor.model.Question.class, id2);
model.addObject("question", question);

List<Question> questions = (List<Question>)em.createNativeQuery("SELECT q.questionText FROM question q WHERE idQuestion IN (SELECT qchq.Question_idQuestion FROM questioncart_has_question qchq JOIN (SELECT qc.idQuestionCart FROM questioncart qc WHERE User_username =:id2 ORDER BY idQuestionCart DESC LIMIT 1) qc ON qchq.QuestionCart_idQuestionCart = qc.idQuestionCart );")
.setParameter("id2", user).getResultList();

model.addObject("questionstoedit", questions);

String name = principal.getName(); //get logged in username
model.addObject("username", name);

return model;
}

尝试将值获取到 jsp View 文件中:

<c:forEach var="question" items="${questionstoedit}">
<p>Question no. ${question.questionText}</p>
</c:forEach>

我收到的错误是:

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

这是模型类:Question.java:

package com.databaseproject.questor.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class Question {

@Id
private int idQuestion;
private String questionText;
private String solutionText;
private byte[] image;
private String filepath;
private int year;
private String User_username;
private int Teacher_idTeacher;
private String Course_coursecode;

@Transient
private String encodedImage;

public int getIdQuestion() {
return idQuestion;
}

public void setIdQuestion(int idQuestion) {
this.idQuestion = idQuestion;
}

public String getQuestionText() {
return questionText;
}

public void setQuestionText(String questionText) {
this.questionText = questionText;
}

public String getSolutionText() {
return solutionText;
}

public void setSolutionText(String solutionText) {
this.solutionText = solutionText;
}

public byte[] getImage() {
return image;
}

public void setImage(byte[] image) {
this.image = image;
}

public String getFilepath() {
return filepath;
}

public void setFilepath(String filepath) {
this.filepath = filepath;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getUser_username() {
return User_username;
}

public void setUser_username(String user_username) {
User_username = user_username;
}

public int getTeacher_idTeacher() {
return Teacher_idTeacher;
}

public void setTeacher_idTeacher(int teacher_idTeacher) {
Teacher_idTeacher = teacher_idTeacher;
}

public String getCourse_coursecode() {
return Course_coursecode;
}

public void setCourse_coursecode(String course_coursecode) {
Course_coursecode = course_coursecode;
}

public String getEncodedImage() {
return encodedImage;
}

public void setEncodedImage(String encodedImage) {
this.encodedImage = encodedImage;
}
}

最佳答案

将返回类型更改为字符串列表:

List<String> questions = (List<String>)em.createNativeQuery("...");

您正在选择字符串的List,如果没有特定的映射器,则无法将其映射到 Question 类。

在 JSP 上应用此更改:

<c:forEach var="question" items="${questionstoedit}">
<p>Question no. ${question}</p>
</c:forEach>

关于Spring-Hibernate-MySQL 应用程序中的 javax.el.PropertyNotFoundException : Property not found on type java. lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36533537/

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