创建SpringMvc项目使用的配置,备注一下
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>MySpringMvcDemo</display-name> <!-- 配置一个servlet --> <!-- servlet的配置 --> <servlet> <!-- servlet的内部名称,自定义。尽量有意义 --> <servlet-name>springmvc</servlet-name> <!-- servlet的类全名: 包名+简单类名 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 第一种:默认加载 /WEB-INF/[servlet-name]-servlet.xml ,使用这种第二种就可以删除--> <!-- 第二种:指定路径 --> <init-param> <param-name>contextConfigLocation</param-name> <!-- 设置SpringMVC配置文件的位置,注意该位置默认是src文件夹下 --> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- servlet 启动优先,一般填写1就可以了 --> <load-on-startup>1</load-on-startup> </servlet> <!-- servlet的映射配置 --> <servlet-mapping> <!-- servlet的内部名称,一定要和上面的内部名称保持一致!! --> <servlet-name>springmvc</servlet-name> <!-- servlet的映射路径(访问servlet的名称) ,据说是拦截所有静态文件的请求,如.js;.css文件等--> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置默认页面,注意这个页面是直接在WebContent文件夹下的,如果是在WebContent下的view文件夹下就要配置成view\index.jsp,但是默认页面不要放在WEB-INF内 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc-servlet.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 注意上面的也可以写成spring-context-4.3.xsd,如果不写则默认是用当前的版本 --> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.demo.springmvc" /> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>