您好,欢迎访问一九零五行业门户网

SpringMVC详解

本系列教程我们将详细的对springmvc进行介绍,相信你在学完本系列教程后,一定能在实际开发中运用自如。
1、什么是 springmvc ?在介绍什么是 springmvc 之前,我们先看看 spring 的基本架构。如下图:
我们可以看到,在 spring 的基本架构中,红色圈起来的 spring web mvc ,也就是本系列的主角 springmvc,它是属于spring基本架构里面的一个组成部分,属于springframework的后续产品,已经融合在spring web flow里面,所以我们在后期和 spring 进行整合的时候,几乎不需要别的什么配置。
springmvc 是类似于 struts2 的一个 mvc 框架,在实际开发中,接收浏览器的请求响应,对数据进行处理,然后返回页面进行显示,但是上手难度却比 struts2 简单多了。而且由于 struts2 所暴露出来的安全问题,springmvc 已经成为了大多数企业优先选择的框架。
那么多的不说,我们直接通过一个实例来看看 springmvc 的魔力。
ps:本篇博客源码下载链接:http://pan.baidu.com/s/1c1oji5e 密码:9hnc
2、创建 web 工程,并导入相应的 jar 包。
这里我们加入了 spring 3.2 的所有 jar 包,正好也佐证了上面所说的 springmvc 是 spring 架构的一部分,注意:一定要包括红色椭圆圈起来的 spring-webmvc-3.2.0.release.jar
3、新建 springmvc 全局配置文件在 src 目录下新建 springmvc.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: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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> </beans>
4、在 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>springmvc_01</display-name> <!-- 配置前端控制器dispatcherservlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <!-- 对应上一步创建全局配置文件的文件名以及目录 --> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
5、编写处理器 handler
package com.ys.controller; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.web.servlet.modelandview; import org.springframework.web.servlet.mvc.controller; public class hellocontroller implements controller{ @override public modelandview handlerequest(httpservletrequest request, httpservletresponse response) throws exception { modelandview modelview = new modelandview(); //类似于 request.setattribute() modelview.addobject("name","张三"); modelview.setviewname("/web-inf/view/index.jsp"); return modelview; } }
6、在 springmvc.xml 文件中配置 handler,处理器映射器,处理器适配器,以及试图解析器在 springmvc.xml 文件中添加如下代码:
<!-- 配置handler --> <bean name="/hello.do" class="com.ys.controller.hellocontroller" /> <!-- 配置处理器映射器 将bean的name作为url进行查找,需要在配置handler时指定bean name(就是url)--> <bean class="org.springframework.web.servlet.handler.beannameurlhandlermapping" /> <!-- 配置处理器适配器,所有适配器都得实现 handleradapter接口 --> <bean class="org.springframework.web.servlet.mvc.simplecontrollerhandleradapter" /> <!-- 配置视图解析器 进行jsp解析,默认使用jstl标签,classpath下得有jstl的包--> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver" />
7、在 /web-inf/view 目录下创建 index.jsp 文件
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> hello:${name} </body> </html>
8、在浏览器输入 http://localhost:8080/springmvc_01/hello.do
跟着上面的步骤,相信你已经搭建了一个 springmvc 的简单实例,那么为什么要这么写呢?请看下一篇博客分解!!!
以上就是springmvc详解的详细内容。
其它类似信息

推荐信息