I am trying to build a simple application to create and update few records using spring MVC. But not able to proceed further as I am facing issue that my spring form tag has some error and I am not able to figure out since 2 days. error says:
java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?
Few Doubts as well :
Why do I need ContextLoaderListener when I am using dispatcherServlet.
Every DispatcherServlet has its own or instantiates WebApplicationContext. Then why is the error saying its not available ?
Anything that I am missing, conceptually or programmatically ?
Welcome.jsp
<html>
<body>
<h2>Welcome to the ADStore Portal</h2>
<a href="WEB-INF/views/addEmp.jsp">Add Employee</a><br>
<!-- <a href="updateEmployee.jsp">Update Employee</a> -->
</body>
</html>
addEmp.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Add Employee</h1>
<form:form action = "/add" modelAttribute = "employee">
<table>
<tr>
<td>Name :</td>
<td><form:input path="empname"/>
</tr>
<tr>
<td>Id :</td>
<td><form:input path="empid"/>
</tr>
<tr>
<td>Designation :</td>
<td><form:input path="designation"/>
</tr>
<tr>
<td>Department :</td>
<td><form:input path="department"/>
</tr>
<tr>
<td><input type="submit" name="Submit"/></td>
</tr>
</table>
</form:form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>AD Store</display-name>
<servlet>
<servlet-name>adstore</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>adstore</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
adstore-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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.adstore" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
EmployeeController.java
package com.adstore.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.adstore.bean.Employee;
import com.adstore.dao.EmployeeDAO;
@Controller
public class EmployeeController {
private EmployeeDAO dao;
@RequestMapping(value="/add")
public ModelAndView saveEmployee(@ModelAttribute("employee") Employee emp) {
System.out.println("In saveEmployee");
dao.saveEmp(emp);
return new ModelAndView("viewEmployee","command",new Employee());
}
}
EmployeeDAO.java
package com.adstore.dao;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import com.adstore.bean.Employee;
public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public boolean saveEmp(final Employee emp) {
boolean result = false;
String query = "INSERT INTO ADSTORE VALUES(?,?,?,?)";
result = jdbcTemplate.execute(query, new PreparedStatementCallback<Boolean>() {
public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
ps.setString(1, emp.getEmpName());
ps.setInt(2, emp.getEmpId());
ps.setString(3, emp.getDesignation());
ps.setString(4, emp.getDepartment());
return ps.execute();
}
});
return result;
}
}
Error Image::
No application context is found because the request is not being handled by the DispatcherServlet as you are going directly to the JSP page. Hence there is no required Spring infrastructure to process the form:form tags.
You should access the page using the url defined in the @Controller using @RequestMapping or its variants. Its not good practice to put the JSP pages in a location which makes them accessible directly by clients. Instead you should hide them in the /WEB-INF/ directory
According to your view resolver the jsp pages should reside in the /WEB-INF/views folder. Move the JSPs to this folder.