鍍金池/ 問答/Java  HTML/ SpringBoot為什么沒有加載.html?

SpringBoot為什么沒有加載.html?

我有添加html的依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

.yml也有相關(guān)的配置

spring:
  session:
    store-type: redis
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    content-type: text/html
    encoding: utf-8

controller的代碼是這樣的

package com.chok.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@SuppressWarnings("unchecked")
@RestController
@RequestMapping("redis")
public class ReidsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public HttpServletRequest getRequest()
    {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest req = attributes.getRequest();
        return req;
    }


    @RequestMapping("/test")
    public ResponseEntity<?> httpSessionRedisTest(HttpSession httpSession)
    {
        if (httpSession.isNew()) {
            logger.info("Successfully creates a session ,the id of session :" + httpSession.getId());
            httpSession.setAttribute("key", "hello");
        } else {
            logger.info("session already exists in the server, the id of session :" + httpSession.getId());
            logger.info(httpSession.getAttribute("key").toString());
        }

        ResponseEntity<?> entity = new ResponseEntity<Object>("Hello world, session id:"
                + httpSession.getId(), HttpStatus.OK);

        return entity;
    }

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String index(HttpSession httpSession)
    {
        Object user = httpSession.getAttribute("name");
        if(user == null)
        {
            return "redirect:login";
        }
        HttpServletRequest req = this.getRequest();
        httpSession.setAttribute("port", req.getLocalPort());
        return "index";
    }

    @RequestMapping(value="/login",method = RequestMethod.GET )
    public String login(HttpSession httpSession)
    {
        Object user = httpSession.getAttribute("name");
        HttpServletRequest req = this.getRequest();
        httpSession.setAttribute("port", req.getLocalPort());

        if(user != null)
        {
            return "index";
        }
        return "login";
    }

    @RequestMapping(value="/login",method = RequestMethod.POST )
    public String loginForm(String name, HttpSession httpSession){
        httpSession.setAttribute("sessionid", httpSession.getId());
        httpSession.setAttribute("name", name);
        HttpServletRequest req = this.getRequest();
        httpSession.setAttribute("port", req.getLocalPort());
        return "index";
    }

    @RequestMapping(value="/logout" )
    public String logout(HttpSession httpSession){
        httpSession.invalidate();
        return "redirect:index";
    }
}

我一開始以為我的.yml文件配置不起作用,但是我把端口8080改成9002,是有改到的。
我就不懂是那里出錯(cuò)了,訪問 / 總是返回String "redirect:login" 到頁(yè)面上,配置好了沒登錄的話不應(yīng)該跳去login.html嗎?

回答
編輯回答
伴謊

檢查控制器的注解是否是@Controller
@RestController不會(huì)返回視圖,只會(huì)返回?cái)?shù)據(jù)
@RestController換成@Controller

2018年3月8日 19:44