java Spring MVC4环境搭建实例详解(步骤)

网友投稿 240 2023-04-11


java Spring MVC4环境搭建实例详解(步骤)

spring WEB MVC框架提供了一个MVC(model-view-controller)模型-视图-控制器的结构和组件,利用它可以开发更灵活、松耦合的web应用。MVC模式使得整个服务应用的各部分(控制逻辑、业务逻辑、UI界面展示)分离开来,使它们之间的耦合性更低。

Model

模型层封装了整个应用程序的数据对象并且将会持久化POJO

View

视图层负责渲染模型层的数据,一般最终使用HTML展示到用户浏览器或移动设备终端

Controller

控制层负责处理用户的请求、构建合适的模型数据并将其传输到视图层进行渲染

SpringMVC核心-DispatcherServlet分发器

SpringMVC框架是设计用来处理所有的HTTP请求和响应的,它的核心是围绕着一个分发器DispatcherServlet。在Spring Web MVC DispatcherServlet的请求处理流程如下图所示:

以下是对应一个HTTP请求时分发器的处理流程:

1.接收到一个Http请求后,DispatcherServlet 告诉映射处理器HandlerMapping 去调用相应的Controller(控制器)。

2.然后Controller控制器根据接收到的的请求调用相应的Service服务方法,基于请求所使用的GET或PUT等其它请求方式。Service方法将会根据业务逻辑设置模型数据并且返回一个视图给DispatcherServlet分发器。

3.DispatcherServlet 分发器将会请求视图解析器ViewResolver去处理找到该请求对应的已经定义的视图view。

4.一旦视图确定了,DispatcherServlet 会将模型数据传输给该视图view,最终渲染到用户浏览设备上。

上述提到的所有组件:映射处理器HandlerMapping, 控制器Controller 和视图解析器ViewResolver都是WebApplicationContext 的组成部分。WebApplicationContext 扩展了ApplicationContext ,但是为web应用程序提供了更多的必要的特性。

SpringMVC需要的配置要求

为了将用户的请求映射到分发器DispatcherServlet 去处理,需要在web.xml中配置URL映射。

以下是一个声明和映射了Demo应用HelloWeb的分发器DispatcherServlet 的配置示例(就是servlet映射配置):

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

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

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

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Spring MVC Application

HelloWeb

org.springframework.web.servlet.DispatcherServlet

1

HelloWeb

/

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

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

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

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Spring MVC Application

HelloWeb

org.springframework.web.servlet.DispatcherServlet

1

HelloWeb

/

首先: web.xml文件根据servlet规范放在WebContent/WEB-INF目录下,使用Mavan风格的放在scr/main/webapp/WEB-INF目录下。

一旦初始化HelloWeb的的DispatcherServlet,框架将会尝试从WebContent/WEB-INF目录下的[servlet-name]-servlet.xml配置文件加载应用上下文application context。在这个Demo中,会去加载 HelloWeb-servlet.xml文件。这是默认行为,我们也可以指定加载配置文件的位置,这点后面会有实例示范。

然后: <\servlet-mapping>标签指明什么样的URL才会被DispatcherServlet映射处理。这里配置的是所有的HTTP请求都会被HelloWeb的DispatcherServlet映射处理

如果你不想使用默认的文件名[servlet-name]-servlet.xml和默认的context加载位置WebContent/WEB-INF(或者scr/main/webapp/WEB-INF),你也可以在web.xml中通过添加一个servlet监听器ContextLoaderListener来自定义指定文件名和位置。示例配置如下:

....

contextConfigLocation

classpath:spring-mvc-config.xml

org.springframework.web.context.ContextLoaderListener

接下来: 使用自定义servlet上下文配置后,我们来看看spring-mvc-config.xml文件的配置信息,该文件放在src/main/resources目录下,文末会展示Demo结构视图:

xmlns:context="http://springframework.org/schema/context"

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

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

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

http://springframework.org/schema/context

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

xmlns:context="http://springframework.org/schema/context"

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

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

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

http://springframework.org/schema/context

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

这里有几点比较重要:

[servlet-name]-servlet.xml文件(现在我们自定义为spring-mvc-config.xml) 会创建已经定义的bean, 会覆盖其他位置已经定义的同名的全局的bean,这点是血泪的教训,参见mapper注入失败问题解决

标签用于激活Spring MVC注解扫描功能,像@Controller、 @RequestMapping等

InternalResourceViewResolver 将使用下面定义的规则来解析视图名称。 按照我们配置的规则:一个名为”hello”的逻辑视图的实现将会是/WEB-INF/jsp/目录下的hello.jsp文件来渲染。

