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

SpringMVC学习

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

SpringMVC学习

一、SpringMVC概述

概念: 实现mvc 模式的控制层框架,spring框架下的一个独立子框架(子模块)

二、入门程序
  •    新建maven项目导入springmvc特有的依赖
        
            org.springframework
            spring-webmvc
            4.3.14.RELEASE
        
  • 在web.xml中配置用来分发到不同controller的分发servlet

        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml  // 告诉系统springmvc controller的配置文件在哪里
        
        1  // web站点一启动就加载
    

    
        dispatcherServlet  
        /  // 拦截一切路径
    
  •  手动创建springmvc的配置文件    

    
    
    

    
    
  •  配置视图解析器

      // 前缀是/
      // 后缀是.jsp

  • 程序
@Controller
public class TestController {

    @RequestMapping("/sayhello")
    public ModelAndView sayHello(String name){
        System.out.println(name + ":hello");
        
        ModelAndView mv = new ModelAndView();
        //  回传给jsp 的数据
        // 相当于 request.setArr
        mv.addObject("msg",name+":hello");
        // 回到哪里去
        // 相当于 request.getDisptach("path").forward
        // 他会加上视图解析器种前缀和后最  /index.jsp
        mv.setViewName("index");
        return mv;
    }
}

三、请求传参
  • request传参
@RequestMapping("/testp01")
public ModelAndView testp01(HttpServletRequest request){
    System.out.println(request.getParameter("name"));
    return null;
}
  • 直接传参
@RequestMapping("/sayhello")
public ModelAndView sayHello(String name){
    System.out.println(name + ":hello");

    return null;
}
  • 直接传参但是页面name 和 参数名字不一样

@RequestMapping("/testp02")
public ModelAndView testp02(@RequestParam("name1") String name){
    System.out.println(name);
    return null;
}
  • 传对象
// 1 创建对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {

    private String name;
    private Integer age;
}

// 2 jsp 页面上传第对象
3: 直接传对象 
// 3 controller 种接收 对象 @RequestMapping("/testp03") public ModelAndView testp03(User user){ System.out.println(user); return null; }
  • 路径传参
路劲传参1
路劲传参2

// controller
@RequestMapping("/testp04/{id}")
public ModelAndView testp04(@PathVariable("id") Integer num){
    if(num==123)
     {
       System.out.println("我是1号技师");
     }else if(num==456)
     {
       System.out.println("我是2号技师");
     }
    return null;
}
  • 传参乱码问题

1.post请求


encoding
org.springframework.web.filter.CharacterEncodingFilter

encoding
utf-8



encoding

public String sayHello01(String name, ModelMap map){
    // 1 打印页面上传来的参数
    System.out.println(name + ":hello");
    // 2 要回传(给页面的值)
    map.put("message",name+": hello");
    // 3 要回到哪里去
    return "index";  // 注意这里,因为我前面配置了视图解析器
}
  • 默认就是转发的新式,下面我门看重定向
@RequestMapping("/sayhello01")

public String sayHello01(String name, ModelMap map){
    // 1 打印页面上传来的参数
    System.out.println(name + ":hello");
    // 2 要回传(给页面的值)
    map.put("message",name+": hello");
    // 3 要回到哪里去
    // 加上redirect关键字  视图解析器中配置的 前后锥就不起作用了
    return "redirect:/index.jsp";
}

前后端分离写法

  • 导入依赖

    com.fasterxml.jackson.core
    jackson-databind
    ${jackson.version}
  • 设置跨域问题
@CrossOrigin("*")
@RequestMapping("/testjson")
@ResponseBody  //把返回值  在最后变成json 然后写回去
public User testJson(){
    User user = User.builder().age(18)
            .name("zoukx")
            .birthday(new Date()).build();
    return user;
}
  • 写一个前端代码来拿,按F12打开控制台即可看到



    
    
    
    document
    


    

    

五、SpringMVC的工作流程
  1. 用户发送请求到前端控制器DispatcherServlet
  2. DispatcherSevlet收到请求调用HandlerMapping处理器映射
  3. 处理器映射器根据请求url找到具体的服务器,生成处理器对象及拦截器(如果有则生成)一并返回给DispatcherServlet
  4. DispatcherServlet通过HandlerAdapter适配器调用处理器
  5. 执行处理器(Controller,也叫后端控制器
  6. Controller执行完返回ModelandView
  7. HandlerAdapter将Cotroller执行结果返回给DispatcherServlet
  8. DispatcherServlet将ModelandView返回给视图解析器
  9. 视图解析器解析后返回具体的View
  10. DispatcherServlet对View进行渲染视图
  11. DispatcherServlet响应用户
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/274077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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