Spring MVC 简单介绍
来源:互联网 发布:蔡健雅 知乎 编辑:程序博客网 时间:2024/05/16 00:40
Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
Spring 的 Web MVC 框架是围绕 DispatcherServlet 设计的,它把请求分派给处理程序,同时带有可配置的处理程序映射、视图解析、本地语言、主题解析以及上载文件支持。默认的处理程序是非常简单的 Controller 接口,只有一个方法 ModelAndView handleRequest(request, response)。
Spring 提供了一个控制器层次结构,可以派生子类。如果应用程序需要处理用户输入表单,那么可以继承 AbstractFormController。如果需要把多页输入处理到一个表单,那么可以继承AbstractWizardFormController。
Spring MVC对于现在较成熟的Model-View-Control框架而言,其解决的主要问题无外乎下面几部分:
1》将web页面中的输入元素封装为一个(请求)数据对象。
2》根据请求的不同,调度相应的逻辑处理单元,并将(请求)数据对象作为参数传入。
3》逻辑处理单元完成运算后,返回一个结果数据对象。
4》将结果数据对象中的数据与预先设计的表现层相融合并展现给用户。
开发步骤:首先新建web Project项目:MySpringMvc
1.加载项目所需要的jar包;
spring.jar -------------------------这个在spring2.5.6资源包的dist下面
spring-webmvc.jar---------------这个在spring2.5.6资源包的dist/module下面2.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>-<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" version="2.5"><display-name/><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/spring-servlet.xml</param-value></context-param><listener> <!-- Spring配置 --><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet> <!-- servlet就是一个运行在服务器端的类,专门处理各种请求,当需要的时候才会调用。 --><servlet-name>springMVC</servlet-name> 注册servlet的名字<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 指向我们要注册的servlet 的类地址。<init-param> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --><param-name>contextConfigLocation</param-name><param-value>classpath*:config/spring-servlet.xml</param-value></init-param></servlet><!--<span> applicationContext.xml文件代表示应用程序服务的配置和 bean 配置。如果想装入多个配置文件,可以在 </span><span></span><span> <span class="tag"><</span><span class="tag-name">param-value</span><span class="tag">></span><span>标记中用逗号作分隔符。 </span></span><span><span></span></span><span> **springmvc配置文件与spring配置的 servlet名称 有关 </span>*****通常springmvc配置文件名称结构为:[servlet-name]-servlet.xml,
注意:如果你没有指定init-param里面contextCofigLocation的值中对应的XML文件的话
(也就是applicationContext全局配置文件没有配置在web.xml中的话),那么
在springmvc中的配置文件就应该是 /WEB-INF/dipatcher-servlet.xml 这样的文件,
如果配置了applicaitonContext.xml这样的spring全局配置文件,如本配置那么就必须建立对应的配置文件,
位置并不固定
-->
<servlet-mapping><servlet-name>springMVC</servlet-name> 与上面的name一致<url-pattern>/</url-pattern> 表示:如果对于某个请求,没有找到匹配的Servlet,那么将使用web应用程序的默认Servlet来处理例子:<url-pattern>/admin/*</url-pattern><p> 如果没有精确匹配,那么对/admin/路径下的资源的所有请求将由 映射了上述URL样式 的Servle来处理</p></servlet-mapping><!-- 经过拦截,强制设置成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> <!-- encoding filter for jsp page --> <filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern> 拦截URl请求的</filter-mapping> </web-app>
定义自己的Spring配置文件,在classpath目录下的config文件夹下面。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "><!--<context:component-scan/>的作用是让Bean定义注解工作起来,也就是xia述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 值得注意的是<context:component-scan/>不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。--><context:component-scan base-package="com"></context:component-scan><!-- 隐式的向Spring容器注册AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor.注册这4个bean处理器主要的作用是为了你的系统能够识别相应的注解。--><mvc:annotation-driven /><!--对 模型视图名称 (可能是类;ModelAndView) 的解析,即wei 模型视图名称 添加前后缀--> <bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url"value="jdbc:mysql://localhost:3306/testdb"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean "><property name="dataSource"><ref bean="dataSource" /></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop></props></property><!-- Spring 注解Hibernate实体类的方法 --> <property name="packagesToScan"> <list> <!-- 扫描实体类,也就是平时所说的model --> <value>com.entity</value> </list> </property> </bean> </beans>
- Spring MVC 简单介绍
- Spring MVC简单介绍
- 基于注解的Spring MVC简单介绍)
- Spring MVC——DispatcherServlet简单介绍
- spring-mvc-web.xml配置文件简单介绍
- spring mvc 3介绍
- spring MVC框架介绍
- Spring MVC 背景介绍
- spring mvc介绍
- Spring MVC介绍
- Spring MVC介绍
- Spring MVC配置介绍
- 1、Spring MVC 介绍
- Spring MVC配置介绍
- Spring MVC配置介绍
- Spring MVC入门介绍
- Spring MVC框架介绍
- Spring MVC Controller介绍
- c# socket 判断端口是否被占用
- android 对话框总结
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系
- 内置传感器---智能手机(资料)
- Java学习的30个目标以及系统架构师推荐的书
- Spring MVC 简单介绍
- HTML5 2D 游戏开发(二): 图形和动画
- 『IOS』symbol(s) not found for architexture i386 路径错误
- IP层实现4-raw socket 接收数据
- fragment点击事件击穿问题
- BZOJ 1975 SDOI 2010 魔法猪学院 A*求K短路
- Xcode 6 正式版如何创建一个Empty Application
- 使用typedef语句定义数组类型
- 过滤器:过滤器在农业发展中的重要地位