• Spring MVC helloWorld中遇到的问题及解决办法


    1、java.io.FileNotFoundException: Could not open ServletContext resource
    不能加载ServletContext的用法是配置到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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 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">
      <display-name>SpringMVCLesson</display-name>
        
        <servlet>
            <servlet-name>SpringMVCLesson</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springservlet-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 -->
        </servlet>
        <!-- Spring MVC配置文件结束 -->
        
        <servlet-mapping>
            <servlet-name>SpringMVCLesson</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
    </web-app>

    解决办法:
    更改xml名字为 {ServletContext}-servlet.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             version="2.4" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <!--<start id="servlet_DispatcherServlet"/>-->
        <servlet>
            <servlet-name>SpringMVCLesson</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!--<end id="servlet_DispatcherServlet"/>-->
    
        <servlet-mapping>
            <servlet-name>SpringMVCLesson</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>

    位置仍为/WEB-INF/下

    2、org.springframework.web.servlet.DispatcherServlet noHandlerFound
    解决办法:
    SpringMVCLesson-servlet.xml增加

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    这个工程中还有个特殊问题是Source Folder设置错误导致java没有生成class文件

    3、其它xml:xsi没有配置正确导致配置的元素没有被识别而报错

    <?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:util="http://www.springframework.org/schema/util"
     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.0.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
       <!--<start id="spring_component_scan" />-->
       <context:component-scan base-package="com.demo.web.controllers" />
       <!--<end id="spring_component_scan" />-->
    
       <!--<start id="mvc_annotatedcontrollers" />-->
       <mvc:annotation-driven/>
       <!--<end id="mvc_annotatedcontrollers" />-->
    
       <!--<start id="mvc_resources"/>--> 
       <mvc:resources mapping="/resources/**" location="/resources/" />
       <!--<end id="mvc_resources"/>--> 
    
    </beans>

    4、Spring中CoC(Convention over Configration)约定优于配置的理解又加深了

  • 相关阅读:
    挂载硬盘,提示 mount: unknown filesystem type 'LVM2_member'的解决方案
    mongo3.4 配置文件 注意事项
    Rsync 传输不需要输入密码
    Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
    linux 安装 mongo
    mysql GTID主从复制(主库在线,添加新丛库)
    计算机网络原理精讲第四章--网络层
    Chrome浏览器商店安装的插件保存到本地
    计算机网络原理精讲第三章--链路层
    计算机网络原理精讲第二章--物理层
  • 原文地址:https://www.cnblogs.com/softidea/p/4458876.html
Copyright © 2020-2023  润新知