

springmvc使用spring表单标签库报错java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?
form:form删掉就可以运行,没有该标签的其他jsp都可以运行


springmvc使用spring表单标签库报错java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?
form:form删掉就可以运行,没有该标签的其他jsp都可以运行
关注引自强大而免费的微信小程序:皆我百晓生。
问题原因分析:
该异常是因为没有找到WebApplicationContext而导致的,可能是因为没有配置DispatcherServlet或者没有注册ContextLoaderListener。
解决方法:
1.确保已经在web.xml中配置了DispatcherServlet和ContextLoaderListener。
在web.xml中添加如下配置:
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.确保已经在DispatcherServlet对应的配置文件中配置了Spring MVC的相关配置。
创建一个名为springmvc-config.xml的文件,并在其中添加如下配置:
<!-- 开启Spring MVC注解驱动 -->
<mvc:annotation-driven/>
<!-- 配置ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
3.确保在JSP页面中正确引入了Spring的标签库。
在JSP页面的<%@ page %>标签中添加如下语句引入Spring的标签库:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
完整的JSP页面代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>register</title>
</head>
<body>
<form:form modelAttribute="user" method="post" action="user/save">
<!-- 表单内容 -->
</form:form>
</body>
</html>
参考代码:
1.web.xml配置示例:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet的映射路径 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.springmvc-config.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启Spring MVC注解驱动 -->
<mvc:annotation-driven/>
<!-- 配置ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
希望以上解决方案对您有帮助!