SSM登录简单验证+实现用户名的显示

**

First:利用HttpSession设置对象。

**

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

    @Autowired
    private IUserService userService;

    @RequestMapping("/doLogin.do")
    public String doLogin(String username,String password,HttpSession session){
        //1.调用service,查询得到结果,自动补全返回值 Ctrl+Alt+V
        User user = userService.doLogin(username);

        //2.判断
        if(user != null){
            if (user.getPassword().equals(password)){
                System.out.println("密码正确,登录成功!");
                session.setAttribute("user",user);
                return "main";
            }else{
                System.out.println("密码错误,登陆失败!");
                session.setAttribute("error","密码错误,请重新登录!");
                return "../login";
            }
        }else{
            System.out.println("用户不存在,请重新输入!");
            session.setAttribute("error","用户名不存在,请重新输入!");
            return "../login";
        }
    }
}

**

Second:修改jsp参数页面(${对象名.方法})

**

<div class="user-panel">
			<div class="pull-left image">
				<img src="${pageContext.request.contextPath}/img/user2-160x160.jpg"
					class="img-circle" alt="User Image">
			</div>
			<div class="pull-left info">
				${user.username}&nbsp;<a href="#"><i class="fa fa-circle text-success"></i>在线</a>
			</div>
		</div>
			<li class="dropdown user user-menu"><a href="#"
					class="dropdown-toggle" data-toggle="dropdown"> <img
						src="${pageContext.request.contextPath}/img/user2-160x160.jpg"
						class="user-image" alt="User Image">${user.username} <span class="hidden-xs">
					</span>
				</a>
					<ul class="dropdown-menu">
						<!-- User image -->
						<li class="user-header"><img
							src="${pageContext.request.contextPath}/img/user2-160x160.jpg"
							class="img-circle" alt="User Image">${user.username}</li>

					 

**

Finally:登录测试页面

**
SSM登录简单验证+实现用户名的显示_第1张图片

你可能感兴趣的:(SSM,java,session)