While trying to access http://localhost:8080/student_tracker_2/student/edit/5
, my Spring is not redirecting me to add-student-form which would prepopulate the page based on the data in db.
But it gives me a 404 error.
The control is getting inside the controllers but not resolving the view. All the other controllers are working fine i.e add Student, remove and list all students. Get Student used in the edit part is also working fine.
The update-form and student-form are the same..so student form helps to add as well as update/edit student.
here are screenshots to understand the problem better:
list-students
adding a student
after clicking edit button
Error Message:-
HTTP Status 404 – Not Found
Type Status Report
Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Message: JSP file [/student/edit/WEB-INF/views/student-form.jsp] not found
This is the file structure
Here are the files:
- web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>student_tracker_2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>student_tracker_2</display-name>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2. spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="com.nainesh">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<tx:annotation-driven />
<!-- <bean id="welcomeService" class="com.nainesh.spring.service.WelcomeService"></bean> -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- data source -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
name="ds">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/spring_poc" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<!-- LocalSessionFactoryBean -->
<bean
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
name="factory">
<!-- data source -->
<property name="dataSource" ref="ds"></property>
<!-- hibernate properties -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- annotated classes -->
<property name="annotatedClasses">
<list>
<value>com.nainesh.entity.Student</value>
</list>
</property>
</bean>
<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
name="hibernateTemplate">
<property name="sessionFactory" ref="factory"></property>
</bean>
<bean
class="org.springframework.orm.hibernate5.HibernateTransactionManager"
name="transactionManager">
<property name="sessionFactory" ref="factory"></property>
</bean>
</beans>
3.StudentController
package com.nainesh.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.nainesh.entity.Student;
import com.nainesh.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students")
public String listStudents(Model model) {
List<Student> students=studentService.getStudents();
model.addAttribute("studentList", students);
return "list-students";
}
@GetMapping("/addForm")
public String addForm(Model model) {
Student student=new Student();
model.addAttribute("student", student);
return "student-form";
}
@PostMapping("/student/add")
public String addStudent(@ModelAttribute("student") Student student) {
studentService.addStudent(student);
return "redirect:/students";
}
@GetMapping("/student/edit/{id}")
public String updateStudent(@PathVariable("id") int id,Model model) {
System.out.println("-------------------Into update mapping-------------------");
Student student=studentService.getStudent(id);
model.addAttribute("student", student);
return "student-form";
}
@GetMapping("/student/remove/{id}")
public String removeStudent(@PathVariable("id") int id) {
studentService.removeStudent(id);
return "redirect:/students";
}
}
- student-form
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is the add student form.
<form:form action="${pageContext.request.contextPath}/student/add"
modelAttribute="student" method="POST">
<form:hidden path="id" />
<table>
<tbody>
<tr>
<td><label>First Name:</label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><label>Last Name:</label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><label>Email:</label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><label>Age:</label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><label>Education:</label></td>
<td><form:input path="education" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save" class="save" /></td>
</tr>
</tbody>
</table>
</form:form>
</body>
</html>
- pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.nainesh</groupId>
<artifactId>student_tracker_2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.3.Final</version>
</dependency>
<!-- SQL CONNECTOR -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
UPDATE: I did miss a '/' in view resolver before WEB-INF as M.Denium pointing out. Thank you Sir.