栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC快速入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC快速入门

需求:客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。

开发步骤:
①导入SpringMVC相关坐标 (pom.xml)
②配置SpringMVC核心控制器DispathcerServlet (web.xml)
③创建Controller类和视图页面 (web.Controller)
④使用注解配置Controller类中业务方法的映射地址
⑤配置SpringMVC核心文件spring-mvc.xml(类似于applicationContext.xml)
⑥客户端发起请求测试

①导入SpringMVC相关坐标 (pom.xml)

    
      org.springframework
      spring-webmvc
      5.0.5.RELEASE
    

②配置SpringMVC核心控制器DispathcerServlet (web.xml)



  Archetype Created Web Application

  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:Spring-mvc.xml
    

    1
  
  
    DispatcherServlet
    /
  

③创建Controller类和视图页面 (web.Controller , jsp)

Controller类

@Controller
public class UserController {
    @RequestMapping("/quick")
    public String save(){
        System.out.println("Controller save running.....");
        return "success.jsp";
    }
}
视图页面 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


success


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

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

⑤配置SpringMVC核心文件spring-mvc.xml(类似于applicationContext.xml)
进行组件扫描





此外:
配置好后,还需要在web.xml中加载spring-mvc.xml文件,也即是第二步代码中的

    
      contextConfigLocation
      classpath:Spring-mvc.xml
    

到此才算完全配置好spring-mvc.xml 文件。

类似于监听器的配置,加载:

  
    org.springframework.web.context.ContextLoaderListener
  


  
    contextConfigLocation
    classpath:applicationContext.xml
  

这是通过Spring容器进行加载的,对比之前做过自己写的监听器加载;


    com.itcast.web.listener.ContextLoaderListener
  

  
    contextConfigLocation
    applicationContext.xml
  
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号