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

时间:2022-06-20 15:29:01

 

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)约定优于配置的理解又加深了