我有一个mysql查询,如果我在mysql workbench的查询编辑器中运行它,则返回多行。现在,我想将查询中的结果推送到列表,将列表添加到模型中,并在视图中显示结果。但是,我收到了一个错误。我做错了什么,以及如何解决这个问题?
来自控制器的代码段:
@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视图文件中:
<c:forEach var="question" items="${questionstoedit}">
<p>Question no. ${question.questionText}</p>
</c:forEach>
我得到的错误是:
javax.el.PropertyNotFoundException:Property&#39; questionText&#39;未找到 在类型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;
}
}
答案 0 :(得分:0)
将返回类型更改为字符串列表:
List<String> questions = (List<String>)em.createNativeQuery("...");
您正在选择List
个字符串,没有特定的Mapper,它无法映射到Question类。
在JSP上应用此更改:
<c:forEach var="question" items="${questionstoedit}">
<p>Question no. ${question}</p>
</c:forEach>