spring boot跳转html

1、因为springboot不赞成使用jsp页面,因此考虑spring boot推荐的Thymeleaf模版

添加模版

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-freemarker</artifactId>

</dependency>

application.properties添加如下配置:(这个不配置也可以的,如果不配置,会到resources/templates下去找同名的模板或者页面)
# 定位模板的目录
spring.mvc.view.prefix=classpath:/templates/
# 给返回的页面添加后缀名
spring.mvc.view.suffix=.html

2、设计一个controller代码

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Auther: Matthew
 * @Date: 2018/12/4 09:12
 * @Description:
 */

@Controller
@RequestMapping(value = "/oa")
public class Handle {

    /**
     * 存入数据模型,并返回跳转的页面
     * @param model
     * @return String
     */
    @RequestMapping(value = "/login")
    public String index(Model model) {

        System.out.print("进来了");
        model.addAttribute("title", "the first spring boot");
        model.addAttribute("name", "楚苓大妈");

        return "index";
    }
}

模板使用demo
          <h1 id="title">${title}</h1>
  模板中的各类语法自己搜索,这里不做介绍
 
  注意,spring boot默认开启了静态文件的配置,任何放在static文件夹下的资源都是静态文件。引用静态文件时以/或者前缀不加任何定位符,都会去static文件夹下查找。
 
  2,实际项目中,现在没有使用模板了。都是使用的静态界面html
  和模板差不多
  # 定位页面的目录到static/下
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
controller当然也是需要的,和之前一样:
@Controller//这里只能用这个

@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
 

1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

例如:本来应该到success.html页面的,则其显示success.

2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。

3)如果需要返回json或者xml或者自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解

 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matthew_leung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值