'Spring MVC: The requested resource is not available
I am new to Spring framework exploring sample project where my jsp page will have a table to list out customer details which is already present in DB and there is Add Customer and Update & delete existing customer button.
When I click Add Customer or Update Delete button 404 error is shown.
Went through various answers in StackOverflow but issue not resolved yet. If someone can help here.
**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>CustomerManagementSystem</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>main-page.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>
<absolute-ordering/>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
**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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Add support for component scanning -->
<context:component-scan
base-package="com.gl.lib.cust"/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="jspViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/CustomerRelationshipManagement"></property>
<property name="username" value="root"></property>
<property name="password" value="password"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.gl.lib.cust.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
**Three JSP pages**
**main-page.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous">
<title>Customer Directory</title>
</head>
<body>
<div class="container">
<h3 style="background-color:green;">CUSTOMER RELATIONSHIP MANAGEMENT</h3>
<hr>
<form action="/CustomerManagementSystem/customer/showFormForAdd"
class="form-inline">
<a href="/CustomerManagementSystem/customer/showFormForAdd">
<button class="btn btn-primary btn-sm mb-3">Add Customer</button>
</a>
</form>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<c:forEach items="${customer}" var="temp">
<tr>
<td><c:out value="${temp.fname}" /></td>
<td><c:out value="${temp.lname}" /></td>
<td><c:out value="${temp.email}" /></td>
<td>
<!-- Add "update" button/link --> <a
href="/CustomerManagementSystem/customer/showFormForUpdate?email=${temp.email}"> Update </a>
<a
href="/CustomerManagementSystem/customer/delete?email=${temp.email}"
onclick="if (!(confirm('Are you sure you want to delete this student?'))) return false">
Delete </a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
**Customer-form.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous">
<title>Save Customer</title>
</head>
<body>
<div class="container">
<h3>CUSTOMER RELATIONSHIP MANAGEMENT</h3>
<hr>
<p class="h4 mb-4">Save Customer</p>
<form action="/CustomerManagementSystem/customer/save" method="POST">
<div class="form-inline">
<input type="text" name="name" value="${customer.fname}"
class="form-control mb-4 col-4" placeholder="First Name:">
</div>
<div class="form-inline">
<input type="text" name="category" value="${customer.lname}"
class="form-control mb-4 col-4" placeholder="Last Name:">
</div>
<div class="form-inline">
<input type="text" name="author" value="${customer.email}"
class="form-control mb-4 col-4" placeholder="Email:">
</div>
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
<hr>
<a href="/CustomerManagementSystem/customer/list">Back to List</a>
</div>
</body>
</html>
**Update-form.jsp:**
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="container">
<h3>CUSTOMER RELATIONSHIP MANAGEMENT</h3>
<hr>
<p class="h4 mb-4">Save Customer</p>
<form action="/CustomerManagementSystem/customer/update" method="POST">
<button type="submit" class="btn btn-info col-2">Save</button>
</form>
<table class="table table-bordered table-striped">
<!-- <thead class="thead-dark">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead> -->
<tbody>
<c:forEach items="${customer}" var="temp">
<tr>
<td>First Name: <c:out value="${temp.fname}" /></td>
<td>Last Name: <c:out value="${temp.lname}" /></td>
<td>Email: <c:out value="${temp.email}" /></td>
<td>
<!-- Add "save" button/link --> <a
href="/CustomerManagementSystem/customer/showFormForUpdate?email=${temp.email}"
class="btn btn-info btn-sm"> Save </a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<hr>
<a href="/StudentManagementSystem/student/list">Back to List</a>
</div>
</body>
</html>
**Controller Class**
package com.gl.lib.cust;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerServiceImplementation customerService;
@RequestMapping("/print")
public String listCustomer(Model theModel) {
List<Customer> ct = customerService.findAll();
// add to the spring model
theModel.addAttribute("customer", ct);
return "main-page";
}
@RequestMapping("/showFormForAdd")
public String showFormForAdd(Model theModel) {
Customer ct = new Customer();
theModel.addAttribute("customer", ct);
return "Customer-form";
}
@RequestMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("email") String email,
Model theModel) {
Customer ct = customerService.findByEmail(email);
theModel.addAttribute("customer", ct);
return "Update-form";
}
@PostMapping("/save")
public String saveCustomerRecord(@RequestParam("fname") String fname,
@RequestParam("lname") String lname,@RequestParam("email") String email) {
Customer ct;
ct=new Customer(fname, lname, email);
customerService.save(ct);
// use a redirect to prevent duplicate submissions
return "redirect:/customer/list";
}
@RequestMapping("/delete")
public String delete(@RequestParam("email")String email) {
// customerService.delete(email);
// redirect to /Books/list
return "redirect:/student/list";
}
public String index() {
return "forward:/customer/main-page.jsp";
}
}
My main-page.jsp is loaded correctly and all the main-page data is displayed but navigation to other 2 pages are failing.
Although the url's are forming correctly. Like, when I select Add Customer button the URL formed is "http://localhost:8080/CustomerManagementSystem/customer/showFormForAdd?"
I did mapped showFormForAdd in my controller class and that is routed to Customer-form.jsp page. Am I missing something over here?
Solution 1:[1]
Your main-menu is being loaded because you defined it in your web.xml as a welcome file.
Are you sure that you have Customer-form and Update-form JSP files in your views folder or did you name them differently because the string value that you return with your controller MUST match with JSP page name?
Also depending of your Eclipse version, when you are creating a new dynamic web project, Eclipse will create EITHER webapp folder or WebContent folder, and that is the project structure that you need to follow.
If your eclipse created webapp folder you cannot really create WebContent folder and expect it to work. Here is a proper structure of dynamic web projects for WebContent folder and webapp folder
Also this is how your ViewResolver bean should look like
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/views/"/>
<property name = "suffix" value = ".jsp"/>
</bean>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



