2012-04-06 52 views
0

我使用的設計模式爲here。它有一個FrontController,Command - > Service和Dao。在JSP MVC中加載URL時如何調用命令?

如果我在瀏覽器中加載這個URL(自拍是JSP的名稱):

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

我不知道如何自動調用ViewProfileCommand以然後顯示用戶的詳細信息。

目前我擁有的方式是這樣的,爲了調用ViewProfileCommand我必須將它連接到URL上,如:http://localhost:8080/MyApplication/FrontController/viewprofile?action=ViewProfile

如何將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);