栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringMVC学习

SpringMVC学习

1、Hello SpringMVC 1.1、导入maven依赖

    junit
    junit
    4.12



    org.springframework
    spring-webmvc
    5.3.9



    javax.servlet
    servlet-api
    2.5


    javax.servlet.jsp
    jsp-api
    2.1


    javax.servlet
    jstl
    1.2

1.2、配置web.xml文件
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet

        
            contextConfigLocation
            classpath:springmvc-servlet.xml
        

        1
    

//    真实访问地址/HelloController/hello
    @RequestMapping("hello")
    public String hello(Model model) {
        model.addAttribute("msg","helloSpringMVC");
        //web-inf/jsp/hello.jsp
        return "hello";
    }
}
2、接收前端传来的参数 2.1、固定参数
  1. 后端代码

     @RequestMapping("Param/p1")
        public String p1(@RequestParam("a") int a, @RequestParam("b") int b, Model model) {
            int result = a + b;
            model.addAttribute("msg","结果是:"+result);
            return "hello";
        }
    
  2. 测试:http://127.0.0.1:8080/Param/p1?a=1&b=2

2.2、不同method
@GetMapping("Param/p1")
    public String p1(int a, int b, Model model) {
        int result = a + b;
        model.addAttribute("msg","结果是:"+result);
        return "hello";
    }
@PostMapping("Param/p1")
public String p2(int a, int b, Model model) {
    int result = a + b;
    model.addAttribute("msg","结果是:"+result);
    return "hello";
}
2.3、接收对象
 @GetMapping("update")
    public String Update(User user) {
        System.out.println(user);
        return "test02";
    }
2.4、RestFul风格
@GetMapping("Param/p1/{a}/{b}")
public String p1(@PathVariable int a, @PathVariable int b, Model model) {
    int result = a + b;
    model.addAttribute("msg","结果是:"+result);
    return "hello";
}
  • 测试:http://127.0.0.1:8080/Param/p1/1/2
3、路由跳转 1、转发
  • 默认方式

    return "hello";
    
  • forward

    return "forward:hello";
    
2.重定向
  • “redirect:/具体路径”

     return "redirect:/WEB-INF/jsp/test02.jsp";
    
4、设置JSON 4.1、Jackson的使用
  1. 配置maven

            
                com.fasterxml.jackson.core
                jackson-databind
                2.13.0
            
    
  2. 解决json return 乱码问题

    • 在springmvc-servlet.xml下

          
              
                  
                      
                  
                  
                      
                          
                              
                          
                      
                  
              
          
      
  3. 创建工具类

    • JsonUtil

      public class JsonUtil {
          public static String setJson(Object o) {
              return setJson(o,"yyyy-MM-dd HH:mm:ss");
          }
          public static String setJson(Object o,String form) {
              ObjectMapper mapper = new ObjectMapper();
              mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
              SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              mapper.setDateFormat(simpleDateFormat);
              try {
                  String s = mapper.writevalueAsString(o);
                  return s;
              } catch (JsonProcessingException e) {
                  e.printStackTrace();
              }
              return null;
          }
      }
      
      
  4. 使用

        @RequestMapping(value = "/j1")
    //    使方法不经过视图解析器
    //    @RequestBody
        public String json1() {
            User user = new User("吕竟", "男");
            return JsonUtil.setJson(user);
        }
    
        @RequestMapping("/j2")
        public String json2() {
            List list = new ArrayList();
            User user = new User("吕竟", "nv");
            User user1 = new User("吕竟", "nv");
            User user2 = new User("吕竟", "nv");
            list.add(user);
            list.add(user1);
            list.add(user2);
            return JsonUtil.setJson(list);
        }
    
        @RequestMapping("/j3")
        public String json3() {
            Date date = new Date();
            return JsonUtil.setJson(date);
        }
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/652178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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