SpringMVC入门-第二节:配置

来源:互联网 发布:农村淘宝在哪里取消 编辑:程序博客网 时间:2024/06/03 01:12

一、Spring MVC环境搭建

1. jar包引入

2.SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,Handler处理以后再返回相应的视图(View)和模型(Model),返回的视图和模型都可以不指定,即可以只返回Model或只返回View或都不返回。

DispatcherServlet是继承自HttpServlet的,既然SpringMVC是基于DispatcherServlet的,那么我们先来配置一下DispatcherServlet,好让它能够管理我们希望它管理的内容。HttpServlet是在web.xml文件中声明的。

我的项目叫FSH04,下面展示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>FSH04</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><!--集成spring配置 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 指定Spring Bean的配置文件所在目录 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/applicationContext.xml</param-value></context-param><!-- Spring MVC配置 -->  <servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--可以自定义springAnnotion-servlet.xml配置文件的位置和名称 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/springAnnotion-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern><!-- 此处也可以*.do来表示 --></servlet-mapping><!-- post请求需要配置的编码格式UTF-8 --><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

springAnnotion-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd            http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd            http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd">        <!-- 配置注解扫描包 -->    <context:component-scan base-package="com.tgb.web.controller.annotation"/>     <context:component-scan base-package="com.tgb.web.controller.upload"/>       <!-- 开启注解 -->    <mvc:annotation-driven/><!-- 静态资源访问 --><!-- 此方法是web.xml里面配置为   <url-pattern>/</url-pattern> 如果是   <url-pattern>*.do</url-pattern> 是不需要进行下面的配置的--> <mvc:resources location="/img/" mapping="/img/**"/><mvc:resources location="/js/" mapping="/js/**"/><!-- 配置渲染器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--结果视图的前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!--结果视图的后缀 --><property name="suffix" value=".jsp" /></bean> <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">         <property name="defaultEncoding" value="UTF-8"/>          <!-- 指定所上传文件的总大小不能超过1048576000KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->          <property name="maxUploadSize" value="1048576000"/>         <!-- 缓存 -->         <property name="maxInMemorySize" value="40960"/>    </bean>  </beans>







0 0
原创粉丝点击