zoukankan      html  css  js  c++  java
  • spring boot 使用视图modelandview

     1:springboot使用视图解析器,添加依赖

            <!-- freemarker模板引擎视图 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
    
            <!-- 热部署,不用重启 ,这个在这里不需要-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
    
            <!-- jsp解析器 -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>

    2:主函数需要继承SpringBootServletInitializer,并覆盖其方法。

    package com.liyafei;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    @EnableAutoConfiguration
    @SpringBootApplication
    //返回jsp页面必须继承SpringBootServletInitializer类重写里面的方法
    public class Main extends SpringBootServletInitializer{
    
        public static void main(String[] args) {
            SpringApplication.run(Main.class, args);
            
        }
        
        protected SpringApplicationBuilder config(SpringApplicationBuilder applicationBuilder){
            return applicationBuilder.sources(Main.class);
        }
    }

    3:配置文件中添加spring.mvc.view配置,配置了视图解析器之后,controlller返回的String,View等就会先找视图解析器

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/demo
        username: root
        password: 1367356
      mvc:
        view:
          prefix: /WEB-INF/
          suffix: .jsp
    
    mybatis:
      config-location: classpath:mybatis-config.xml


    4:controller映射

      

    package com.liyafei.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
    
    import com.liyafei.pojo.User;
    
    //这个注解不能使用RestController,不然会返回模板类型的页面
    @Controller
    public class MyController {
        
        User user=new User();
        @RequestMapping("/my")
        public ModelAndView test(){
            ModelAndView mv=new ModelAndView();
            mv.setViewName("modelandview");
            mv.addObject("name", "liyafei");
            user.setAge(20);
            user.setName("wangwu");
            mv.addObject("user", user);
            
            //设置返回的数据为json类型,也可以不设置,返回对象
            //mv.setView(new MappingJackson2JsonView());
            return mv;
        }
        @RequestMapping("index")
        public String index(){
            return "index";
        }
    
    }

    5:测试成功:

      

    6:目录结构

    本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究
  • 相关阅读:
    HDU5877
    HDU5874
    HDU5875
    广西党史知识竞赛活动
    知识竞答小程序更新记录
    答题小程序批量导入时增加对图片的支持
    关注】答题赢话费,安全用妆知识竞赛小程序上线啦!
    反向代理应知应会
    抽奖助手小程序v3
    答题小程序功能列表
  • 原文地址:https://www.cnblogs.com/liyafei/p/7955943.html
Copyright © 2011-2022 走看看