SpringMVC(二)配置文件

来源:互联网 发布:steam mac 编辑:程序博客网 时间:2024/06/18 10:52

了解SpringMVC后,接下来我们学习它的配置文件:

1、新建WEB项目:
这里写图片描述
2、在WebContent-WEB-INF下的lib文件里导入jar包:
由于SpringMVC是基于表现层的框架,所以我们建的是Web项目,这里也需要导入servlet.jar
这里写图片描述
3、配置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">  <display-name>springMVC01</display-name>  <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>  <!-- 第一部分 -->  <!-- 配置SpringMVC前端控制器 -->  <servlet>    <servlet-name>springMVC</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- 启动WEB容器自动加载前端控制器 -->    <load-on-startup>1</load-on-startup>  </servlet>  <!-- SpringMVC前端控制器 映射 -->  <servlet-mapping>    <servlet-name>springMVC</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>   <!-- 第二部分 -->  <!-- 配置字符集编码过滤器 -->  <filter>    <filter-name>encodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <!-- 设置编码格式 -->    <init-param>        <param-name>encoding</param-name>        <param-value>UTF-8</param-value>    </init-param>  </filter>  <!-- 配置字符集编码过滤器映射 -->  <filter-mapping>    <filter-name>encodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <!-- 第三部分 -->  <!-- 配置加载springMVC框架环境配置文件 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/springMVC-servlet.xml</param-value>  </context-param>  <!-- 监听器 -->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener></web-app>

4、创建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:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <!-- 配置处理器映射 (必配项) -->    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    <!-- 配置页面处理器适配器 (必配项) -->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />    <!-- 配置视图解析器(必配项) -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />        <!-- 设置视图前缀 -->        <property name="prefix" value=""    />        <!-- 设置视图后缀 -->        <property name="suffix" value=".jsp"/>    </bean>    <!-- 配置访问静态资源 (必配项)-->    <mvc:default-servlet-handler/></beans>

基本配置就配置完成了!

原创粉丝点击