Spring MVC Database Mapping
I created mySQL table: student.studentInfo with: -
Int id autofill PK,
String name,
int age,
String email.
The user fills in the StudentRegForm and the values ββfor name, age and email are displayed on the jsp RegisteredStudent.
I want to display the content [items] of a mySQL table in 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:
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 to 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 out 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:
@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);
}
And 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
//β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦β¦
}
I can enter name, age, email on Studentform.jsp and these inputs show up to RegdSuccess.jsp, but my RegdStudent.jsp page does not show list entries from my database as I want. 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>
I would like this age JSP to show all records from mysql database.
source to share
By making
mav.addObject("msg",""+listStudent);
you are forcing the list listStudent
to be a string, making it impossible to repeat it later in your JSP. If you change the line to:
mav.addObject("msg", listStudent);
the object ${msg}
will be iterable.
You don't seem to know what is working and what is not in your application, since you were dumping code for all layers. You should familiarize yourself with your IDE debugger and learn to follow the path of the request to see what's going on. Narrowing down the problem drastically reduces the time it takes to find and fix errors.
source to share