/**
* factory包,工厂类包
* EntityFactory:用于从HttpServletRequest生成实体类对象的工厂
* OperationFactory:用于生成Operation类的工厂
*/
package factory;
import javax.servlet.http.HttpServletRequest;
import util.Constant;
import entity.*;
public class EntityFactory {
//组装的工艺类型entityType+组装的材料来源request
public static Object createEntityFromRequest(String entityType,HttpServletRequest request){
String id=request.getParameter("entityId");
if(entityType.equals("User")){
User user=new User();
if(id==null||"".equals(id)){//属于add
user.setUserId(-1);
}else{//属于edit
user.setUserId(Integer.parseInt(id));
}
user.setUserPassword(request.getParameter("userPassword"));
user.setUserName(request.getParameter("userName"));
Role role=new Role();
role.setRoleId(Integer.parseInt(request.getParameter("userRole")));
user.setUserRole(role);
return user;
}
else if(entityType.equals("Course")){
Course course=new Course();
if(id==null||"".equals(id)){//属于add
course.setCourseId(-1);
}else{//属于edit
course.setCourseId(Integer.parseInt(id));
}
course.setCourseName(request.getParameter("courseName"));
course.setCourseUser((User)request.getSession().getAttribute("sessionUser"));
return course;
}
else if(entityType.equals("Work")){
Work work=new Work();
if(id==null||"".equals(id)){//属于add
work.setWorkId(-1);
}else{//属于edit
work.setWorkId(Integer.parseInt(id));
}
Course course=new Course();
String temp=request.getParameter("workCourse");
course.setCourseId(Integer.parseInt(temp));
work.setWorkCourse(course);
work.setWorkTitle(request.getParameter("workTitle"));
work.setWorkTime(Constant.getDate());
return work;
}
else if(entityType.equals("Job")){
//不管是老师批阅,还是学生做作业,均为修改
Job job=new Job();
job.setJobId(Integer.parseInt(id));
//只会用到jobContent、jobScore
job.setJobContent(request.getParameter("jobContent"));
job.setJobScore(request.getParameter("jobScore"));
return job;
}
else if(entityType.equals("Lesson")){//选课,都是新增
Lesson lesson=new Lesson();
lesson.setLessonId(-1);
Course course=new Course();
course.setCourseId(Integer.parseInt(request.getParameter("courseId")));
lesson.setLessonCourse(course);
return lesson;
}
return null;
}
}
package factory;
import inter.IOperation;
import operation.*;
public class OperationFactory {
public static IOperation createOperation(String entityType){
if(entityType.equals("User")){
return new UserOperation();
}else if(entityType.equals("Role")){
return new RoleOperation();
}else if(entityType.equals("Work")){
return new WorkOperation();
}else if(entityType.equals("Job")){
return new JobOperation();
}else if(entityType.equals("Course")){
return new CourseOperation();
}else if(entityType.equals("Lesson")){
return new LessonOperation();
}
return null;
}
}
/**
* inter包:接口包,用于保存所有接口interface
* IAction接口:定义Aciton动作类的规范
* IOperation接口:定义Operation数据库实体操作类的规范
*/
package inter;
import java.util.Map;
import exception.MyException;
public interface IAction {
//执行动作,并返回一些结果
public Map<String, Object> execute()throws MyException;
}
package inter;
import java.util.List;
//规范了用于执行对象对应数据库表基本操作的类
public interface IOperation {
public int selectCount();//获取个数
public List selectAll();//选取表中所有数据
public List selectPage(int offset,int size);//分页显示,第一个参数为起始位置(按id排序),第二个参数为每页显示个数
public Object selectById(int id);//按id获取一条记录
public int add(Object obj);//添加一条数据
public int deleteById(int id);//按id删除一条记录
public int update(Object obj);//按obj对象的信息修改一条记录(以obj的id标记需要修改的记录)
}
/**
* servlet包包含ActionServlet:项目唯一控制器
* ActionController:控制逻辑类
*/
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import action.Action;
import exception.MyException;
import java.util.Iterator;
import java.util.Map;
//ActionServlet作为整个项目唯一的Servlet
public class ActionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);//doGet与doPost一样处理
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//输入输出格式设置
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//根据不同的ActionContext创建不懂的action动作
Action action=ActionController.assemblyAction(request);
Map<String,Object> map=null;
try {
map=action.execute();//执行动作并返回结果
} catch (MyException ex) {//如果有异常,跳转异常提示页面
request.setAttribute("tipInfo", ex.getInfo());
request.getRequestDispatcher("/tip.jsp").forward(request,response);
return;
}
//设置返回页面
request.setAttribute("actionUrl",action.getActionUrl());
//取上下文中的返回值
if(map!=null){
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
if(key.toString().startsWith("session")){
request.getSession().setAttribute(key.toString(), val);
}else{
request.setAttribute(key.toString(), val);
}
}
}
//跳转到index.jsp主页面,注意 actionUrl指向页面显示在index.jsp右边内容区域
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
}
package servlet;
import javax.servlet.http.HttpServletRequest;
import entity.User;
import factory.EntityFactory;
import action.*;
/**
* 实质的控制器
* @author 猫哥
* @date 2017.2.11
* @modify 2017.2.19 去除context改由Action自行决定需要参数列表
* ActionController此时只需负责根据Action需求提供参数即可
*/
public class ActionController {
//直接装配出Action
public static Action assemblyAction(HttpServletRequest request){
//两个必备参数
String operationType=request.getParameter("method");
String entityType=request.getParameter("entityType");
//根据Action需要装配
if(operationType.equals("delete")){
String entityId=request.getParameter("entityId");
//第三个参数null表示交给Action去设置
return new DeleteAction(entityType,operationType,Integer.parseInt(entityId));
}
else if(operationType.equals("view")){
//所有view都分页
String inputPage=request.getParameter("page");
int page=-1;
if(inputPage==null||"".equals(inputPage)){
page=1;
}else{
page=Integer.parseInt(inputPage);
}
//特殊查看逻辑
if(entityType.equals("Work")){//查看作业,需要课程编号
return new ViewWorkAction(entityType,operationType,page,Integer.parseInt(request.getParameter("byCourseId")));
}else if(entityType.equals("Course")){//查看课程,需要登录人员信息
return new ViewCourseAction(entityType,operationType,page,(User)request.getSession().getAttribute("sessionUser"));
}else if(entityType.equals("Job")){//查看作业,学生查看未做的,教师查看已做了未批阅的
return new ViewJobAction(entityType,operationType,page,(User)request.getSession().getAttribute("sessionUser"));
}
//正常查看
return new ViewAction(entityType,operationType,page);
}
else if(operationType.equals("add")){
if(entityType.equals("Work")){//添加作业时,需要携带教师创建的课程列表,此处只是需要登录用户身份即可
User user=(User)request.getSession().getAttribute("sessionUser");
return new AddWorkAction(entityType,operationType,user.getUserId());
}
return new AddAction(entityType,operationType);
}
else if(operationType.equals("edit")){
//点击编辑后,需要显示被编辑人的信息,所以edit要携带id
String entityId=request.getParameter("entityId");
return new EditAction(entityType,operationType,Integer.parseInt(entityId));
}
else if(operationType.equals("editJobContent")){//做作业
String entityId=request.getParameter("entityId");
return new EditJobContentAction(entityType,operationType,Integer.parseInt(entityId));
}
else if(operationType.equals("editJobScore")){//批阅作业
String entityId=request.getParameter("entityId");
return new EditJobScoreAction(entityType,operationType,Integer.parseInt(entityId));
}
else if(operationType.equals("save")){
//注意,点击保存后,不同的实体需要携带不同的参数列表,所以采用createEntityFromRequest来创建实体
Object saveEntity=EntityFactory.createEntityFromRequest(entityType, request);
Boolean isAddOrEdit=null;
//需要根据entityId判断是新增还是修改
String id=request.getParameter("entityId");
if(id==null||"".equals(id)){
isAddOrEdit=true;
}else{
isAddOrEdit=false;
}
//因为Save之后要跳到列表,有些列表需要根据登录用户身份显示,所以
User sessionUser=(User)request.getSession().getAttribute("sessionUser");
//注意因为work是跟课程相关的,修改work后要跳转到当前课程所有作业列表,所以添加该输入项
if(entityType.equals("Work")){
return new SaveWorkAction(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}else if (entityType.equals("Job")){
return new SaveJobAction(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}else if (entityType.equals("Lesson")){
return new SaveLessonAction(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}
return new SaveAction(entityType,operationType,isAddOrEdit,saveEntity,sessionUser);
}
else if(operationType.equals("login")){
return new LoginAction(entityType,operationType,request.getParameter("userId"),request.getParameter("userPassword"));
}
return null;
}
}
/**
* action包:包含所有动作类
* Action类是所有动作类抽象基类
* 具体动作类包含:AddAction AddWorkAction DeleteAction EditAction EditJobContentAction EditJobScoreAction
* LoginAction SaveAction SaveJobAction SaveLessonAction SaveWorkAction ViewAction ViewCourseAction
* ViewJobAction ViewWorkAction Action是业务核心,全部列出
*/
package action;
import inter.IAction;
/**
* 所有动作的抽象基类Action
* @author 猫哥
* @date 2017.2.19
* @modify 2017.2.19 to remove context
*/
public abstract class Action implements IAction{
//三个必备参数,采用protected便于子类使用
protected String entityType=null;
protected String operationType=null;
protected String actionUrl=null;
public Action(String entityType,String operationType){
this.entityType=entityType;
this.operationType=operationType;
}
public String getEntityType() {
return entityType;
}
public void setEntityType(String entityType) {
this.entityType = entityType;
}
public String getOperationType() {
return operationType;
}
public void setOperationType(String operationType) {
this.operationType = operationType;
}
public String getActionUrl() {
return actionUrl;
}
public void setActionUrl(String actionUrl) {
this.actionUrl = actionUrl;
}
}
package action;
import java.util.HashMap;
import java.util.Map;
import exception.MyException;
/***
* 动作:点击新增按钮后对应动作,直接跳转对应实体更新页面
* @author 猫哥
* @date 2017.2.9
* @modify 2017.2.19 to remove context
*/
public class AddAction extends Action{
public AddAction(String entityType,String operationType){
super(entityType,operationType);
}
@Override
public Map<String, Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
actionUrl=entityType.toLowerCase()+"Update.jsp";
return resultMap;
}
}
package action;
import java.util.Map;
import operation.CourseOperation;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:新增作业
* @author 猫哥
* @date 2017.2.17
* @modify 2017.2.19 to remove context
*/
public class AddWorkAction extends AddAction{
//AddWork不同与AddAction的参数
protected int loginUserId=-1;
public AddWorkAction(String entityType,String operationType,int loginUserId){
super(entityType,operationType);
this.loginUserId=loginUserId;
}
@Override
public Map<String, Object> execute() throws MyException {
//通过父类方法设置了actionUrl
Map<String,Object> resultMap=super.execute();
//注意新增作业时需要选择课程,所以返回信息里面含有教师所有课程信息
CourseOperation CourseOper=(CourseOperation)OperationFactory.createOperation("Course");
resultMap.put("courses",CourseOper.selectAllByUserId(loginUserId));
return resultMap;
}
}
package action;
import inter.IOperation;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:删除后对应动作
* @author 猫哥
* @date 2017.2.9
* @modify 2017.2.19 to remove context
*/
public class DeleteAction extends Action{
protected int deleteEntityId=-1;
public DeleteAction(String entityType,String operationType,int deleteEntityId){
super(entityType,operationType);
this.deleteEntityId=deleteEntityId;
}
@Override
public Map<String, Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
//按类型获取实体操作类
IOperation oper=OperationFactory.createOperation(entityType);
int affectedCount=oper.deleteById(deleteEntityId);
if(affectedCount<1){//删除失败咯
throw new MyException(new Date(),"DeleteAction执行失败,entityType:"+entityType,"删除失败");
}else{
resultMap.put("tipInfo", "删除成功");
actionUrl="tip.jsp";
return resultMap;
}
}
}
package action;
import java.util.HashMap;
import java.util.Map;
import inter.IOperation;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:点击修改按钮后对应动作
* @author 猫哥
* @date 2017.2.9
* @modify 2017.2.19 to remove context
*/
public class EditAction extends Action{
protected int editEntityId=-1;
public EditAction(String entityType,String operationType,int editEntityId){
super(entityType,operationType);
this.editEntityId=editEntityId;
}
@Override
public Map<String, Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
IOperation oper=OperationFactory.createOperation(entityType);
Object entity=oper.selectById(editEntityId);
resultMap.put(entityType.toLowerCase(), entity);
actionUrl=entityType.toLowerCase()+"Update.jsp";
return resultMap;
}
}
package action;
import java.util.HashMap;
import java.util.Map;
import inter.IOperation;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:学生做作业
* @author 猫哥
* @date 2017.2.20
*/
public class EditJobContentAction extends Action{
protected int editEntityId=-1;
public EditJobContentAction(String entityType,String operationType,int editEntityId){
super(entityType,operationType);
this.editEntityId=editEntityId;
}
@Override
public Map<String, Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
IOperation oper=OperationFactory.createOperation(entityType);
Object entity=oper.selectById(editEntityId);
resultMap.put(entityType.toLowerCase(), entity);
actionUrl="jobUpdateByStudent.jsp";
return resultMap;
}
}
package action;
import java.util.HashMap;
import java.util.Map;
import inter.IOperation;
import exception.MyException;
import factory.OperationFactory;
/***
* 动作:学生做作业
* @author 猫哥
* @date 2017.2.20
*/
public class EditJobScoreAction extends Action{
protected int editEntityId=-1;
public EditJobScoreAction(String entityType,String operationType,int editEntityId){
super(entityType,operationType);
this.editEntityId=editEntityId;
}
@Override
public Map<String, Object> execute() throws MyException {
Map<String,Object> resultMap=new HashMap<String,Object>();
IOperation oper=OperationFactory.createOperation(entityType);
Object entity=oper.selectById(editEntityId);
resultMap.put(entityType.toLowerCase(), entity);
actionUrl="jobUpdateByTeacher.jsp";
return resultMap;
}
}
package action;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import operation.UserOperation;
import util.Constant;
import entity.User;
import exception.MyException;
import factory.OperationFactory;
/**
* 登录动作
* @author Administrator
* @modify 2017.2.19 to remove context
*/
public class LoginAction extends Action{
protected String inputUserId=null;
protected String inputUserPassword=null;
public LoginAction(String entityType,String operationType,String inputUserId,String inputUserPassword){
super(entityType,operationType);
this.inputUserId=inputUserId;
this.inputUserPassword=inputUserPassword;
}
@Override
public Map<String, Object> execute() throws MyException {
UserOperation oper=(UserOperation)OperationFactory.createOperation(entityType);
int userId=-1;
try{
userId=Integer.parseInt(inputUserId);
}catch(Exception ex){
throw new MyException(new Date(),ex.toString(),"登录失败!");
}
User realUser=(User)oper.selectById(userId);
if(realUser.getUserId()!=0&&realUser.getUserPassword().equals(inputUserPassword)){
//设置返回参数
Map<String,Object> resultMap=new HashMap<String,Object>();
//如果需要Servlet将返回值放入session作用域,则此处命名时以session开头即可
resultMap.put("sessionUser", realUser);
//登录用户角色对应菜单
resultMap.put("sessionRoleMenu", Constant.RoleMenu.get(realUser.getUserRole().getRoleName()));
actionUrl="tip.jsp";
return resultMap;
}
else{
throw new MyException(new Date(),"LoginAction Error","登录失败!");
}
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