【发布时间】:2018-02-04 09:03:22
【问题描述】:
我正在尝试构建一个简单的应用程序来使用 Spring MVC 创建和更新少量记录。但由于我面临的问题是我的弹簧表单标签有一些错误,并且我无法从 2 天开始弄清楚,因此无法继续进行。错误说:
java.lang.IllegalStateException: No WebApplicationContext found: not in an DispatcherServlet request and no ContextLoaderListener register?
还有一些疑问:
为什么我在使用 dispatcherServlet 时需要 ContextLoaderListener。
每个 DispatcherServlet 都有自己的或实例化 WebApplicationContext。那为什么错误说它不可用?
我在概念上或程序上遗漏了什么?
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;
}
}
错误图片::
【问题讨论】:
-
包含 EmployeeController 的代码
-
我已经更新了上面的代码并添加了 2 个类。请检查。谢谢。
标签: spring jsp spring-mvc spring-form