2012-04-06 3 views
0

デザインパターンはhereです。 FrontController、Command - > Service、およびDaoがあります。JSP MVCでURLをロードするときに、どのようにコマンドを呼び出すのですか?

http://localhost:8080/MyApplication/FrontController/viewprofile

私は、自動的にそのユーザの詳細を表示するViewProfileCommandを起動する方法がわからない:私は私のブラウザでこのURLを読み込む場合

は(viewprofileは、JSPの名前です)。 http://localhost:8080/MyApplication/FrontController/viewprofile?action=ViewProfile

現在、私はそれを持っている方法は、ViewProfileCommandを呼び出すために、私は次のようにURLにそれを連結する必要があり、このようなものです。

jspをコマンドにマップするにはどうすればURLに連結する必要はありませんか?おかげ

FrontController:あなたが代わりに要求パス情報のリクエストパラメータに基づいてアクションを決定するためにあなたのフロントコントローラを設計している

package com.secret.bookstore.servlet; 

import java.io.IOException; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import com.secret.bookstore.command.Command; 
import com.secret.bookstore.command.CommandFactory; 
import com.secret.bookstore.exceptions.CommandCreationException; 
import com.sun.net.httpserver.Filter.Chain; 

/* 
* Front Controller (Mediator Pattern) 
*/ 
@WebServlet(urlPatterns={"/FrontController"}) 
public class FrontController extends HttpServlet { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public FrontController(){ 
     super(); 
    } 

    protected void service(HttpServletRequest request, HttpServletResponse response){ 
     String action = request.getParameter("action"); 

     CommandFactory commandFactory = CommandFactory.getInstance(); 
     Command command = null; 
     String view = null; 

     view = request.getPathInfo().substring(1); 

     if(action != null){ 
      try { 
       command = commandFactory.createCommand(action); 
       view = command.execute(request, response); 
      } catch(CommandCreationException e) { 
       e.printStackTrace(); 
      } 
     } 

     forwardToPage(request, response, view); 
    } 

    /** 
    * Forward to server to the supplied page 
    */ 
    private void forwardToPage(HttpServletRequest request, HttpServletResponse response, String view){ 

     //Get the request dispatcher object and forward the request to the appropriate JSP page... 
     if(view.equals(request.getPathInfo().substring(1))){ 
       RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/" + view + ".jsp"); 

       try { 
        dispatcher.forward(request, response); 
       } catch (ServletException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
     } else { 
      try { 
       response.sendRedirect(view); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

} 

答えて

2

。したがって、アクションを呼び出すためには、実際にリクエストパラメータを指定する必要があります。

リンクされた回答のフロントコントローラーの例では、リクエストメソッドとリクエストパス情報に基づいてアクションが決定され、これもより賢明な方法です。 GET要求を前処理できるという機能的な要件を達成するには、同じことを行う必要があります。

基本的に:

command = commandFactory.createCommand(request.getMethod(), request.getPathInfo()); 

または単に与えられた例のように:

command = commandFactory.createCommand(request); 
関連する問題