在春天没有得到理想的输出

时间:2017-05-21 16:17:05

标签: java spring jsp

我在quiz.jspplay.jsp中有一个链接。在play.jsp我想显示测验问题和数据库的答案,但我没有得到它。 如果有任何帮助,那么我会非常感激。

Quiz.java

public class Quiz {
    private int id;
    private String question;
    private String ans1;
    private String ans2;
    private String ans3;

    // getters and setters here
}

QuizRowMapper.java

public class QuizRowMapper implements RowMapper {

    public Quiz mapRow(ResultSet rs, int row) throws SQLException {
        Quiz quiz = new Quiz();
        quiz.setId(rs.getInt("idquestion"));
        quiz.setQuestion(rs.getString("question"));
        quiz.setAns1(rs.getString("ans1"));
        quiz.setAns2(rs.getString("ans2"));
        quiz.setAns3(rs.getString("ans3"));
        return quiz;
    }
}

QuizDaoImpl.java

public class QuizDaoImpl implements QuizDao {
    private JdbcTemplate template;

    @Autowired
    DataSource dataSource;

    public void setTemplate (JdbcTemplate template){
        this.template = template;
    }

    public List<Quiz> getQuestion() {
        List questionList = new ArrayList();
        String sql = "select * from question";
        JdbcTemplate template = new JdbcTemplate(dataSource);
        questionList = template.query(sql, new QuizRowMapper());
        return questionList;
    }

    public void insertQuestion() {
        // TODO Auto-generated method stub
    }
}

HomePageController.java

@Controller
public class HomePageController {

    @Autowired
    UserDaoImpl userDaoImpl;

    @Autowired
    QuizDaoImpl quizDaoImpl;

    @RequestMapping("/register")
    public ModelAndView registerUser(@ModelAttribute user use){
        return new ModelAndView("register");    
    }

    @RequestMapping("/insert")
    public ModelAndView successMessage(@ModelAttribute user use){
        if(use != null){
            userDaoImpl.insertData(use);
        }
        return new ModelAndView("success" ,"name", use.getuName());
    }

    @RequestMapping("/login")
    public ModelAndView getUserList(@ModelAttribute user u){
        List<user> userList = userDaoImpl.getData();
        int len = userList.size();
        for(int i = 0;i< len ; i++){
            String userr = userList.get(i).getuName();
            if(userr.equals(u.getuName())){
                return new ModelAndView("quiz","name", u.getuName());
            }
        }
        return new ModelAndView("register");
    }

    @RequestMapping("/userSign")
    public ModelAndView getUser(String name){

        List<user> userList = userDaoImpl.getData();
        int len = userList.size();
        for(int i = 0;i<len ; i++){
            String userr = userList.get(i).getuName();
            if(userr.equals(name)){
                return new ModelAndView("quiz","name", name);
            }
        }
        return new ModelAndView("error","name",name);
    }

    @RequestMapping(value="/play.jsp")
    public ModelAndView getQuestion(){
        List<Quiz> quiz= quizDaoImpl.getQuestion();
        return new ModelAndView("play", "quiz", quiz);
    }
}

quiz.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>play quiz</title>
</head>
<body>
    Welcome ${name}
    <a href="play.jsp">quizz</a>
</body>
</html>

play.jsp

<html>  
<head>  
    <title>Being Java Guys | quiz </title>   
</head>  
<body>  
    <center>  
        <br /> <br /> <br /> <b>User  
         List | Being Java Guys </b><br /> <br />  
         <p>Question</p>
         <p> ${quiz.id}</p>
         <p>{quiz.question}</p><br>
         <c:forEach items="${quiz}" var="qz">
             <p> ${qz.id}</p>
             <p>{qz.question}</p><br>
             <p>${qz.ans1}</p>
             <p> ${qz.ans2} </p>
             <p>${qz.ans3} </p>
         </c:forEach>
     </center>  
</body>  
</html>  

2 个答案:

答案 0 :(得分:0)

在你的play.jsp中输入了

${user.ans1}

而不是

${quiz.ans1}

然后,由于您要将一个列表对象返回到play.jsp,您将使用jstl foreach标记循环遍历列表

<c:forEach items="${quiz}" var="qz"> ${qz.question} ${qz.ans1} ${qz.ans2} ${qz.ans3} <br> </c:forEach>

请记住在页面顶部添加jstl taglib uri。这是

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

答案 1 :(得分:0)

您忘了将@ResponseBody放在/play.jsp端点中...添加可能解决问题的方法......