2011-12-27 180 views
0

我不得不指定doGet參數以及每次拋出的內容,這讓我覺得多餘。有沒有辦法在我的Controller內完成所有這些操作,因此無需在每個頁面上完成?我Controller目前沒有doGet()減少冗餘代碼

@WebServlet(name = "EditServlet", urlPatterns = {"/content/edit"}) 
public class EditServlet extends cms.library.Controller { 
    @Override 
    public void doGet (HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     this.loadView(new cms.library.PageConfig() 
       .setRequest(request) 
       .setResponse(response) 
       .setTemplate("/content/edit")); 
    } 

    @Override 
    public void doPost (HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     System.out.println("posted"); 

     this.doGet(request, response); 
    } 
} 
+0

由於有一個「doGet」,不知道你的意思是「沒有doGet」。另外,我不確定將POST和GETs等同起來是否合理。它幾乎可以肯定是一個客戶端錯誤發佈到非形式的東西。 – 2012-01-01 15:21:35

回答

0

您可以在模板中的cms.library.Controller類傳遞,這樣你就不必定義的doGet每次。

public class Controller { 
    private final String template; 
    public Controller(String template) { 
     this.template = template; 
    } 
    @Override 
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     this.loadView(new cms.library.PageConfig() 
       .setRequest(request) 
       .setResponse(response) 
       .setTemplate(template)); 
    } 
} 

@WebServlet(name = "EditServlet", urlPatterns = {"/content/edit"}) 
public class EditServlet extends cms.library.Controller { 
    public EditServlet() { 
     super("/content/edit"); 
    } 
    @Override 
    public void doPost (HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     System.out.println("posted"); 
     this.doGet(request, response); 
    } 
} 

或者你可以讓自己的註釋和Controller類將檢查在其構造實現類的註釋(而不是傳遞字符串中)。