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

SpringMVC基础知识总结+示例

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

SpringMVC基础知识总结+示例

SpringMVC基础知识

学完SpringMVC必须知道的知识点,

首先导依赖

    
      org.springframework
      spring-web
      5.0.5.RELEASE
    
    
    
      org.springframework
      spring-webmvc
      5.0.5.RELEASE
    

    
    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
    
      javax.servlet.jsp
      javax.servlet.jsp-api
      2.3.3
      provided
    
    web与spring的集成
    web.xml的配置:
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

applicationContext.xml文件内容


    
    

使用以上web.xml配置把spring的配置文件在服务器启动时就直接加载进去,不用我们手动加载spring配置文件
测试:spring是否加载成功

@WebServlet("/User")
public class UserController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        //使用就监听器让服务器一启动就创建应用上下文,保存在最大域Context中 避免每次都需要创建应用上下文
        ServletContext servletContext = this.getServletContext();
        ApplicationContext app=WebApplicationContextUtils.getWebApplicationContext(servletContext);
        User bean = app.getBean(User.class);
        System.out.println(bean);

    }

}

启动服务器输入配置的Servlet地址回来查看控制台显示如下

这样就spring与web集成了

    SpringMVC配置
    配置 web.xml文件,把springmvc-config.xml 加载到核心控制器(配置前端控制器)
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
       1
        

            contextConfigLocation
            classpath:springmvc-config.xml
        
    
    
        dispatcherServlet
        /
    

控制器

@Controller
@RequestMapping("/user")//访问路径的总地址
public class UserController  {

@RequestMapping(value = "/test1" ,method = RequestMethod.GET)//uesr路径下的test1路径
    public String test1(){
    System.out.println("字符串返回方法代表能返回页面");
    return "../success.jsp";//字符串返回的是一个页面地址
}

    }

利用web.xml文件添加过滤器防止中文乱码:

    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        

            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    

这样web.xml的东西差不多就配好了
1.加载应用上下文(加载 applicationContext.xml文件)
2.配置前端控制器(加载springmvc-config.xml文件)
3.配置过滤器(防止中文乱码)

web.xml配置代码:






  CharacterEncodingFilter
  org.springframework.web.filter.CharacterEncodingFilter
  
    encoding
    UTF-8
  

  
    CharacterEncodingFilter
    /*
  


  Archetype Created Web Application
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

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



springmvc-config.xml基础配置
第一步配置注解扫描,只扫描控制层

  

第二步 配置注解驱动

 

第三步开放资源权限


第四步配置视图解析器

    

        
    

未配置视图解析器 返回的路径前+../返回到根路径前端控制器才能找到
基本的springmvc-config.xml的文件配置就差不多了
合在一起代码如下:




    
    
    
        
    

    

    
        
        
    


    控制层的跳转方法

1.字符串方法跳转

@RequestMapping(value = "/test1" ,method = RequestMethod.GET)
    public String test1(){
    System.out.println("字符串返回方法代表能返回页面");
    return "success";
}

2 返回modelandView方法

    @RequestMapping(value = "/test2" ,method = RequestMethod.GET)
    public ModelAndView test2(){
        System.out.println("字符串返回方法代表能返回页面");
        ModelAndView modelAndView=new ModelAndView();
//        存储值 相当于javaWeb的 session对象 利用键值存储
        modelAndView.addObject("user",1111);
        //设置跳转路径
        modelAndView.setViewName("success");
        return modelAndView;
    }

3.形参类型自动注入
3.1 自动注入model 类型 ,返回字符串类型

    @RequestMapping("/test3")
    public String test3(Model model){
    //自动把形参的类型注入
    model.addAttribute("name","张三");
    return "success";
    }

3.2自动注入实体类型无返回值
网页地址上…/test5?name=zs&sex=1 进行访问 spring会自动把网页上传的参数与形参属性相同的进行注入

    @RequestMapping("/test5")
//    告诉框架进行数据响应,不进行页面跳转
    @ResponseBody
    public void test5(User user){
        System.out.println(user);
    }

结果:

3.3
自动注入数组类型 ,只要网页参数名字与形参名字相同,且网页上传的参数有多个值,则会进行数组注入:输入网址…/test6/?str=1&str=2&str=3

    @RequestMapping("/test6")
//    告诉框架进行数据响应,不进行页面跳转
    @ResponseBody
    public void test6(String str[]){
        System.out.println(str);
    }

3.4
网页上传的参数与 形参不一致也能进行赋值
使用@QuestParam进行对参数的映射

    @RequestMapping("/test8")
//    告诉框架进行数据响应,不进行页面跳转
    @ResponseBody
    public void test8(@RequestParam(name = "sex") String ser){
        System.out.println(Arrays.asList(ser));
    }

restful风格编码根据提交方法不同,把参数跟着地址后面 而不是跟在问号后面不用带参数,直接地址后写/值
…/test1?name=2
restful风格:
…/test/2
@PathVariable()告诉Spring 是路径上的参数

    @RequestMapping("/test9/{name}" )
//    告诉框架进行数据响应,不进行页面跳转
    @ResponseBody
    public void test9(@PathVariable ("name")String name){
        System.out.println(name);
    }

以上就是SpringMVC需要掌握的知识点
总结:
web.xml配置
三步:过滤器,加载应用上下文,配置前端控制器
springmvc-config配置
四步:包扫描,注解驱动,视图解析器,开放资源
controller控制层的跳转:字符串跳转页面,ModelAndView跳转
数据处理:形参自动注入,restful风格,引用类型注入
使用的注解:
@RequestMapp()
@Controller
@ResponseBody
@RequestBody
@PathVariable
@RequestParam

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

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

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