【问题标题】:Java Servlet Front ControllerJava Servlet 前端控制器
【发布时间】:2015-07-19 06:17:13
【问题描述】:

我对这种模式Design Patterns web based applications 有点困惑。 我正在尝试开发简单的登录示例。我有登录成功的表单和 home.jsp 的 login.jsp。

在 ActionFactory 类中,我登录了地图和返回主页的 LoginAction。我的 ActionController 类与上面的链接一样,并使用 /pages/* 映射。 我应该使用什么 url 来调用 loginAction? 如果我使用 /login 前端控制器不匹配它,如果我使用 /pages/login 它匹配它但是当“home”来到 FrontController 操作时为空。我也应该对 home.jsp 采取行动吗?在那种情况下,如何实现重定向?

动作工厂

public class ActionFactory {

    private static ActionFactory instance;

    public ActionFactory() {
        actions = new HashMap<>();
        actions.put("login", new LoginAction());
    }

    public static ActionFactory getInstance() {
        if (instance == null) {
            return new ActionFactory();
        } else {
            return instance;
        }
    }

    Map<String, Action> actions;

    public Action getAction(HttpServletRequest request) {
        return actions.get(request.getPathInfo().substring(1));
    }
}

Servlet

@WebServlet(name = "ActionController", urlPatterns = {"/pages/*"})
public class ActionController extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        Action action = ActionFactory.getInstance().getAction(request);
        String view = action.execute(request, response);
        if (view.equals(request.getPathInfo().substring(1))) {
            request.getRequestDispatcher("/" + view + ".jsp").forward(request, response);
        } else {
            response.sendRedirect(view); // We'd like to fire redirect in case of a view change as result of the action (PRG pattern).
        }
    } catch (Exception e) {
        throw new ServletException("Executing action failed.", e);
    }
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

登录操作

public class LoginAction implements Action{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "home";
    }
}

【问题讨论】:

  • 你能显示一些代码吗?
  • 代码很简单。它与上面的链接完全相同。
  • 我已经添加了代码 sn-ps。

标签: jsp servlets


【解决方案1】:

我想这篇文章可以帮到你。

How come request.getPathInfo() in service method returns null?

您想使用 HttpServletRequest#getServletPath() 并且网址是 /pages/login

简单修复:调用页面“login.jsp”,Action的execute方法返回字符串“login”,测试的url又是“/pages/login”

【讨论】:

  • 我的问题不在于那行代码。如果 url /pages/home 来到 ActionController 我有动作对象 null 因为动作映射中不存在 home 。我的问题是我应该将 home 操作放在操作映射中吗?如果我应该如何将用户重定向到 home.jsp?
  • 只有当路径在注解中有 /pages urlPatterns = {"/pages/*"}) 时,url 才会转到 ActionController,而重定向或转发时应该转到 /home.jsp跨度>
  • 所以你是说 home.jsp 不应该通过 ActionController?
  • 我是说当重定向时你不应该附加 /pages/ 前缀,否则会在动作控制器中再次出现。
猜你喜欢
  • 1970-01-01
  • 2011-01-04
  • 2015-07-28
  • 2019-10-22
  • 2011-07-04
  • 2015-11-30
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多