接下来: 我们编写一些实际使用的组件例如Controller, Model and View。

定义一个Controller

DispatcherServlet代表请求通过控制器来执行该请求的指定业务处理功能。@Controller 注解表明该类是一个控制器。@RequestMapping注解用于请求的URL映射到一个类或一个特定的处理方法

package org.byron4j.helloWeb;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

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

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

/**

* Hello world!

*

*/

@Controller

@RequestMapping("/hello")

public class HelloController{

@RequestMapping(method = RequestMethod.GET)

public String printHello(ModelMap model) {

model.addAttribute("message", "Hello Spring MVC Framework!");

return "hello";

}

}

@Controller注解将一个类定义为Spring MVC 的控制器controller。 在这里@RequestMapping的第一个使用位置是表明这个Controller类的所有方法都是处理/hello请求的。 第二个位置的用法@RequestMapping(method = RequestMethod.GET) 表明printHello()方法作为这个Controller类的默认服务方法去处理HTTP的GET请求。你可以依葫芦画瓢定义一个方法处理同名的的URL的POST请求,试试,你仅仅需要在新方法中将@RequestMapping(method = RequestMethod.GET)替换为@RequestMapping(method = RequestMethod.POST)即可。

你也可以将上面的Controller替换成下面的的这种写法:

@Controller

public class HelloController{

@RequestMapping(value = "/hello", method = RequestMethod.GET)

public String printHello(ModelMap model) {

model.addAttribute("message", "Hello Spring MVC Framework!");

return "hello";

}

}

功能是一样的。

@RequestMapping的value属性指明该方法仅仅映射处理的GET方式的请求/hello。 以下有几点比较重要:

你可以在Controller的处理方法中调用其他业务方法去处理业务逻辑。

基于定义的业务逻辑,你需要在该方法中创建一个模型对象。你可以设置不同的模型属性,这些属性可以让视图访问最终渲染给用户。这个demo中创建了一个包含”message”属性的模型对象。

一个定义的服务方法可以返回一个包含渲染模型的视图名字的字符串。这个demo返回的”hello” 作为一个逻辑视图的名字。

创建JSP视图

SpringMVC支持多种类型的视图显示技术。包括 JSP, HTML, PDF, Excel worksheets, XML, Velocity 末班, XSLT, JSON, Atom 和RSS feeds, JasperReports, Thymeleaf等等。这里我们使用jap视图, 在/WEB-INF/hello/jsp目录下创建一个hello.jsp:

这里的${message} 是Controller中模型对象设置的属性message的值。你还可以在你的视图中展示另外的其他属性。

demo最后自定义的web.xml:

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

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

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

version="2.5">

Spring MVC Application

/index.jsp

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc-config.xml

1

dispatcherServlet

/

contextConfigLocation

classpath:spring-mvc-config.xml

org.springframework.web.context.ContextLoaderListener

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

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

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

version="2.5">

Spring MVC Application

/index.jsp

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc-config.xml

1

dispatcherServlet

/

contextConfigLocation

classpath:spring-mvc-config.xml

org.springframework.web.context.ContextLoaderListener

demo项目的pom.xml:

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

4.0.0

org.byron4j

helloWeb

1.0.0-SNAPSHOT

war

helloWeb

http://blog.csdn.net/zixiao217

UTF-8

4.2.5.RELEASE

1.2.17

&ltbUMIPnZXP;/properties>

org.springframework

spring-webmvc

${spring.version}

log4j

log4j

${log4j.version}

javax.servlet

servlet-api

2.5

junit

junit

3.8.1

test

jstl

jstl

1.2

org.apache.maven.plugins

maven-compiler-plugin

3.5.1

1.8

1.8

helloWeb

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

4.0.0

org.byron4j

helloWeb

1.0.0-SNAPSHOT

war

helloWeb

http://blog.csdn.net/zixiao217

UTF-8

4.2.5.RELEASE

1.2.17

&ltbUMIPnZXP;/properties>

org.springframework

spring-webmvc

${spring.version}

log4j

log4j

${log4j.version}

javax.servlet

servlet-api

2.5

junit

junit

3.8.1

test

jstl

jstl

1.2

org.apache.maven.plugins

maven-compiler-plugin

3.5.1

1.8

1.8

helloWeb

以下是demo在eclipse的package explorer视图:

以下是demo在eclipse的navigator视图:

运行项目,在浏览器输入http://localhost:8080/helloWeb/hello,显示:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口设计:建立高效系统的关键步骤
下一篇:什么是接口开发?如何进行接口开发?接口开发的意义
相关文章

 发表评论

暂时没有评论,来抢沙发吧~