【框架整合】一、spring+springMVC框架搭建

来源:互联网 发布:tensorflow可视化界面 编辑:程序博客网 时间:2024/04/30 11:11

一、简介

       1.

Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,

即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,

框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的


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

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

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

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

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

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

√非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,

  而是放在一个Model里(Map数据结构实现,  因此很容易被其他框架使用);

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

√提供一套强大的JSP标签库,简化JSP开发;

√支持灵活的本地化、主题等解析;

√更加简单的异常处理;

√对静态资源的支持;

√支持Restful风格。




2.请求流程

                随便找的图





具体执行步骤如下:

a、  首先用户发送请求————>前端控制器,前端控制器根据请求信息(如URL)来决定选择哪一个页面控制器进行处理并把请求委托给它,即以前的控制器的控制逻辑部分;图中的1、2步骤;

b、  页面控制器接收到请求后,进行功能处理,首先需要收集和绑定请求参数到一个对象,这个对象在Spring Web MVC中叫命令对象,并进行验证,然后将命令对象委托给业务对象进行处理;处理完毕后返回一个ModelAndView(模型数据和逻辑视图名);图2中的3、4、5步骤;

c、  前端控制器收回控制权,然后根据返回的逻辑视图名,选择相应的视图进行渲染,并把模型数据传入以便视图渲染;图中的步骤6、7;

d、  前端控制器再次收回控制权,将响应返回给用户,图中的步骤8;至此整个结束





3.核心架构图







二、框架搭建


          暂时不使用maven进行构建

1. 下载spring jar包

下载地址:http://repo.spring.io/release/org/springframework/spring/

如图(这里只是部分)


2. 新建java web 工程

导入如下必需jar包

//这个jar 文件包含Spring 框架基本的核心工具类。Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己///的应用系统中使用这些工具类。//外部依赖Commons Logging(Log4J)。spring-core-4.0.0.RELEASE.jarcommons-logging-1.2.jar//这个jar 文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control / Dependency Injection//(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI 支持,引入spring-core.jar 及spring-beans.jar 文件就可以了spring-beans-4.0.0.RELEASE.jar//这个jar 文件为Spring 核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,//JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类spring-context-4.0.0.RELEASE.jar//这个jar 文件包含在应用中使用Spring 的AOP 特性时所需的类和源码级元数据支持。使用基于AOP 的Spring特性,如声明型事务管理//(Declarative Transaction Management),也要在应用里包含这个jar包spring-aop-4.0.0.RELEASE.jar//这个jar 文件包含Web 应用开发时,用到Spring 框架时所需的核心类,包括自动载入Web Application Context 特性的类、//Struts 与JSF 集成类、文件上传的支持类、Filter 类和大量工具辅助类spring-web-4.0.0.RELEASE.jar//这个jar 文件包含Spring MVC 框架相关的所有类。包括框架的Servlets,Web MVC框架,控制器和视图支持。//当然,如果你的应用使用了独立的MVC 框架,则无需这个JAR 文件里的任何类spring-webmvc-4.0.0.RELEASE.jar//为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理spring-tx-4.0.0.RELEASE.jar//Spring表达式语言spring-expression-4.0.0.RELEASE.jarspring-context-support-4.0.0.RELEASE.jar//这个jar 文件包含支持UI模版(Velocity,FreeMarker,JasperReports),邮件服务,脚本服务(JRuby),缓存Cache(EHCache),任///务计划Scheduling(uartz)方面的类。



3. 配置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" 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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>mySpringmvcWeb</display-name>    <!-- webAppRootKey配置作用:可以用System.getProperty("webapp.root")来动态获项目的运行路径。                  一般返回结果例如:/usr/local/tomcat6/webapps/项目名 -->  <context-param>    <param-name>webAppRootKey</param-name>    <param-value>mySpringmvcWeb</param-value>  </context-param>    <!-- spring mvc 配置 -->  <servlet>    <servlet-name>springmvc-web</servlet-name>    <!-- spring mvc 核心分发器 -->    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml -->      <param-value>classpath:spring-servlet.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>  </servlet>    <servlet-mapping>    <servlet-name>springmvc-web</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>    <!-- spring 配置 -->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>    <!-- 防止内存泄露 -->  <listener>    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  </listener>    <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>    <filter>    <filter-name>encoding</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>  </filter>    <filter-mapping>    <filter-name>encoding</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>    <welcome-file-list>    <welcome-file>index.html</welcome-file>  </welcome-file-list></web-app>




4.配置spring-servlet.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd "><!-- (注解探测器)启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --><context:component-scan base-package="com.mvc" use-default-filters="false"><context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /></context:component-scan><!-- 默认静态资源处理 -->    <!-- <mvc:default-servlet-handler/> -->        <!-- 启动注解驱动的Spring MVC功能 --><!-- 注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --><mvc:annotation-driven /><!-- spring mvc 视图解析器配置 --><bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="contentType" value="text/html;charset=utf-8"/><property name="cache" value="true" /><property name="prefix" value="/WEB-INF/webapp/"/><property name="suffix" value=".jsp"/></bean></beans>


5. 配置 applicationContext.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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"default-autowire="byName" default-lazy-init="false"><context:annotation-config /><!-- 该配置项其实包含了自动注入bean<context:annotation-config />的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。 --><context:component-scan base-package="com.mvc" />    </beans>



三、测试




新建包 com.mvc.first

package com.mvc.first;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * 搭建springmvc 测试 *  * @author xiazhang * @date   2017-6-11 */@Controller@RequestMapping(value="/hello")public class FirstController {    private static Log logger = LogFactory.getLog(FirstController.class);    @RequestMapping(value="/world",method=RequestMethod.GET)    public String hello(Model model,HttpServletRequest request,HttpServletResponse response){    response.setCharacterEncoding("UTF-8");        model.addAttribute("msg", "你好spring mvc");                return "index";    }}


页面直接用index.jsp 就好了


稍微改一下

注意spring-servlet.xml 中视图解析器中的配置


<!-- spring mvc 视图解析器配置 --><bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="contentType" value="text/html;charset=utf-8"/><property name="cache" value="true" /><property name="prefix" value="/WEB-INF/webapp/"/><property name="suffix" value=".jsp"/></bean>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>xiazhang</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    ${msg}<br>  </body></html>



发布服务,结果界面




测试成功





原创粉丝点击