我正在尝试制作一个简单的网络应用程序,并且我已经完成了注册和登录表单。现在我的数据库中有问题,我想在登录成功时显示问题并让用户回答测验的问题。
但问题是我是新手,我不知道应该如何使用。如果有人能帮助我指导一种方法,那么我会非常感激。 here
在Quiz.java
package com.quiz.model;
public class Quiz {
private int id;
private String question;
private String ans1;
private String ans2;
private String ans3;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAns1() {
return ans1;
}
public void setAns1(String ans1) {
this.ans1 = ans1;
}
public String getAns2() {
return ans2;
}
public void setAns2(String ans2) {
this.ans2 = ans2;
}
public String getAns3() {
return ans3;
}
public void setAns3(String ans3) {
this.ans3 = ans3;
}
}
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
}
}
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;
}
}
HomePageController.java
@Controller
public class HomePageController {
@Autowired
UserDaoImpl userDaoImpl;
@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);
}
}