当前位置: 首页 > news >正文

springMVC框架搭建流程


1、创建Dynamic Web Project

2、导入spring和springmvc所需要的文件

3、配置web.xml文件

3.1 监听spring上下文容器

3.2 加载spring的xml文件到spring的上下文容器(spring-context.xml)

3.3 配置spring MVC的DispatcherServlet

3.4 加载spring MVC的xml到spring的上下文容器(springMVC-context.xml)

3.5 配置DispatcherServlet所需要拦截的 url(固定了HTTP的格式 如*.do)

4、配置spring的xml文件

主要配置链接数据库等信息

5、配置spring MVC的xml文件

5.1 加载spring的全局配置文件

5.2 扫描指定包下的所有类是注解生效

5.3 配置SpringMVC的视图渲染器

6、写Controller(TestController.java)

7、写jsp文件

执行流程个人总结:接收到请求后会扫描springMVC配置文件中指定的包中的类(controller),根据controller中的注解@RequestMapping("test")找到对应的jsp文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
  <display-name></display-name>
  
  <!--1.  监听spring上下文容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 2. 加载spring的xml到spring的上下文容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context.xml</param-value>
  </context-param>

  <!-- 3. 配置spring MVC的DispatcherServlet -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--4.  加载spring MVC的xml到spring的上下文容器 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/springMVC-context.xml</param-value>
    </init-param>
    <!-- 启动加载该servlet -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <!--5. 配置DispatcherServlet所需要拦截的 url -->
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

spring-context.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:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                http://www.springframework.org/schema/context  
                 http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
    <!-- Root Context: defines shared resources visible to all other web components -->  
  
       
</beans>

springMVC-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.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">
  <!-- 加载Spring的全局配置文件 -->
  <beans:import resource="spring-context.xml" />
  
  <!-- SpringMVC配置 -->
  
  <!-- 通过component-scan 让Spring扫描org.swinglife.controller下的所有的类,让Spring的代码注解生效 -->
  <context:component-scan base-package="com.liuyunlong.controller"></context:component-scan>
  
  <!-- 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.jsp  将视图渲染到/page/<method返回值>.jsp中 -->
  <beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/page/" p:suffix=".jsp">
    </beans:bean>


</beans:beans>

TestController.java

package com.liuyunlong.controller;

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

@Controller
public class TestController {
  @RequestMapping("test")
  public String test(){
    return "test";
  }
}

jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'test.jsp' starting page</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>
  Welcome to springMVC <br>
  </body>
</html>

原文 :http://www.tuicool.com/articles/Nj6Bna



相关文章:

  • 四舍五入的方法
  • Java代码生成二维码图片
  • 2.Cocos2dx 3.2重力系统Box2D
  • Ubuntu下安装C/C++开发环境【!!!有更新!!!Ubuntu14.10下使用eclipse搭建C语言开发环境】
  • 翻译一篇文章:It's Difficult to Grow a Test Developer(成为测试开发工程师的艰辛)...
  • Spring的核心机制:依赖注入
  • Spring获取ApplicationContext对象工具类
  • linux基础学习8
  • Ubuntu系统更新命令笔记
  • 大龄屌丝自学笔记--Java零基础到菜鸟--038
  • VMWare虚拟机提示:锁定文件失败,打不开磁盘的解决办法
  • HDU2030 汉字统计
  • Windows下删除不需要的服务
  • [原创] SQLite数据库使用清单(下)
  • Oracle11g安装详细步骤【有图在里边哦】
  • 分享的文章《人生如棋》
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • Angular 响应式表单之下拉框
  • C++类中的特殊成员函数
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • CSS实用技巧
  • docker容器内的网络抓包
  • ES6 学习笔记(一)let,const和解构赋值
  • JavaScript对象详解
  • Linux gpio口使用方法
  • mongodb--安装和初步使用教程
  • PHP变量
  • React中的“虫洞”——Context
  • 对JS继承的一点思考
  • ------- 计算机网络基础
  • 简析gRPC client 连接管理
  • 一个完整Java Web项目背后的密码
  • 用 Swift 编写面向协议的视图
  • 最近的计划
  • 阿里云服务器如何修改远程端口?
  • ​ubuntu下安装kvm虚拟机
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • (C)一些题4
  • (HAL库版)freeRTOS移植STMF103
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (待修改)PyG安装步骤
  • (第二周)效能测试
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (算法设计与分析)第一章算法概述-习题
  • (转)【Hibernate总结系列】使用举例
  • (转)ObjectiveC 深浅拷贝学习
  • (转载)Linux 多线程条件变量同步
  • (转载)Linux网络编程入门
  • ***利用Ms05002溢出找“肉鸡
  • **PHP二维数组遍历时同时赋值
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET Framework .NET Core与 .NET 的区别
  • .NET6 命令行启动及发布单个Exe文件