您的位置:首页 > 编程语言 > Java开发

SpringMVC系列之(一) 入门实例

2015-05-11 15:09 369 查看
Spring MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了。Spring MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也很优秀.



Struts2 VS Spring MVC



Struts2 特点

也是比较优秀的MVC构架,优点非常多比如良好的结构。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降。Struts2的多层拦截器、多实例action性能都很好。

Spring3 MVC的优点

1、Spring3 MVC的学习难度小于Struts2,Struts2用不上的多余功能太多。呵呵,当然这不是决定因素。

2、Spring3 MVC很容易就可以写出性能优秀的程序,Struts2要处处小心才可以写出性能优秀的程序(指MVC部分)

3、Spring3 MVC的灵活是你无法想像的,Spring的扩展性有口皆碑,Spring3 MVC当然也不会落后,不会因使用了MVC框架而感到有任何的限制。

先来学习MVC :Model-View-Control

框架性质的C 层要完成的主要工作:封装web 请求为一个数据对象、调用业务逻辑层来处理数据对象、返回处理数据结果及相应的视图给用户。

Spring C 层框架的核心是 DispatcherServlet,它的作用是将请求分发给不同的后端处理器,也即 使用了一种被称为Front Controller 的模式(后面对此模式有简要说明)。 Spring 的C 层框架使用了后端控制器来、映射处理器和视图解析器来共同完成C 层框架的主要工作。并且spring 的C 层框架还真正地把业务层处理的数据结果和相应的视图拼成一个对象,即我们后面会经常用到的ModelAndView 对象。

Spring MVC对于现在较成熟的Model-View-Control框架而言,其解决的主要问题无外乎下面几部分:

1》将web页面中的输入元素封装为一个(请求)数据对象。

2》根据请求的不同,调度相应的逻辑处理单元,并将(请求)数据对象作为参数传入。

3》逻辑处理单元完成运算后,返回一个结果数据对象。

4》将结果数据对象中的数据与预先设计的表现层相融合并展现给用户。



一、入门实例

1. 搭建环境



在spring 的官方API 文档中,给出所有包的作用概述,现列举常用的包及相关作用:

org.springframework.aop-3.0.5.RELEASE.jar :与Aop 编程相关的包

org.springframework.beans-3.0.5.RELEASE.jar :提供了简捷操作bean 的接口

org.springframework.context-3.0.5.RELEASE.jar :构建在beans 包基础上,用来处理资源文件及国际化。

org.springframework.core-3.0.5.RELEASE.jar :spring 核心包

org.springframework.web-3.0.5.RELEASE.jar :web 核心包,提供了web 层接口

org.springframework.web.servlet-3.0.5.RELEASE.jar :web 层的一个具体实现包,DispatcherServlet也位于此包中。

后文全部在spring3.0 版本中进行,为了方便,建议在搭建环境中导入spring3.0 的所有jar 包(所有jar 包位于dist 目录下)。

2. 编写 欢迎看我的博客--李社 河 实例

建立名为lishehespringMVC1 ,并导入上面列出的jar 包。



Spring可以通过指定classpath*:与classpath:前缀加路径的方式从classpath加载文件,如bean的定义文件.

classpath*:的出现是为了从多个jar文件中加载相同的文件.
classpath:只能加载找到的第一个文件.

编写web.xml 配置文件,启用Spring的请求接收器DispatcherServlet

代码如下:

[html] view
plaincopyprint?

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>lishehespringMVC1</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>

<servlet>

<servlet-name>springMVC</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]- servlet.xml,如spring-servlet.xml

-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:config/spring-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>

</servlet-mapping>

</web-app>

3:完成编写HelloWorldController.java类并实现Controller接口,一般来说呢框架是越轻量级越好,如果新建一个类还需要去实现别的接口或者继承别的类,会使得该类耦合很大.

[java] view
plaincopyprint?

package com.tgb.web.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 HelloWorldController implements Controller {

@Override

public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)

throws Exception {

System.out.print("---hellotigaoban---");

String hello="lishehe hello";

//返回到前台页面,根据结果的字符串,到相应页面

return new ModelAndView("/welcome","result",hello);

}

//一种是注解,一种是xml,现在来看注解大大的提高了开发效率

}

spring-servlet.xml

[html] view
plaincopyprint?

<bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

新建前台页面welcome.jsp

${} JSTL表达式

[plain] view
plaincopyprint?

<%@ 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>

欢迎看我的博客--李社河

<br/>

<h>传递数据</h>

${result}

</body>

</html>

运行后效果:



总结 

掌握以上这些Spring MVC就已经有了很好的基础了,几乎可应对与任何开发,在熟练掌握这些后,便可更深层次的灵活运用的技术,如多种视图技术,例如 Jsp、Velocity、Tiles、iText 和 POI。Spring MVC框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。

接下来详解springMVC和Struts的区别联系,请期待!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: