<file_sep>package com.haya.entity;

import lombok.Data;

import javax.persistence.*;
import java.util.Collection;
import java.util.List;

/**
 * @author haya
 */
@Entity
@Table(name = "course")
@Data
public class Course {
    @Id
    @Column(name = "id")
    private int id;
    @Basic
    @Column(name = "name")
    private String name;
    @OneToMany(mappedBy = "course",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    private List<TeacherCourse> teacherCoursesById;

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder( "{" );
        sb.append( "\"id\":" )
                .append( id );
        sb.append( ",\"name\":\"" )
                .append( name ).append( '\"' );
        sb.append( '}' );
        return sb.toString();
    }
}
<file_sep>package com.haya.service;

import com.haya.entity.TeacherCourse;

import java.util.List;

/**
 * @author haya
 */
public interface TeacherCourseService {
    List<TeacherCourse> find(int teacherId);
}
<file_sep>package com.haya.service.impl;

import com.haya.bean.Page;
import com.haya.dao.CourseDao;
import com.haya.entity.Course;
import com.haya.entity.User;
import com.haya.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author haya
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;

    @Override
    public Page<Course> getAllCourse(Page<Course> page) {
        return courseDao.findPageByHql( "from Course course", page);
    }

    @Override
    public void updateOrSave(Course course) {
        if (course.getId() == 0) {
            courseDao.save( course );
        } else {
            courseDao.update( course );
        }
    }

    @Override
    public Page<Course> like(Page<Course> page, Course course) {
        page = courseDao.findPageByHql( "from Course c where c.name like '%" + course.getName()+"%'", page );
        return page;
    }
}
<file_sep>package com.haya.entity;

import lombok.Data;

import javax.persistence.*;

/**
 * @author haya
 */
@Data
@Entity
@Table(name = "stu_course")
public class StuCourse {
    @Id
    @Column(name = "id")
    private int id;
    @Column(name = "stu_id")
    private Integer stuId;
    @Column(name = "teacher_course_id")
    private Integer teacherCourseId;
    @Column(name = "result")
    private Integer result;
    @ManyToOne
    @JoinColumn(name = "stu_id", referencedColumnName = "id",insertable=false ,updatable=false)
    private User user;
    @ManyToOne
    @JoinColumn(name = "teacher_course_id", referencedColumnName = "id",insertable=false ,updatable=false)
    private TeacherCourse teacherCourse;

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder( "{" );
        sb.append( "\"id\":" )
                .append( id );
        sb.append( ",\"stuId\":" )
                .append( stuId );
        sb.append( ",\"teacherCourseId\":" )
                .append( teacherCourseId );
        sb.append( ",\"result\":" )
                .append( result );
        sb.append( ",\"user\":" )
                .append( user );
        sb.append( ",\"teacherCourse\":" )
                .append( teacherCourse );
        sb.append( '}' );
        return sb.toString();
    }
}
<file_sep>package com.haya.action;


import com.haya.bean.Page;
import com.haya.bean.VALUE;
import com.haya.entity.Course;
import com.haya.entity.StuCourse;
import com.haya.entity.User;
import com.haya.service.CourseService;
import com.haya.service.StudentCourseService;
import com.haya.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import static com.haya.bean.VALUE.*;

/**
 * @author haya
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ParentPackage("myStruts")
@Namespace("/")
public class UserAction extends SuperAction {
    @Autowired
    private UserService userService;
    private User user = new User();
    @Action(value = "login", results = {
        @Result(name = "admin", location = "/admin/getTeacherPage.action", type = "redirect"),
        @Result(name = "teacher", location = "/teacher/getTeacherCoursePage.action", type = "redirect"),
        @Result(name = "student", location = "/student/getStudentResult.action", type = "redirect"),
        @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String login() {
        user = userService.login( user );
        if (user == null) {
            return "error";
        }
        Integer roleId = user.getRoleId();
        session.put( ROLE_ID.toString(), roleId );
        session.put( LOGIN_USER_ACCOUNT.toString(), user.getAccount() );
        session.put( LOGIN_USER_ID.toString(), user.getId() );
        if (roleId == 2) {
            return "teacher";
        } else if (roleId == 3) {
            return "student";
        } else {
            return "admin";
        }
    }

    @Action(value = "logout", results = {
            @Result(name = "success", location = "/login.jsp", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String logout() {
        session.remove( "loginUserAccount" );
        return "success";
    }
}
<file_sep>package com.haya.action;

import com.haya.bean.Page;
import com.haya.entity.Course;
import com.haya.entity.User;
import com.haya.service.CourseService;
import com.haya.service.StudentCourseService;
import com.haya.service.TeacherCourseService;
import com.haya.service.UserService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author haya
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ParentPackage("myStruts")
@Namespace("/admin")
public class AdminAction extends SuperAction {
    @Autowired
    private UserService userService;
    @Autowired
    private StudentCourseService studentCourseService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherCourseService teacherCourseService;
    private User user = new User();
    private Page<User> page = new Page<>();
    private Page<Course> pageCourse = new Page<>();
    private Course course= new Course();

    /**
     * 管理员 分页获取学生
     *
     * @return
     */
    @Action(value = "getStudentPage", results = {
            @Result(name = "success", location = "/admin/admin_student.jsp", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String getStudentPage() {
        Page<User> list = userService.getAllStudent( 1, 10 );
        System.out.println( list );
        if (list != null) {
            session.put( "studentPage", list );
            return "success";
        }
        return "error";
    }

    /**
     * 管理员 分页获取老师
     *
     * @return
     */
    @Action(value = "getTeacherPage", results = {
            @Result(name = "success", location = "/admin/admin_teacher.jsp", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String getTeacherPage() {
        if (page.getPageNum() == 0) {
            page.setPageNum( 1 );
            page.setPageSize( 10 );
        }
        Page<User> list = userService.getAllTeacher( page );
        if (list != null) {
            System.out.println( list );
            session.put( "teacherList", list );
            return "success";
        }

        return "error";
    }

    /**
     * 管理员 更新用户
     *
     * @return
     */
    @Action(value = "updateUser", results = {
            @Result(name = "teacher", location = "/admin/getTeacherPage.action", type = "redirect"),
            @Result(name = "student", location = "/admin/getStudentPage.action", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String updateUser() {
        userService.updateOrSave( user );
        Integer roleId = user.getRoleId();
        return roleId == 2 ? "teacher"
                : roleId == 3 ? "student"
                : "error";
    }

    /**
     * 管理员 删除用户
     *
     * @throws IOException
     */
    @Action(value = "removeUser")
    public void removeUser() throws IOException {
        userService.remove( user );
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType( "text/html;charset=UTF-8" );
        PrintWriter out = response.getWriter();
        out.println( "seccess" );
        out.close();
    }

    @Action(value = "searchUser", results = {
            @Result(name = "teacher", location = "/admin/admin_teacher.jsp", type = "redirect"),
            @Result(name = "student", location = "/admin/admin_student.jsp", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String searchUser() {
        if (page.getPageNum() == 0) {
            page.setPageNum( 1 );
            page.setPageSize( 10 );
        }
        Page<User> list = userService.like( page, user );
        if (user.getRoleId() == 2) {
            session.put( "teacherList", list );
            return "teacher";
        } else {
            session.put( "studentPage", list );
            return "student";
        }
    }


    /**
     * 管理员 分页获取课程
     * @return
     */
    @Action(value = "getCoursePage", results = {
            @Result(name = "success", location = "/admin/admin_course.jsp", type = "redirect"),
            @Result(name = "error", location = "/login.jsp", type = "redirect")
    })
    public String getCoursePage() {
        if (pageCourse.getPageNum() == 0) {
            pageCourse.setPageNum( 1 );
            pageCourse.setPageSize( 10 );
        }
        Page<Course> list = courseService.getAllCourse( pageCourse );
        System.out.println(list);
        if (list != null) {
            session.put( "coursePage", list );
            return "success";
        }
        return "error";
    }

    /**
     * 管理员更新课程
     * @return
     */
    @Action(value = "updateCourse", results = {
            @Result(name = "success", location = "/admin/getCoursePage.action", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String updateCourse() {
        courseService.updateOrSave( course );
        return "success";
    }


    @Action(value = "searchCourse", results = {
            @Result(name = "success", location = "/admin/admin_course.jsp", type = "redirect"),
            @Result(name = "error", location = "/error.jsp", type = "redirect")
    })
    public String search() {
        if (page.getPageNum() == 0) {
            page.setPageNum( 1 );
            page.setPageSize( 10 );
        }
        Page<Course> list = courseService.like( pageCourse, course );
        session.put( "coursePage", list );
        return "success";
    }
}
<file_sep>package com.haya.service;

import com.haya.bean.Page;
import com.haya.entity.StuCourse;
import com.haya.entity.TeacherCourse;
import com.haya.entity.User;

import java.util.List;

/**
 * @author haya
 */
public interface UserService {
    User getById(int id);

    User getByNo(int no);

    User login(User user);

    Page<User> getAllStudent(int pageNum, int pageSize);

    List<StuCourse> getStudentByCourse(TeacherCourse tc);

    Page<User> getAllTeacher(Page<User> page);

    void remove(User user);

    void update(User user);

    void updateOrSave(User user);

    Page<User> like(Page<User> page,User user);

}
<file_sep>package com.haya.service.impl;

import com.haya.bean.Page;
import com.haya.dao.StuCourseDao;
import com.haya.dao.TeacherCourseDao;
import com.haya.dao.UserDao;
import com.haya.entity.StuCourse;
import com.haya.entity.TeacherCourse;
import com.haya.entity.User;
import com.haya.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author haya
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private TeacherCourseDao teacherCourseDao;
    @Autowired
    private StuCourseDao stuCourseDao;

    @Override
    public User getById(int id) {
        return userDao.findById( id ) ;
    }

    @Override
    public User getByNo(int no) {
        return userDao.findByColumn( "no", no );
    }

    @Override
    public User login(User user) {
        List<User> list = userDao.getHibernateTemplate().findByExample( user );
        if (list != null && list.size() != 0) {
            return list.get( 0 );
        }
        return null;
    }

    @Override
    public Page<User> getAllStudent(int pageNum, int pageSize) {
        return userDao.findPageByHql( "from User user where user.roleId=3", new Page<>( pageNum, pageSize ) );
    }

    @Override
    public List<StuCourse> getStudentByCourse(TeacherCourse tc) {
        if (tc.getId() == 0) {
            tc = teacherCourseDao.find( tc );
        }
        System.out.println( tc );
        List<StuCourse> list = stuCourseDao.findListByColumn( "teacherCourseId", tc.getId() );
        return list;
    }

    @Override
    public Page<User> getAllTeacher(Page<User> page) {
        return userDao.findPageByHql( "from User user where user.roleId=2", page );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(User user) {
        userDao.deleteById( user.getId() );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(User user) {
        userDao.update( user );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOrSave(User user) {
        if (user.getId() == 0) {
            userDao.save( user );
        } else {
            userDao.update( user );
        }
    }

    @Override
    public Page<User> like(Page<User> page, User user) {
        page = userDao.findPageByHql( "from User u where u.no like '%" + user.getNo()+"%' and u.roleId="+user.getRoleId(), page );
        return page;
    }
}
<file_sep>package com.haya.interceptor;

import com.opensymphony.xwork2.ActionContext;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

import static com.haya.bean.VALUE.ROLE_ID;

/**
 * @author haya
 */
@WebFilter(filterName = "FilterDemo01", urlPatterns = { "/*" })
public class JspFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String uri = request.getRequestURI();
        String root = request.getContextPath() + "/";
        System.out.println("filter:");
        System.out.println(uri);
        Map<String, Object> session = ActionContext.getContext().getSession();
        // 访问非登陆页面 且 未登录
        if (!uri.contains( "login" ) && session.get( "loginUserAccount" ) == null) {
            response.sendRedirect( root + "login.jsp" );
            return;
        }
        // 访问登陆页面 且 已登录
        if (uri.contains( "login" ) && session.get( "loginUserAccount" ) != null) {
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId == 1) {
                response.sendRedirect( root + "admin/admin_teacher.jsp" );
            } else if (roleId == 2) {
                response.sendRedirect( root + "teacher/index.jsp" );
            } else if (roleId == 3){
                response.sendRedirect( root + "student/index.jsp" );
            }
            return;
        }
        if (uri.contains( "admin" )) {
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId != 1) {
                response.sendRedirect( root + "error.jsp" );
                return;
            }

        } else if (uri.contains( "teacher" )) {
            System.out.println("teacher");
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId != 2) {
                response.sendRedirect( root + "error.jsp" );
                return;
            }

        } else if (uri.contains( "student" )) {
            System.out.println("student");
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId != 3) {
                response.sendRedirect( root + "error.jsp" );
                return;
            }

        }
        filterChain.doFilter( request, response );
    }

    @Override
    public void destroy() {

    }
}
<file_sep>package com.haya.interceptor;

import com.haya.bean.VALUE;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

import static com.haya.bean.VALUE.LOGIN_USER_ACCOUNT;
import static com.haya.bean.VALUE.ROLE_ID;

/**
 * @author haya
 */

public class SessionInterceptor extends SuperInterceptor {

    private final String loginPage = "login";

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        super.intercept( invocation );
        if (loginPage.equals( ActionContext.getContext().getName() )) {
            return invocation.invoke();
        }
        if (session.get( LOGIN_USER_ACCOUNT.toString() ) == null) {
            return "error";
        }
        System.out.println( invocation.getProxy().getNamespace() );
        if (invocation.getProxy().getNamespace().contains( "admin" )) {
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId != 1) {
                return "error";
            }
        } else if ( invocation.getProxy().getNamespace().contains( "teacher" )) {
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId != 2) {
                return "error";
            }

        } else if ( invocation.getProxy().getNamespace() .contains( "student" )) {
            int roleId = (int) session.get( ROLE_ID.toString() );
            if (roleId != 3) {
                return "error";
            }
        }
        return invocation.invoke();
    }
}
<file_sep>hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
jdbc.url=jdbc:mysql:///cjgl?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=<PASSWORD>
