具有重复语句的Servlet控制器

时间:2011-12-22 19:13:14

标签: java oop web-applications

我倾向于有这样的陈述

STKUser authenticatedUser = (STKUser) request.getSession().getAttribute("STKUserSession");

在我的类的每个方法中。 authenticatedUser用于授权检查/逻辑流程。这样可以,还是应该以不同的方式编写这个类?还有任何推荐的书籍可以帮助改进我的Java类的编码,如下面的那些,用于Web应用程序?我的大部分课程看起来都像下面那样。

public class TD0301AssignmentForm extends Form {
  private boolean notifyApprover = false;
  boolean employeeChange = false;

  public TD0301AssignmentForm(TD0301AssignmentDAO dao) {
    this.dao = dao;
  }


  private TD0301Assignment unlockAssignment(HttpServletRequest request) {
    STKUser authenticatedUser = (STKUser) request.getSession().getAttribute("STKUserSession");
    TD0301Assignment tdas = new TD0301Assignment();
    notifyApprover = true;
    boolean unlock = false;

    try {
        // get the original data
        tdas = dao.retreive(request.getParameter("calc_num"), request.getParameter("calc_rev"), request.getParameter("calc_dept"), authenticatedUser);

        if ("3".equals(tdas.getForm_approve_state()) && authenticatedUser.getBadge().equals(tdas.getOriginator())) {
            tdas.setForm_approve_state("1");
            notifyApprover = true;
            unlock = true;
        }

     }


public TD0301Assignment updateAssignment(HttpServletRequest request) {
    STKUser authenticatedUser = (STKUser) request.getSession().getAttribute("STKUserSession");
    .... 
    if (authenticatedUser.getBadge().equals(tdas.getOriginator())) {
        //do something
}

修改

使用这两个类访问TD0301AssignmentForm类。

的Servlet

TD0301AssignmentDAO dao = new TD0301AssignmentDAO();
TD0301AssignmentForm form = new TD0301AssignmentForm(dao);
TD0301Assignment obj = new TD0301Assignment();

String pkString = "calc_num=" + request.getParameter("calc_num") + "&calc_rev=" + request.getParameter("calc_rev") + "&calc_dept="
        + request.getParameter("calc_dept");

modelMap.put("dbTable", dbTable);
modelMap.put("action", request.getRequestURL());
modelMap.put("reportTitle", "CommitmentReport");

// I think this is the Application Controller Strategy
actionMap.put(null, new ListAction(modelMap, form, "WEB-INF/views/genericList_v.jsp", "WEB-INF/views/genericList_v.jsp"));
actionMap.put("list", new ListAction(modelMap, form, "WEB-INF/views/genericList_v.jsp", "WEB-INF/views/genericList_v.jsp"));
actionMap.put("view", new ViewAction(modelMap, form, obj, "WEB-INF/views/genericView_v.jsp", "WEB-INF/views/genericView_v.jsp"));
actionMap.put("delete", new DeleteAction(modelMap, form, obj, "WEB-INF/views/genericDeleteConfirm_v.jsp", "WEB-INF/views/genericView_v.jsp"));
actionMap.put("sqlConfirmDelete", new DeleteConfirmAction(form, request.getRequestURL() + "?message=Deletion was successful!", request.getRequestURL()
        + "?method=view&" + pkString));
actionMap.put("edit", new EditAction(modelMap, form, obj, "WEB-INF/views/genericEdit_v.jsp", "WEB-INF/views/genericView_v.jsp"));
actionMap.put("sqlUpdate", new UpdateAction(modelMap, form, obj, request.getRequestURL() + "?message=Update was successful!", "WEB-INF/views/genericEdit_v.jsp"));
actionMap.put("new", new NewAction(modelMap, form, "WEB-INF/views/genericAdd_v.jsp"));
actionMap.put("sqlInsert", new InsertAction(modelMap, form, obj, request.getRequestURL() + "?message=Insert was successful!", "WEB-INF/views/genericAdd_v.jsp"));

String op = request.getParameter("method");
ControllerAction action = (ControllerAction) actionMap.get(op);

if (action != null) {
    action.service(request, response);
} else {
    String url = "WEB-INF/views/errorMessage_v.jsp";
    String errMessage = "Operation '" + op + "' not a valid for in '" + request.getServletPath() + "' !!";
    request.setAttribute("message", errMessage);
    request.getRequestDispatcher(url).forward(request, response);
}

public class EditAction implements ControllerAction {

private Form form;
private Object obj;
private String xPage;
private String yPage;
private HashMap modelMap;

public EditAction(HashMap modelMap, Form form, Object obj, String yPage, String xPage) {
    this.form = form;
    this.obj = obj;
    this.xPage = xPage;
    this.yPage = yPage;
    this.modelMap = modelMap;
}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    obj = form.edit(request);

    Iterator it = modelMap.entrySet().iterator(); 
    while (it.hasNext()) { 
        Map.Entry pairs = (Map.Entry)it.next(); 
        request.setAttribute(pairs.getKey().toString(), pairs.getValue());       
    }

    request.setAttribute("obj", obj);
    request.setAttribute("form", form);

    if (form.isSucces()) {
        RequestDispatcher view = request.getRequestDispatcher(yPage);
        view.forward(request, response);
    }
    else {
        RequestDispatcher view = request.getRequestDispatcher(xPage);
        view.forward(request, response);
    }
}

}

1 个答案:

答案 0 :(得分:1)

如果您发现自己一直在检索相同的值,则可能至少希望将其抽象为基类中的方法:

public class BaseForm extends WhateverYouHave {
    public STKUser getUser(HttpServletRequest request) {
        return request.getSession().getAttribute("STKUserSession");
    }
    ...
}

...

public class AnotherServlet extends BaseForm {
    public TD0301Assignment updateAssignment(HttpServletRequest request) {
        if (getUser(request).equals(tdas.getOriginator())) {
        ...

另一种可能更清洁的选择取决于您的调度/实例化/等。机制是将价值注入你的表格(如果他们不是单身,不清楚):

public class AnotherServlet extends BaseForm {
    public AnotherServlet(STKUser user) {
        this.user = user;
        ...
    }

    public TD0301Assignment updateAssignment(HttpServletRequest request) {
        if (user.equals(tdas.getOriginator())) {
        ...

或者将其作为形成方法的参数(如果是):

public TD0301Assignment updateAssignment(STKUser user, HttpServletRequest request) {
    if (user.equals(tdas.getOriginator())) {
        ...

很遗憾,您的表单直接与servlet规范绑定;没有这个要求,尽可能多地进行开发会更愉快。