在Spring中显示用户名

时间:2018-02-01 01:40:02

标签: spring spring-data-jpa thymeleaf

我可以登录并注销,并在页面上显示用户列表:

<li th:each="user : ${users}">
    <span th:text="${user.firstname}+' '+${user.lastname}"></span>
</li>

我现在只想显示当前登录用户的名称,但我不知道如何。我想将它添加到标题片段中,以便每个页面都清楚地显示用户登录的是谁。

LoginForm.java:

package com.demo.spring.domain;

import org.hibernate.validator.constraints.NotEmpty;

public class LoginForm {
    public String getAccountname() {
        return accountname;
    }

    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @NotEmpty
    String accountname;
    @NotEmpty
    String password;
}

的login.html:

<h3>User Login</h3>

<form action="#" th:action="@{/user/login}" th:object="${user}" method="post">
    <!--<input type="hidden" th:field="*{id}"/>-->
    <p>Account Name:</p> <input type="text" th:field="*{accountname}"/>
    <p>Password:</p> <input type="password" th:field="*{password}"/>
    <p/><input type="submit" value="Login"/>
</form>

<div th:if="${message!=null}">
    <br/>
    <span th:text="${message}"/>

</div>

用于登录的UserController代码:

package com.demo.spring.controller;

import com.demo.spring.domain.LoginForm;
import com.demo.spring.domain.User;
import com.demo.spring.domain.UserSearchForm;
import com.demo.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginView(Model model)
    {
        LoginForm user = new LoginForm();
        model.addAttribute("user", user);
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(Model model, @Valid @ModelAttribute("user") LoginForm user, BindingResult bindingResult, HttpSession session)
    {
        if(bindingResult.hasErrors())
        {
            model.addAttribute("user", user);
            model.addAttribute("message", "Please provide information in each field");
            return "login";
        }

        if (userService.validateLogin(user)==false)
        {
            model.addAttribute("user", user);
            model.addAttribute("message", "Your accountname and/or password are incorrect");
            return "login";
        }

        session.setAttribute("login", true);
        return "redirect:/";

    }

UserService

package com.demo.spring.service;

import com.demo.spring.domain.LoginForm;
import com.demo.spring.domain.UserSearchForm;
import com.demo.spring.domain.User;
import com.demo.spring.domain.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    public boolean validateLogin(LoginForm user)
    {
        List<User> users = userRepository.checkUserInput(user.getAccountname(),user.getPassword());
        return users !=null && users.size()>0;
    }

1 个答案:

答案 0 :(得分:1)

我们将登录用户的名字放在会话中

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Model model, @Valid @ModelAttribute("user") LoginForm user, BindingResult bindingResult, HttpSession session)
{
     ... 
     session.setAttribute("accountName", user.getAccountName());
     session.setAttribute("login", true);
     return "redirect:/";
}

一旦进入会话,会话变量就可以简单地作为${session.accountName}进行访问。因此,您可以在标题片段中使用<span th:text="${session.accountName}"></span>