3

ページコントローラーパターンを使用しています。リクエスト アクションを検出し、結果に従ってディスパッチすることで、2 つの異なるページに同じコントローラーを使用するにはどうすればよいでしょうか?

これが私のコードです:

account.jsp

<form name="input" action="<%=request.getContextPath() %>/edit" method="get">
   <input type="submit" value="Modifier" />
</form>

アカウントサーブレット

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("received HTTP GET");

        String action = request.getParameter("action");

        if (action == null)
        {
            // the account page
            dispatch(request, response, "/account");    
        }
        else if (action == "/edit") {
            // the popup edit page
            dispatch(request, response, "/edit");
        }

        protected void dispatch(HttpServletRequest request,
            HttpServletResponse response, String page)
            throws javax.servlet.ServletException, java.io.IOException {
        RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(page);
        dispatcher.forward(request, response);
}
    }
4

3 に答える 3

4

usingHttpServletRequest#getServletPath()が必要なものを正確に取得することがわかったので、何も解析する必要はありません!

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("received HTTP GET");

    String action = request.getServletPath();

    if (action.equals("/account"))
    {
        // the account page
        dispatch(request, response, "/content/account.jsp");    
    }
    else if (action.equals("/edit")) 
    {
        // the popup edit page
        dispatch(request, response, "/content/edit.jsp");
    }
}

protected void dispatch(HttpServletRequest request,
    HttpServletResponse response, String page)
        throws javax.servlet.ServletException, java.io.IOException {
    RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(page);
    dispatcher.forward(request, response);
    }
}
于 2012-06-30T03:12:22.953 に答える
1

を使用する必要がありますhttpServletRequest.getPathInfo()

これはドキュメンテーションが言っていることです:

java.lang.String getPathInfo()

Returns any extra path information associated with the URL the client sent when it
made this request. The extra path information follows the servlet path but 
precedes the query string and will start with a "/" character.

This method returns null if there was no extra path information.

Same as the value of the CGI variable PATH_INFO.

Returns:
    a String, decoded by the web container, specifying extra path information that
    comes after the servlet path but before the query string in the request URL; 
    or null if the URL does not have any extra path information

あなたの場合、にAccountServletマップされている場合、/accounts/*次のような値が得られます。

  • URLの/accounts/account場合は/account
  • URLの/accounts/edit場合は/edit
于 2012-06-29T07:57:46.437 に答える
0

request.getRequestURI()その URI の最後の部分を確認して取得することをお勧めします。

String uri = request.getRequestURI().getPath();
String action = uri.substring(0, uri.lastIndexOf('/'));
...

このコードはチェックしていません。エッジケースには注意してください。

あなたの質問を理解したかどうかわかりません。とにかく、これが役立つことを願っています。

于 2012-06-29T03:15:07.237 に答える