Spring学习日记3(SpringMVC入门)

Spring学习日记3(SpringMVC入门),第1张

Spring学习日记3(SpringMVC入门) Spring集成web环境

Spring提供获取应用上下文的工具

Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象

所以我们需要做的只有两件事: 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标) 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext

在web.xml中进行配置

  
    contextConfigLocation
    classpath:applicationContext.xml
  

  
     org.springframework.web.context.ContextLoaderListener
  

使用用例

//        获取servlContext域
        ServletContext servletContext = this.getServletContext();
//    获取应用上下文
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        UserService userService = (UserService) webApplicationContext.getBean("userService");

        userService.eat();
SpringMVC简介
SpringMVC快速入门

导入SpringMVC相关坐标

配置SpringMVC核心控制器DispathcerServlet

创建Controller类和视图页面

使用注解配置Controller类中业务方法的映射地址

配置SpringMVC核心文件 spring-mvc.xml

客户端发起请求测试

 

导入SpringMVC相关坐标

      
          org.springframework
          spring-webmvc
          5.0.5.RELEASE
      

配置SpringMVC核心控制器DispathcerServlet

  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet

    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
  
  
    DispatcherServlet
    /
  

创建Controller类和视图页面

使用注解配置Controller类中业务方法的映射地址

package com.xxx.controller;

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

@Controller
public class UserController {
    @RequestMapping("/quick")
    public String quick(){
        System.out.println("success..");
        return "success.jsp";
    }
}

配置SpringMVC核心文件 spring-mvc.xml

 

客户端发起请求测试

 

SpringMVC组件解析

执行流程

 

 注解解析

@RequestMapping

value:相当于路径

method:用于设置请求方法

params:用于对请求参数进行限制

xml配置解析

redirect:重定向前缀

forward: 转发前缀,默认

可以通过注入的形式设置固定前缀与后缀

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5137770.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-17
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存