1

バッキングメソッドを使用してサーブレットに適切にリダイレクトするにはどうすればよいですか?

class MyBean{ 
    public String doRedirect() {
        //some conditions
        return "newlocation";
    }
}


<h:commandButton value="Test" action="#{myBean.doRedirect}" />

これにより、私はnewlocation.xhtmlにリダイレクトされます。

しかし、WebServletがある場合はどうなりますか?

@WebServlet(urlPatterns = "/newlocation")

次に、xhtmlファイルではなくサーブレットにリダイレクトするにはどうすればよいですか?

4

2 に答える 2

2

JSF ナビゲーション ハンドラを使用して、JSF 以外のリソースに移動することはできません。

ExternalContext#redirect()メソッドを直接使用するだけです:

public void doRedirect() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect("newlocation");
}

または、現在のリクエスト パスがわからない場合:

public void doRedirect() throws IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/newlocation");
}
于 2012-08-21T21:27:29.883 に答える
-1

生の HTTPServletResponse にアクセスし、その API を使用してリダイレクトを行うことができます

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect(URL);
于 2012-08-21T21:27:45.677 に答える