你知道目前最流行的SpringMVC框架吗?如何搭建呢?

Spring MVC 是 Spring 家族中的一个 web 成员, 它是一种基于 Java 的实现了 Web MVC 设计思想的请求驱动类型的轻量级 Web 框架,即使用了 MVC 架构模式的思想,将 web 层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring MVC 也是要简化我们日常 Web 开发的。

Spring MVC 是服务到工作者思想的实现。前端控制器是 DispatcherServlet;应用控制器拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;支持本地化/国际化(Locale)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了强大的约定大于配置(惯例优先原则)的契约式编程支持。

SpringMVC 搭建的方式

开发环境搭建

新建 Maven webApp

Springmvc 环境 jar 包依赖

配置 web.xml (前端控制器配置)

servlet-context.xml 配置

页面控制器的编写

添加视图页面

启动 jetty 服务器

案例实操

开发环境搭建

Eclipse + jdk1.7 + maven + Jetty

新建 Maven webApp

建立 springmvc01 工程并调整 web 环境。

Springmvc 环境 jar 包依赖

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.xxx

springmvc01

war

0.0.1-SNAPSHOT

springmvc01 Maven Webapp

http://maven.apache.org

junit

junit

4.12

test

org.springframework

spring-web

4.3.2.RELEASE

org.springframework

spring-webmvc

4.3.2.RELEASE

javax.servlet

javax.servlet-api

3.0.1

springmvc01

src/main/resources

org.apache.maven.plugins

maven-compiler-plugin

2.3.2

1.7

1.7

UTF-8

org.mortbay.jetty

maven-jetty-plugin

6.1.25

10

/springmvc01

配置 web.xml (前端控制器配置)

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

contextConfigLocation

classpath:*.xml

    org.springframework.web.context.ContextLoaderListener

char encoding filter

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

springMvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:servlet-context.xml

1

springMvc

/

要想启动我们的 springMvc 环境,目前对于 mvc 框架的配置还未进行。以上在 web.xml 中引用了 servlet-context.xml 文件。

servlet-context.xml 配置

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/mvc  

http://www.springframework.org/schema/mvc/spring-mvc.xsd  

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.xsd

http://www.springframework.org/schema/aop  

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx  

http://www.springframework.org/schema/tx/spring-tx.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView"/>

如果返回乱码:配置消息转换器

   

页面控制器的编写

/**

* 采用注解扫描形式

*/

@Controller

publicclassHelloController{

/**

* 请求映射地址 /hello

* @return

*/

@RequestMapping("/hello")

publicModelAndViewhello(){

ModelAndViewmv=newModelAndView();

mv.addObject("hello","hello spring mvc");

mv.setViewName("hello");

returnmv;

   }

}

添加视图页面

在 WEB-INF 下新建 jsp 文件夹 ,并在文件加下新建 hello.jsp

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

Stringpath=request.getContextPath();

StringbasePath=

request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

">

My JSP 'hello.jsp' starting page

    ${hello}

启动 jetty 服务器

选中项目右键 → run as → maven build → goals 输入框中输入 jetty:run 即可启动服务器。

如果这里启动成功,浏览器(最好用强大的 chrome 或者火狐浏览器,程序员的最爱!!!)访问地址 http://localhost:8080/springmvc01/hello

最终效果如下:

至此,springmvc 环境搭建完毕。

扩展

Spring MVC 能帮我们做什么

让我们能非常简单的设计出干净的 Web 层;

进行更简洁的 Web 层的开发;

天生与 Spring 框架集成(如 IoC 容器、AOP 等);

提供强大的约定大于配置的契约式编程支持;

能简单的进行 Web 层的单元测试;

支持灵活的 URL 到页面控制器的映射;

非常容易与其他视图技术集成,如 Velocity、FreeMarker 等等,因为模型 数据不放在特定的 API 里,而是放在一个 Model 里(Map 数据结构实现,因此很容易被其他框架使用);

非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的 API;

支持灵活的本地化等解析;

更加简单的异常处理;

对静态资源的支持;

支持 Restful 风格。    

你可能感兴趣的:(你知道目前最流行的SpringMVC框架吗?如何搭建呢?)