我是使用Spring Framework的新手。
我找到了一个教程,向我展示了如何使用Spring Security设置简单的登录。 它还包括视图的一些代码:home.jsp和controller:HomeController.java
他们看起来像这样:针对home.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<link rel="stylesheet" href="../css/styles.css" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<a href=<c:url value="/j_spring_security_logout"/>>Logout</a><br/>
<sec:authorize ifAnyGranted="ROLE_ADMIN">
<h1>Only admin can see this</h1><br/>
<a href="admin"> Admin Home </a>
</sec:authorize>
<h1>Welcome!</h1>
Logged in as: <c:out value= **"${CurrentUser}"** /> !<br />
</body>
</html>
和HomeController.java
package rd.controller;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = "/home" , method = RequestMethod.GET)
public String setUp(Model model){
**model.addAttribute("CurrentUser", SecurityContextHolder.getContext().getAuthentication().getName());**
return "home";
}
}
从我收集的内容中,连接**的行被连接起来,使jsp页面的属性可供显示。然而,我正在做的事情和网站字面打印有问题:
Logged in as: ${CurrentUser}!
我可以使用逻辑检查以及Spring如何连接Controller和View的解释。此外,欢迎任何好的Spring教程链接,谢谢!
答案 0 :(得分:0)
尝试使用:
package rd.controller;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = "/home" , method = RequestMethod.GET)
public ModelAndView setUp(){
ModelAndView model = new ModelAndView("home");
**model.addObject("CurrentUser", SecurityContextHolder.getContext().getAuthentication().getName());**
return model;
}
}