Java 通过隐式导航重定向到servlet?
如何通过支持方法正确地重定向到servletJava 通过隐式导航重定向到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导航处理程序导航到
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()
执行转发,而不是重定向。您明确要求“重定向”,这是一个完全不同的过程。另见