zoukankan      html  css  js  c++  java
  • spring boot学习(3) SpringBoot 之MVC 支持

    第一节:@RequestMapping 配置url 映射
     
    第二节:@Controller 处理http 请求
    转发到一个页面,以前是转发到jsp页面,现在使用freemarker;
    在pom.xml页面右键,spring-edit starters , 添加freemarker支持:spring-boot-starter-freemarker
    pom.xml:
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>

    com.cy.controller.HelloWorldFreemarkerController.java:

    package com.cy.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/freemarker")
    public class HelloWorldFreemarkerController {
        
        @RequestMapping("/say")
        public ModelAndView say(){
            ModelAndView mav=new ModelAndView();
            mav.addObject("message", "springboot你好!");
            mav.setViewName("helloWorld");
            return mav;
        }
        
    }

    freemarker模板是ftl为后缀的;src/main/resources/templates/helloWorld.ftl:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    show: ${message}
    </body>
    </html>

    浏览器http://localhost:8888/HelloWorld/freemarker/say:显示:show: springboot你好!

    第三节:@RestController 处理ajax 请求
     
    第四节:@PathVariable 获取url 参数
     
    第五节:@RequestParam 获取请求参数

    例子如下:

    com.cy.controller.HelloWorldAjaxController.java:

    package com.cy.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @RequestMapping("/ajax")
    public class HelloWorldAjaxController {
        
        @RequestMapping("/hello")
        public String say(){
            return "{'message1':'SpringBoot你好','message2','Spring你好2'}";
        }
    }

    com.cy.controller.BlogController.java:

    package com.cy.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping("/blog")
    public class BlogController {
        
        @RequestMapping("/{id}")
        public ModelAndView show(@PathVariable("id") Integer id){
            ModelAndView mav=new ModelAndView();
            mav.addObject("id", id);
            mav.setViewName("blog");
            return mav;
        }
        
        @RequestMapping("/query")
        public ModelAndView query(@RequestParam(value="q",required=false) String q){
            ModelAndView mav=new ModelAndView();
            mav.addObject("q", q);
            mav.setViewName("query");
            return mav;
        }
        
    }

    webapp/index.html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <script src="resources/jquery/jquery.min.js"></script>
    <script type="text/javascript">
        
        function show(){
            $.post("ajax/hello",{},function(result){
                alert(result);
            });
        }
        
    </script>
    </head>
    <body>
    <button onclick="show()">点击</button>
    <a href="/HelloWorld/blog/21">博客</a>
    <a href="/HelloWorld/blog/query?q=123456">搜索</a>
    </body>
    </html>

    src/main/resoureces/templates/blog.ftl:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    博客id:${id}
    </body>
    </html>
    View Code

    src/main/resources/templates/query.ftl:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    q: ${q}
    </body>
    </html>
    View Code

    测试:

    浏览器:http://localhost:8888/HelloWorld/index.html

    点击按钮:alert: {'message1':'SpringBoot你好','message2','Spring你好2'}

    点击博客链接:转发页面,显示博客id:21

    点击搜索,到页面http://localhost:8888/HelloWorld/blog/query?q=123456,显示q: 123456

  • 相关阅读:
    郁闷,母版页为什么会这样?怎么在使用了母版页的情况下使用js?大家帮忙
    .NET中实现无刷新客户端联动下拉菜单 (无刷新)(一)
    ADO.NET(二)
    HasRows的返回值问题
    动态生成DataTable绑定至DataList一例
    关于FastReport4.3的使用心得1
    资源文件的编译
    加密当前数据库的所有存储过程。
    使用拼音首字母序列实现检索功能
    关于错误Access Violation和too many consecutive exceptions,解决方法
  • 原文地址:https://www.cnblogs.com/tenWood/p/8641778.html
Copyright © 2011-2022 走看看