SpringMVC简介及入门小程序

SpringMVC概述

SpringMVC为展示层(显示层和控制层)提供了基于MVC设计里面的优秀的Web框架,是目前最流行的MVC框架,没有之一。

Spring3.0之后全面超越了Struts2(Action),称为了最优秀的MVC框架,SpringMVC是通过一套MVC的注解,让普通的JavaBean称为请求控制器(请求处理器,Handler),而无需事先任何的Spring接口,SpringMVC是支持RestFul风格的URL请求(目前请求风格主流有两种:RPC和RestFul,RestFul更适合互联网版的程序开发),采用了松散耦合可插拔组件的结构,比其他的MVC框架更具有扩展性和灵活性。

SpringMVC HelloWorld程序

SpringMVC是MVC框架,肯定是做java EE程序的,既然是和Spring同一套jar包,开发模式肯定和Spring有非常大的相似之处,也需要我们对已经学习过的SpringMVC有所掌握。

SpringMVC HelloWorld程序开发步骤:

  • 新建一个动态web工程
  • 加入jar包(类库)
  • 加入SpringMVC的配置文件(Spring 配置文件,但是需要对动态web工程的web.xml进行配置)
  • 编写一个类,并且标识为一个请求处理器
  • 编写视图(jsp,html)

①:创建动态web工程,注意一定要勾选 web.xml文件的创建

②:加入类库

spring-aop-4.0.0.RELEASE.jar

spring-aspects-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

commons-logging-1.1.3.jar

③:编写SpringMVC的配置文件(就是一个Spring的配置文件)

<?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"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

​		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.wanbangee"></context:component-scan>

</beans>

④:将SpringMVC配置到web.xml中,表示不再是由main方法去启动,而是在tomcat启动的时候就加载Spring的IOC容器。

- SpringMVC需要在web.xml中配置一个Servlet(DispatcherServlet),而这个Servlet是由SpringMVC提供的,可以处理指定的请求,一般请求下(通过配置),我们会将所有的请求都交给这个SpringMVC的Servlet处理,而这个Servlet就具体负责去寻找调用的是哪个Handler。

- 2012之前流行的Struts2,人家使用的时候就不是配置一个Servlet而是配置一个Filter(过滤器)

<?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_3_1.xsd" id="WebApp_ID" version="3.1">

 <display-name>SpringMVC01</display-name>

 <welcome-file-list>

  <welcome-file>index.html</welcome-file>

 </welcome-file-list><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 

​			init-param : 表示配置初始化参数

​				param-name : 参数名称 contextConfigLocation 表示上下文配置地址,实际上表示SpringMVC配置文件的具体地址

​				param-value : 参数值,location 表示位置

​		 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:SpringMVC.xml</param-value></init-param><!-- 

​			load-on-startup : 表示是否在web服务器启动的时候就加载这个Servlet

​				1 : 表示加载

​				0 : 表示不加载 ,会在Serlvet第一次被请求之前来加载

​		--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><!-- 

​			一般情况下,我们会对url-pattern进行分类的区别:

​				- 第一种,使用/表示处理所有请求,存在问题,比如我要访问的是一张图片(静态资源),也要经过DispatcherServlet,在我们看来是不合适的,但是却是SpringMVC推荐的方式,因为SpringMVC对静态资源的访问提供了另外一种形式

​				- 第二种,使用/*.do ,表示处理所有的带.do 的请求地址

​				- 第三种,使用/*.action ,表示处理所有的带.action 的请求地址

​			第二种和第三种,是Struts2推荐的方式,第一种是SpringMVC推荐的方式,优雅的SpringMVC不希望请求地址带.do 或者.action

​		 --><url-pattern>/</url-pattern></servlet-mapping>

 

</web-app>

⑤:编写一个请求处理器

package com.wanbangee.controller.helloworld;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

@Controller //表示是一个控制器

public class HelloWorld {/**

​	 * 	sayHello 我们称之为请求处理器的 目标方法,每一个带有@RequestMapping注解的方法都叫做目标方法,能够处理一个URL请求

​	 * 	@RequestMapping用来映射目标方法请求URL

​	 * @return

​	 */@RequestMapping("/helloworld")public String sayHello() {

​		System.out.println("Hello SpringMVC");return "success";}

}

现在浏览器已经能够通过http://localhost:8080/SpringMVC01/helloworld 访问到请求处理器的目标方法了,但是出现了404,原因在于我们没有配置视图解析器,表示目标方法返回的字符串SpringMVC没办法解析。

⑥:配置视图解析器:在SpringMVC的配置文件中,配置视图解析器

<?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"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

​		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自动扫描的包 --><context:component-scan base-package="com.wanbangee"></context:component-scan>

​	

​	<!-- 

​		配置视图解析器:将目标方法返回的逻辑视图,解析成一个实际的物理视图

​			属性注入:

​			- prefix:配置视图解析的前缀 

​			- suffix:配置视图解析的后缀

​			

​			比如视图解析器的前缀配置为/jsp/view/

​			    视图解析器的后缀配置为.jsp

​			    请求处理器的目标方法

​				@RequestMapping("/helloworld")

​				public String sayHello() {

​					System.out.println("Hello SpringMVC");

​					return "success";

​				}

​			那么请求处理器目标方法返回的字符串success 就会被解析为 /jsp/view/success.jsp 并且进行转发

​	 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/jsp/view/"></property><property name="suffix" value=".jsp"></property></bean>

</beans>
小结

​ SpringMVC是在Spring的基础上发展而来的,在Spring3.0以后全面的超越了Struct2框架,称为最优秀的MVC框架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值