登录功能没有实现
登录功能没有实现,点击登录后还是跳转到了登录页面
package com.imooc.bbs.entity;
public class User {
private Integer id;
private String username;
private String password;
private String email;
private Integer userStatus;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getUserStatus() {
return userStatus;
}
public void setUserStatus(Integer userStatus) {
this.userStatus = userStatus;
}
}
package com.imooc.bbs.dao;
import com.imooc.bbs.entity.User;
import java.util.List;
public interface UserDao {
void insert(User user);
void update(User user);
List<User> selectAll();
List<User> selectById(Integer id);
// 根据用户名选取用户
User selectByUsername(String username);
}
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.bbs.dao.UserDao">
<resultMap id="user" type="User">
<id property="id" column="id" javaType="Integer"/>
<result property="username" column="username" javaType="String"/>
<result property="password" column="password" javaType="String"/>
<result property="email" column="email" javaType="String"/>
<result property="userStatus" column="userStatus" javaType="Integer"/>
</resultMap>
<insert id="insert" parameterType="User" useGeneratedKeys="true">
insert into user (username, password, email, userStatus)
values (#{username}, #{password}, #{email}, #{userStatus})
</insert>
<update id="update" parameterType="User">
update user
set username=#{username},
password=#{password},
email=#{email},
userStatus=#{userStatus}
where id = #{id}
</update>
<select id="selectAll" resultMap="user">
select id, username, email, userStatus
from user
</select>
<select id="selectById" resultMap="user">
select id, password, username, email, userStatus
from user
where id = #{id}
</select>
<select id="selectByUsername" resultType="user">
select id, password, username, email, userStatus
from user
where username = #{username}
</select>
</mapper>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.imooc.bbs.dao"/>
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
<!--注意:端口后修改数据库名称-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/bbs?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&userSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1111"/>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--注意:entity前项目名修改-->
<property name="typeAliasesPackage" value="com.imooc.bbs.entity"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!--注意:dao包前项目名修改-->
<property name="basePackage" value="com.imooc.bbs.dao"/>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="spring-dao.xml"/>
<!--注意修改biz前的项目名-->
<context:component-scan base-package="com.imooc.bbs.biz"/>
<aop:aspectj-autoproxy/>
<bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transationManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--注意修改biz前的项目名-->
<aop:pointcut id="txpc" expression="execution(* com.imooc.bbs.biz.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>
</beans>
package com.imooc.bbs.biz;
import com.imooc.bbs.entity.User;
import java.util.List;
public interface UserBiz {
void add(User user);
void update(User user);
List<User> getAll();
List<User> selectById(Integer id);
//用户登录
User login(String username,String password);
}
package com.imooc.bbs.biz.impl;
import com.imooc.bbs.biz.UserBiz;
import com.imooc.bbs.dao.UserDao;
import com.imooc.bbs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userBiz")
public class UserBizImpl implements UserBiz {
@Qualifier("UserDao")
private UserDao userDao;
@Autowired
public UserBizImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void add(User user) {
user.setUserStatus(0);
userDao.insert(user);
}
@Override
public void update(User user) {
userDao.update(user);
}
@Override
public List<User> getAll() {
return userDao.selectAll();
}
@Override
public List<User> selectById(Integer id) {
return userDao.selectById(id);
}
@Override
public User login(String username, String password) {
User user = userDao.selectByUsername(username);
if (user!=null&&user.getPassword().equals(password)) {
return user;
}
return null;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>慕课论坛</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/frontstatic/static/css/all-df86af5803.css">
</head>
<body class="forum" data-page="forum">
<div class="header">
<nav class="navbar navbar-inverse navbar-fixed-top navbar-default">
<div class="container">
<div class="navbar-header" id="navbar-header">
<a href="/bbs/list.do" class="navbar-brand"><img src="${pageContext.request.contextPath}/frontstatic/images/logo.png">
</a>
</div>
<div id="main-nav-menu">
<ul class="nav navbar-nav">
<li class="active"><a href=""><i class="fa fa-home"></i> <span
class="hidden-xs hidden-sm">首页</span></a></li>
<li class="hidden-sm hidden-xs"><a href="thread.html"><i class="fa fa-comments-o"></i> 话题</a></li>
</ul>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="nav-search hidden-xs hidden-sm">
<form method="GET" action="###" accept-charset="UTF-8"
class="navbar-form form-search active" target="_blank">
<div class="form-group">
<input placeholder="搜索" class="form-control" name="q" type="search">
</div>
<i class="fa fa-search"></i>
</form>
</li>
<!-- 登录成功要显示的内容 -->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
imooc1 <!-- 此处显示用户名 -->
<span class="caret"></span></a>
<button class="navbar-toggle" type="button" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="sr-only">Toggle</span>
<i class="fa fa-reorder"></i>
</button>
<ul class="dropdown-menu" role="menu"><li class=""><a href="my.html">我的主页</a></li>
<li><div class='divider'></div></li>
<li><a href="my.html">个人设置</a></li>
<li><a href="score.html">我的积分</a></li>
<li class='divider'></li>
<li><a href="###" onclick=" return confirm('你确定要退出吗?')"><i class="fa fa-sign-out"></i> 退出
</a></li>
</ul>
</li>
<!-- 未登录显示的内容 -->
<li><a href="/user/to_register" id="signup-btn">注册</a></li>
<li><a href="/user/to_login" id="login-btn">登录</a></li>
</ul>
</div>
</nav>
</div>
<div id="main" class="main-container container">
<!-- 首页通栏(4个推荐位) -->
<div class="col-md-9 threads-index main-col">
<div class="panel panel-default">
<div class="panel-heading">
<div class="pull-left hidden-sm hidden-xs">
<i class="fa fa-list"></i> 首页
</div>
<ul class="pull-right list-inline remove-margin-bottom thread-filter">
<li>
<a href="thread.html">
<i class="fa fa-history"></i> 最新话题
</a>
</li>
<li>
<a href="thread.html">
<i class="fa fa-diamond"> </i> 优质帖子
</a>
</li>
<li>
<a href="thread.html">
<i class="fa fa-thumbs-o-up"> </i> 最多按赞
</a>
</li>
<li>
<a href="thread.html">
<i class="fa fa-eye"></i> 等待回复
</a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="panel-body remove-padding-horizontal">
<ul class="list-group row thread-list">
<!-- 显示帖子,每个li显示一个帖子的内容 -->
<c:forEach items="${list}" var="post">
<li class="list-group-item media " style="margin-top: 0px;">
<a class="pull-right" href="post.html"><span class="badge badge-reply-count"> 25 </span>
</a>
<div class="avatar pull-left">
<a href="post.html">
<img class="media-object img-thumbnail avatar-48" alt="imooc" src="${pageContext.request.contextPath}/frontstatic/images/avatar.jpg"/>
</a>
</div>
<div class="infos">
<div class="media-heading">
<i class="fa fa-thumb-tack text-danger"></i>
<a href="/bbs/getPost.do?id" title="<!-- 此处存放帖子标题的内容 -->">
<%-- <a href="/bbs/getPost.do?id=<%=post.getId()%>" title="<!-- 此处存放帖子标题的内容 -->">--%>
${post.title} <!-- 此处显示帖子标题 -->
</a>
</div>
<div class="media-body meta">
<a href="post.html">
<span class="fa fa-thumbs-o-up"> 10 </span>
</a>
<span> • </span>
<span>回复</span>
<a href="post.html">
用户名
</a>
<span> • </span>
<span class="timeago " data-toggle="tooltip" data-placement="top"
title="2017-02-08 21:47:25"><spring:eval expression="post.createTime"/></span>
</div>
</div>
</li>
</c:forEach>
</ul>
</div>
<div class="panel-footer text-right remove-padding-horizontal pager-footer">
<!-- Pager -->
<!-- 如有需要可以在此处添加分页,本次作业不要求完成 -->
<ul class="pagination">
<li> <a href=""><span></span></a></li>
</ul>
</div>
</div>
</div>
<div class="col-md-3 side-bar">
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">BBS</h3>
</div>
<!-- 登录后显示的内容 -->
<c:if test="${sessionScope.user!=null}">
<div class="panel-body text-center">
<a href="" class="btn btn-primary">
<i class="fa fa-user"> </i> 发帖
</a>
</div>
</c:if>
<!-- 未登录显示的内容 -->
<c:if test="${sessionScope.user==null}">
<div class="panel-body text-center">
<a href="/user/to_register" class="btn btn-primary">
<i class="fa fa-user"> </i> 注册
</a>
</div>
</c:if>
<div class="panel-footer text-center">
已注册用户请 <a href="/user/to_login">登录</a>
</div>
</div>
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">热门标签</h3>
</div>
<div class="panel-body">
<div class="badge badge-tag-cloud">
<a href="">Java</a> <a href="">Python</a>
</div>
</div>
</div>
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">小贴士</h3>
</div>
<div class="panel-body">
猪有猪的思想,人有人的思想。如果猪有人的思想,那它就不是猪了——是八戒!
</div>
</div>
<div class="panel panel-default corner-radius">
<div class="panel-heading">
<h3 class="panel-title">统计信息</h3>
</div>
<ul class="list-group">
<li class="list-group-item">社区会员: 747</li>
<li class="list-group-item">主题数: 95</li>
<li class="list-group-item">回帖数: 218</li>
</ul>
</div>
</div>
<div class="clearfix"></div>
</div>
<footer class="footer">
<div class="container">
<div class="copyright">
<blockquote class="pull-left">
<p>慕课论坛</p>
<p>Copyright © 2015-2016 <a href="index.html">imooc</a> 1.0.0-rc2 <span class="pipe">|</span><span class="pipe">|</span></p>
</blockquote>
</div>
<div class="pull-right hidden-sm hidden-xs">
<p>
<a href="###">关于我们</a>
<span class="pipe">|</span>
<a href="###">联系我们</a>
<span class="pipe">|</span>
<a href="###">常见问题解答</a>
</p>
<p>
<a href="index.html" target="_blank"><img src="${pageContext.request.contextPath}/front/images/imooc-logo.png" border="0" height="40"
data-toggle="tooltip" data-placement="top"
title="Powered by imooc"/></a>
</p>
</div>
</div>
</footer>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>慕课论坛</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/frontstatic/static/css/all-df86af5803.css">
</head>
<body class="forum" data-page="forum">
<div class="header">
<nav class="navbar navbar-inverse navbar-fixed-top navbar-default">
<div class="container">
<div class="navbar-header" id="navbar-header">
<a href="index.html" class="navbar-brand"><img src="${pageContext.request.contextPath}/frontstatic/images/logo.png">
</a>
</div>
<div id="main-nav-menu">
<ul class="nav navbar-nav">
<li ><a href="index.html"><i class="fa fa-home"></i> <span class="hidden-xs hidden-sm">首页</span></a></li>
<li class="hidden-sm hidden-xs"><a href="thread.html"><i class="fa fa-comments-o"></i> 话题</a></li>
</ul>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="nav-search hidden-xs hidden-sm">
<form method="GET" action="/user/login" accept-charset="UTF-8" class="navbar-form form-search active" target="_blank">
<div class="form-group">
<input placeholder="搜索" class="form-control" name="q" type="search">
</div>
<i class="fa fa-search"></i>
</form>
</li>
<li ><a href="register.html" id="signup-btn">注册</a></li>
<li class="active"><a href="login.html" id="login-btn">登录</a></li>
</ul>
</div>
</nav>
</div> <div id="main" class="main-container container">
<div class="container-fluid">
<div class="row">
<div class="col-md-5 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">登录</div>
<div class="panel-body">
<form role="form" method="POST" action="">
<div class="form-group">
<input type="login" class="form-control" name="username" value="" placeholder="用户名">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="密码">
</div>
<div class="form-group">
<input type="submit" name="commit" value="登录" class="btn btn-primary btn-lg btn-block">
</div>
</form>
</div>
<div class="panel-footer">
<a href="">注册</a>
</div>
</div>
</div>
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-heading">用其他平台的帐号登录</div>
<ul class="list-group">
<li class="list-group-item">
<a href="###" class="btn btn-default btn-lg btn-block"><i class="fa fa-github"></i> Github</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<div class="copyright">
<blockquote class="pull-left">
<p>慕课论坛</p>
<p>Copyright © 2015-2016 <a href="index.html">imooc</a> 1.0.0-rc2 <span class="pipe">|</span><span class="pipe">|</span></p>
</blockquote>
</div>
<div class="pull-right hidden-sm hidden-xs">
<p>
<a href="###">关于我们</a>
<span class="pipe">|</span>
<a href="###">联系我们</a>
<span class="pipe">|</span>
<a href="###">常见问题解答</a>
</p>
<p>
<a href="index.html" target="_blank"><img src="images/imooc-logo.png" border="0" height="40" data-toggle="tooltip" data-placement="top" title="Powered by imooc" /></a>
</p>
</div>
</div>
</footer>
</body>
</html>
package com.imooc.bbs.controller;
import com.imooc.bbs.biz.UserBiz;
import com.imooc.bbs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
@Controller("userController")
@RequestMapping("/user")
public class UserController {
private UserBiz userBiz;
@Autowired
public UserController(UserBiz userBiz) {
this.userBiz = userBiz;
}
@RequestMapping("/list")
public String list(Map<String, Object> map) {
map.put("list", userBiz.getAll());
return "/admin/users";
}
//删除用户,将用户状态设备为2,不实际删除用户
@RequestMapping("/delete")
public String delete(int id) {
List<User> user = userBiz.selectById(id);
user.get(0).setUserStatus(2);
userBiz.update(user.get(0));
return "redirect:list";
}
//锁定用户,将用户的状态设置为1
@RequestMapping("/lock")
public String lock(int id) {
List<User> user = userBiz.selectById(id);
user.get(0).setUserStatus(1);
userBiz.update(user.get(0));
return "redirect:list";
}
//解锁用户,将用户的状态设置为正常的0
@RequestMapping("/unlock")
public String unlock(int id) {
List<User> user = userBiz.selectById(id);
user.get(0).setUserStatus(0);
userBiz.update(user.get(0));
return "redirect:list";
}
@RequestMapping("/to_register")
public String toRegister() {
return "/front/register";
}
@RequestMapping("/register")
public String register(User user) {
userBiz.add(user);
return "redirect:list";
}
@RequestMapping("/to_login")
public String toLogin() {
return "/front/login";
}
@RequestMapping("login")
public String login(HttpSession session, @RequestParam String username, @RequestParam String password) {
User user = userBiz.login(username, password);
if (user == null) {
return "redirect:to_login";
}
session.setAttribute("user",user);
return "redirect:/post/list";
}
}
package com.imooc.bbs.controller;
import com.imooc.bbs.biz.PostBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller("postController")
@RequestMapping("/post")
public class PostController {
private PostBiz postBiz;
@Autowired
public PostController(PostBiz postBiz){
this.postBiz=postBiz;
}
@RequestMapping("/list")
public String list(Map<String,Object> map) {
map.put("list",postBiz.getAll());
return "/front/list";
}
}
14
收起
正在回答 回答被采纳积分+1
2回答
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星