我什么时候应该使用<h:outputLink>
而不是<h:commandLink>
?
我知道a commandLink
生成一个HTTP帖子; 我猜这outputLink
将产生HTTP获取.也就是说,我读过的大多数JSF教程材料都是commandLink
(几乎?)使用的.
上下文:我正在实现一个小的演示项目,它显示了一个到用户页面的标题链接,就像Stack Overflow的......
......我不确定commandLink
(可能是?faces-redirect=true
用于书签)还是outputLink
正确的选择.
我正在学习JSF,当我意识到每当我们使用时<h:form>
,JSF的标准行为总是向我显示浏览器中上一页的URL ,而不是当前页面的URL,我感到非常惊讶和困惑.
我知道这与JSF总是将表单发布到同一页面的方式有关,然后只是呈现控制器将其返回给浏览器的任何页面,而浏览器不知道页面位置已更改.
似乎JSF已经存在了足够长的时间,必须有一个干净,可靠的方法来处理这个问题.如果是这样,你介意分享吗?
我找到了各种解决方法,但遗憾的是,这似乎不是一个真正可靠的解决方案.
"?faces-redirect=true"
到每个 bean的操作的返回值然后
@RequestScoped
用其他东西替换(Flash Scopes,CDI对话,@ SessionScoped,......).如果它"?faces-redirect=true"
是如此好,有没有办法配置整个应用程序以这种方式处理所有请求?
似乎以下是等价的:
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation("/index.xhtml?faces-redirect=true");
FacesContext.getCurrentInstance().getExternalContext().redirect("/testapp/faces/index.xhtml");
Run Code Online (Sandbox Code Playgroud)
是否有任何差异,何时应该使用?
我想在调用某个方法时通过JSF支持bean重置.假设有一个命令按钮,有人按下它,在成功完成事务后,我的View或Session范围JSF bean应该被重置.有没有办法做到这一点?
谢谢
我正在尝试使用commandbutton在我的托管bean中转发页面:
<h:commandButton action="#{bean.action}" value="Go to another page" />
Run Code Online (Sandbox Code Playgroud)
以下行:
public void action() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("another.xhtml");
}
Run Code Online (Sandbox Code Playgroud)
重定向页面,而不是转发.我已经看到了类似的问题并尝试了给定的解决方案:
public void action() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().dispatch("another.xhtml");
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Index: 0, Size: 0
Run Code Online (Sandbox Code Playgroud)
那么如何从托管bean转发到页面?
我有简单的测试网络应用程序:第一页 - index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
First page
<h:form>
<h:commandButton value="Go to Next page" action="next"/>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
和下一页 - next.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Next page
<h:form>
<h:commandButton value="Go to First page" action="index"/>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,我在浏览器上有该URL:
http://localhost:8080/Test/
Run Code Online (Sandbox Code Playgroud)
和index.xhtml作为第一页.
然后当我点击"转到下一页"我有网址
http://localhost:8080/Test/index.xhtml
Run Code Online (Sandbox Code Playgroud)
和next.xhtml页面.当我点击"转到第一页"页面更改(到index.xhtml)但是url它
http://localhost:8080/Test/next.xhtml
Run Code Online (Sandbox Code Playgroud)
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping> …
Run Code Online (Sandbox Code Playgroud) 我觉得这将是一个最短的答案将是一个问题:"这就是为什么JSF取代了JSP",但我会继续问问题.
问题:我想知道:我可以获取JSF页面的Response对象(如果有的话)?
为何奇怪?:我发现自己处于需要从JSF页面传递到JSP的情况,所以我想为什么不response.sendRedirect
从一个从JSF页面调用的bean 重定向(with )然后......你可以看到它的标题.
我觉得这样可以用更干净的方式完成,但看不出怎么样!
编辑:在它上面,我还会询问哪种方式最适合重定向到JSF页面.
提前感谢您的建议.
在OmniFaces中,FullAjaxExceptionHandler在找到要使用的正确错误页面之后,调用JSF运行时来构建视图并呈现它而不是包含AJAX调用的页面.
为什么这个?恕我直言,只是执行一个ExternalContext#redirect()
?有没有具体的理由这样做?
我们正在编写基于FullAjaxExceptionHandler的自己的ExceptionHandler,并希望了解这种设计背后的原因.
我正在使用选择一个菜单来导航到我网站的不同部分:
<p:selectOneMenu value="#{navigator.outcome}">
<f:selectItem itemLabel="Select page..." />
<f:selectItem itemValue="page1" itemLabel="Page 1" />
<f:selectItem itemValue="page2" itemLabel="Page 2" />
<f:selectItem itemValue="page3" itemLabel="Page 3" />
<p:ajax event="change" listener="#{navigator.navigate}" />
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
是否有更多SEO友好的方式这样做?我担心JavaScript链接不会被跟踪.
我尝试将 WebFilter 与 JSF 2 一起使用,但我的过滤器无法正常工作。无法识别 urlPattern。
我的过滤器类:
@WebFilter(urlPatterns = {"/rws/*"})
public class AuthorizationFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession(true);
Object o = session.getAttribute("user");
HttpServletResponse res = (HttpServletResponse) response;
if(o == null)
res.sendRedirect(req.getContextPath() + "/login.xhtml");
else
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
Run Code Online (Sandbox Code Playgroud)
在我的结构中,我想保护 rws 文件夹中的所有页面,但我无法配置过滤器。
我试过@WebFilter("/rws/*") @WebFilter("/faces/rws/*")
我的过滤器从未被执行! …