m0_61273446 2023-11-15 16:30 采纳率: 100%
浏览 9
已结题

springmvc使用spring表单标签库报错java.lang.IllegalStateException

img

img

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

  • 写回答

7条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-11-15 16:30
    关注

    引自强大而免费的微信小程序:皆我百晓生

    问题原因分析

    该异常是因为没有找到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>
    

    希望以上解决方案对您有帮助!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 11月23日
  • 已采纳回答 11月15日
  • 创建了问题 11月15日