Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring MVC database display

I have created a mySQL table: student.studentInfo with:-

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

User fills up a StudentRegForm and The values of name, age and email are shown on a RegisteredStudent jsp.

I want to display contents [data elements] of the mySQL table on a jsp page.

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;
    }   
}

The above StudentMapper method is:

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;
    }
}

View Resolver is set on MvcConfiguration and DataSource props are declared as @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;
    }
}

Students fill up this 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 class is:

@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 is:

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);

}

And StudentImplDao is:

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
    //………………………………
}

I can input name, age, email on the Studentform.jsp and those inputs are displayed on RegdSuccess.jsp, but my RegdStudent.jsp page is not displaying list records from my database as I want it to. RegdStudent.jsp is:

<%@ 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>

I would like to have this JSP age show all the records from the mysql database.

like image 660
javaz Avatar asked May 18 '26 07:05

javaz


1 Answers

By doing

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

you're coercing the listStudent List to a string, making it impossible to iterate over it later in your JSP. If you change the line to:

mav.addObject("msg", listStudent);

the ${msg} object will be an iterable list.


It seems that you have no idea what works and what doesn't in your application since you dumped code for all layers. You should familiarise yourself with your IDE's debugger and learn to follow request's path to see what's going on. Narrowing down the problem drastically reduces time needed to find and fix bugs.

like image 175
kryger Avatar answered May 21 '26 04:05

kryger