Spring MVC框架配置方法详解

网友投稿 230 2023-02-08


Spring MVC框架配置方法详解

本文实例为大家分享了Spring MVC框架配置方法,供大家参考,具体内容如下

1、概述

Spring MVC 作用:用来实现前端浏览器与后面程序的交互

Spring MVC 是基于Spring 的MVC框架,所谓MVC(model,controller,view) ,整个Spring MVC 作用就是,基于Spring 将model(数据)在controller(后台程序) ,view(前端浏览器)之间交互

至于Spring MVC优点缺点,了解不深 不作评价,

2、引用的jar包

既然是基于Spring那么 Spring的核心jar包(beans,context,core,expression,commons-logging)是必须的;Spring MVC的相关Jar包有个(web,webmvc),特别包(aop)这个包不是必须,但如果基于注解,用以包扫描的时候就必需

3、配置文件

配置文件,就是显式配置程序执行的初始化的文件。配置文件如下:

springmvc-config.xml

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

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

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/mvc

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

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

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

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/mvc

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

Web.xml

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

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-config.xml

1

springmvc

/

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

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc-config.xml

1

springmvc

/

图片来源(spring mvc DispatcherServlet之前端控制器架构详解)

步骤:1、客户端发起访问,被Spring MVC的前端控制器拦截(DispatcherServlet )

2、拦截器会找到映射器(handlerMapping),让映射器根据URL找到具体的bean,例如 上面如果 URL  "/firstController"  那么就找到了对应的Bean,并反馈给DispatcherServlet

3、DispatcherServlet将到找到的bean交给适配器(handlerAdapter),由适配器去调用对应的handler(执行bean中的方法)

4、执行完成后,将结果把返回给DispatcherServlet,然后由DispatcherServlet 交给视图解析器(ViewReslover)

5、视图解析完成后,再交给DispatcherServlet,然后交给view 渲染(比如 jsP) 。最后将渲染后的结果,反馈给客户端

4、Controller类

package com.itheima.controller;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* 控制器类

*/

public class FirstController implements Controller{

@Override

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) {

// 创建ModelAndView对象

ModelAndView mav = new ModelAndView();

// 向模型对象中添加数据

mav.addObject("msg", "这是我的第一个Spring MVC程序");

// 设置逻辑视图名

mav.setViewName("/WEB-INF/jsp/first.jsp");

// 返回ModelAndView对象

return mav;

}

}

示例中使用的适配器(SimpleControllerHandlerAdapter)要求handler必须实现Controller接口

5、JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

"http://w3.org/TR/html4/loose.dtd">

${msg}

测试即可通过

PS:以上代码为黑马视频教程的代码,自己手动敲的。

声明:本文是初学Spring MVC 用以记录笔记,完全小白,理解较浅显,如有大拿愿意指点,不胜感激


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

上一篇:SpringMVC使用MultipartFile实现文件上传
下一篇:Angular数据绑定机制原理
相关文章

 发表评论

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