注意点一:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
SpringMVC10_20</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--这里实际的路径是在src同级的config文件夹下的config.xml,千万不要写成<param-value>classpath:/config/config.xml</param-value>-->
<param-value>classpath:config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
注意点二:
controller里面的最后的 return ModelAndView; 不要忘记了
注意点三:
启动tomcat后,控制台报错:WEB-INF/lib/servlet-api.jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
保证WEB-INF/lib跟tomcat的common/lib下的JAR包没有重复的包即可!
将项目的web-inf/lib下的jsp-api.jar删掉!ok
注意点四:
用注解方式的话web.xml处修改成,注意这个只能注解用,静态文件用/的话,会发生错误,静态文件的话,用*.html,*.action也可以,但要跟dispatch那一致
<span style="white-space:pre"> </span><servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<span style="background-color: rgb(255, 102, 102);"><url-pattern>/</url-pattern></span>
</servlet-mapping>
dipather处的xml改为,要将类添加进去
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.lee.controller" />