我有添加html的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
.yml也有相关的配置
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,是有改到的。
我就不懂是那里出错了,访问 / 总是返回String "redirect:login" 到页面上,配置好了没登录的话不应该跳去login.html吗?
检查控制器的注解是否是
@Controller
@RestController
不会返回视图,只会返回数据将
@RestController
换成@Controller