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
- SpringMVC入门-第二节:配置
- 第二章、SpringMVC学习-入门
- 第二章---SpringMVC----Spring入门
- SpringMVC之入门配置
- springMvc入门配置Validation
- 关于SpringMVC入门配置
- SpringMVC入门 配置
- Springmvc的入门---配置
- SpringMVC入门,配置过程
- SpringMVC入门配置和试用
- springmvc入门-前端控制器配置
- springmvc入门与基本配置
- MyBatis第二课:入门配置
- 第二章 Spring MVC入门 —— 跟开涛学SpringMVC
- 第二章 Spring MVC入门 —— 跟开涛学SpringMVC
- 第二章 Spring MVC入门 —— 跟开涛学SpringMVC
- 第二章 Spring MVC入门 —— 跟凯哥学springMVC
- 第二章 Spring MVC入门 —— 跟开涛学SpringMVC
- Epoll详解及源码分析
- 山东省省外院校毕业生注册【山东省高校毕业生就业信息网】须知
- XML解析之PULL解析+SAX解析+DOM解析
- Golang 常用的一些基本操作
- 不属于底部导航栏上的Fragment 替换底部的Fragment
- SpringMVC入门-第二节:配置
- 很简单的图片自动轮播,点击焦点就显示点击的那个
- 如何自学Android编程
- Python入门自学笔记20160628
- day04_filtter() 和map函数
- Android 发送短信
- django日志 2016.06.29回顾
- Android 如何搭建一个局域网 Web 服务器
- 交换两个变量的值,不使用第三个变量的四种法方