spring MVC数据库显示

时间:2014-08-15 20:12:59

标签: spring spring-mvc jdbc jstl datasource

我创建了一个mySQL表:student.studentInfo: -

Int id autofill PK, 
 String name, 
int age,  
String email.

用户填写StudentRegForm,姓名,年龄和电子邮件的值显示在RegisteredStudent jsp上。

我想在jsp页面上显示mySQL表的内容[数据元素]。

My StudentDaoImpl:

public class StudentDaoImpl implements StudentDao {
    DataSource datasource;
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource datasource) {
        this.datasource = datasource;
        this.jdbcTemplate = new JdbcTemplate(datasource);
    }


    @Override
    public List<Student> getStudentList() {
        List<Student> studentList = new ArrayList<Student>();
        String sql = "select * from student.studentInfo";
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        studentList = jdbcTemplate.query(sql, new StudentMapper());
        return studentList;
    }   
}

上面的StudentMapper方法是:

public class StudentMapper implements RowMapper<Student>{

    @Override
    public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
        Student student = new Student();
        student.setId( rs.getInt("id"));
        student.setName(rs.getString("name"));
        student.setAge(rs.getInt("age"));
        student.setEmail(rs.getString("email"));

        return student;
    }
}

在MvcConfiguration上设置了View Resolver,并且DataSource props被声明为@Bean:

@Configuration
@ComponentScan(basePackages = "com.anand")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
                "/resources/");
    }

    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/student");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        return dataSource;
    }
}

学生填写此Studentform.jsp:

<form   action="StudentAddSuccess.htm" method=post>
${formHeading}
<p>
id <input type="text" name="id" />
</p>
<p>
Name <input type="text" name="name"/>
</p>
<p>
Age <input type="text" name="age" />
</p>
<p>
Email <input type="text" name="email" />
</p>

<p>
<input type="submit" name="submit" value="save">
</p>

</form>

控制器类是:

@Controller
public class StudentController {    
    @Autowired
    StudentDao stdDao;  
    @RequestMapping(value = "/studentForm.htm", method = RequestMethod.GET)
    public ModelAndView sendStudentForm() {
        ModelAndView mav = new ModelAndView("studentForm");
        mav.addObject("formHeading", "Student Registration Form");
        mav.setViewName("studentForm");
        System.out.println("student Form");
        return mav;
    }
//**********************************************************
    @RequestMapping(value = "RegdSuccess", method = RequestMethod.POST)
    public ModelAndView addStudent(@ModelAttribute("student1") Student student1) {
        ModelAndView mav = new ModelAndView();
        //mav.addObject("studentId", "id is:-" + student1.getId());
        mav.addObject("studentName", " name is:- " + student1.getName());
        mav.addObject("studentAge", " age is:-" + student1.getAge());
        mav.addObject("studentEmail", "student email is:-" + student1.getEmail());
        // mav.addObject("student", "student list is"+ student1);
        System.out.println(" hello  from RegdSuccess");
        mav.setViewName("RegdSuccess");
        return mav;
    }
//********************************************************************  


@RequestMapping( value = "RegdStudent", method = RequestMethod.GET)
        public ModelAndView showStudent(@ModelAttribute("std")Student std) throws IOException{
            ModelAndView mav = new ModelAndView();
            List<Student> listStudent= stdDao.getStudentList(); 
            mav.addObject("msg", "hello from Regd jsp");
            //mav.addObject("listStudent", listStudent);
            mav.addObject("msg",""+listStudent);

            System.out.println(" hello  from Regd Students controller"+std.getName());
            mav.setViewName("RegdStudent");

        return mav;
    }

StudentDao是:

public interface StudentDao {   
    // create
    public void createStudent(Student student);

    // read
    public Student getStudent(Integer id);

    // Update
    public void updateStudent(Student student);

    // delete
    public void deleteStudent(Integer id);

    // List
    public List<Student> getStudentList();

    // save
    public void save(Student student);

}

StudentImplDao是:

public class StudentDaoImpl implements StudentDao {
    StudentDaoImpl StudentDao;  
    DataSource datasource;
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource datasource) {
        this.datasource = datasource;
        this.jdbcTemplate = new JdbcTemplate(datasource);
    }

    public void doExecute() {
        String sql = "SELECT * FROM STUDENT.StudentInfo";
        SqlRowSet srs = jdbcTemplate.queryForRowSet(sql);
        int rowCount = 0;
        while (srs.next()) {
            System.out.println(srs.getString("id") + "-"
                    + srs.getString("name") + "-" + srs.getString("age") + "-"
                    + srs.getString("email"));

        }
        rowCount++;
        System.out.println("Number of records" + rowCount);
    }

    // -------------List----------------------------------------
    @Override
    public List<Student> getStudentList() {

        List<Student> studentList = new ArrayList<Student>();
        String sql = "select * from student.studentInfo";
        JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
        studentList = jdbcTemplate.query(sql, new StudentMapper());
        return studentList;

    }
 // other remaining methods of StudentDao go here for the implementations
    //………………………………
}

我可以在Studentform.jsp上输入姓名,年龄,电子邮件,这些输入显示在RegdSuccess.jsp上,但我的RegdStudent.jsp页面没有按我的意愿显示数据库中的列表记录。 RegdStudent.jsp是:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<head>
<title>Regd. Students</title>
</head>
<body>
    hello from Regd Students jsp 
    <form:form>
    <table>

        <c:forEach items="${msg}" var="employee">
            <tr>
                <td><c:out value="${employee.id}" /></td>
                <td><c:out value="${employee.name}" /></td>
                <td><c:out value="${employee.age}" /></td>
                <td><c:out value="${employee.email}" /></td>
            </tr>
        </c:forEach>

    </table>
    </form:form>
</body>
</html>

我想让这个JSP时代显示来自mysql数据库的所有记录。

1 个答案:

答案 0 :(得分:2)

通过

mav.addObject("msg",""+listStudent);

您将listStudent列表强制转换为字符串,因此无法在JSP中稍后对其进行迭代。如果您将行更改为:

mav.addObject("msg", listStudent);

${msg}对象将是一个可迭代列表。


由于您为所有图层转储了代码,因此您似乎不知道应用程序中的功能和功能。您应该熟悉IDE的调试器,并学会按照请求的路径查看正在进行的操作。缩小问题可以大大减少查找和修复错误所需的时间。