Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过隐式导航重定向到servlet?_Java_Jsf_Jakarta Ee_Servlets - Fatal编程技术网

Java 通过隐式导航重定向到servlet?

Java 通过隐式导航重定向到servlet?,java,jsf,jakarta-ee,servlets,Java,Jsf,Jakarta Ee,Servlets,如何通过支持方法正确地重定向到servlet class MyBean{ public String doRedirect() { //some conditions return "newlocation"; } } <h:commandButton value="Test" action="#{myBean.doRedirect}" /> 然后如何重定向到servlet而不是xhtml文件?您不能使用JSF导航处理程序导航到

如何通过支持方法正确地重定向到servlet

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


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

然后如何重定向到servlet而不是xhtml文件?

您不能使用JSF导航处理程序导航到非JSF资源

直接使用该方法即可:

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");
}

不能使用JSF导航处理程序导航到非JSF资源

直接使用该方法即可:

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");
}

您可以访问原始HTTPServletResponse并使用其API执行重定向

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect(URL);

您可以访问原始HTTPServletResponse并使用其API执行重定向

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect(URL);

好酷,谢谢你的提示!使用
.redirect
.dispatch
更好吗?
dispatch()
执行转发,而不是重定向。您明确要求“重定向”,这是一个完全不同的过程。请参阅Ok cool,谢谢您的提示!使用
.redirect
.dispatch
更好吗?
dispatch()
执行转发,而不是重定向。您明确要求“重定向”,这是一个完全不同的过程。另见