• springboot返回html和jsp


    一、返回html

    (1)添加maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    (2)thymeleaf模板默认寻找resources下,templates文件夹放html页面,static文件夹放css及js

    (3)controller代码如下

    @Controller
    @RequestMapping("money")
    public class MoneyController {
    @GetMapping("/description")
    public String getDescription(){
    return "aaa";
    }
    }


    二、返回jsp

    (1)添加jsp的maven依赖

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    </dependency>
    注:返回jsp需要把spring-boot-starter-thymeleaf注释掉

    (2)在controller里添加寻找jsp页面的视图解析器

    @Bean
    public InternalResourceViewResolver viewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setPrefix("/WEB-INF/");
      viewResolver.setSuffix(".jsp");
      return viewResolver;
    }
    (3)结构图如下

    (4)controller代码如下

    package springboot.controller;

    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;

    @Controller
    public class JspController {
    @RequestMapping("/test")
    public String index() {
    return "home";
    }
    @Bean
    public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
    }
    }
    注:返回html和jsp时使用@Controller注解
    ---------------------
    原文来自

  • 相关阅读:
    [转]tf.summary() 用法
    PASCAL VOC工具包解读
    [ERROR] 安装完Detectron后出现 cython_nms.so: undefined symbol: PyFPE_jbuf
    用Tensorflow做蝴蝶检测
    双系统,重装ubuntu后无法进入windows
    [Error]NodeDef mentions attr 'identical_element_shapes' not in Op<name=TensorArrayV3;
    [转]调试 smallcorgi/Faster-RCNN_TF 的demo过程遇到的问题
    js交互轮播图
    js取俩个数之间的随机数
    原生js实现触摸滚动轮播图
  • 原文地址:https://www.cnblogs.com/cuiqq/p/10843803.html
Copyright © 2020-2023  润新知