我想从JSF页面中的链接重定向,我该怎么办?
在HTML中我可以使用<a>标签.但是在JSF中我使用<h:outputLink>或者<h:commandLink>可以有条件地渲染它们.我希望将链接重定向到同一应用程序中的其他页面或外部URL.我怎么能用JSF做到这一点?如何使用action在<h:commandLink>此?
我需要在另一个视图范围的bean中使用保存在视图范围bean中的一些数据.
@ManagedBean
@ViewScoped
public class Attivita implements Serializable {
//
}
Run Code Online (Sandbox Code Playgroud)
和
@ManagedBean
@ViewScoped
public class Nota implements Serializable {
@ManagedProperty("#{attivita}")
private Attivita attivita;
// Getter and setter.
}
Run Code Online (Sandbox Code Playgroud)
现在,也许我关于它的理论仍然很差,我注意到当#{attivita}注入时,Attivita构造函数被调用,从而创建另一个实例.这是正确的行为吗?如果我想引用同一个实例而不是创建一个新实例呢?
我有两页:
String page1 = "user/newuser.jsf";
String page2 = "department/newdepartment.jsf";
Run Code Online (Sandbox Code Playgroud)
如果我重定向到page1这样:
FacesContext.getCurrentInstance().getExternalContext().redirect(page1);
Run Code Online (Sandbox Code Playgroud)
网址更改为localhost:8080/NavFile/user/newuser.jsf.
在此页面上,我重定向到page2:
FacesContext.getCurrentInstance().getExternalContext().redirect(page2);
Run Code Online (Sandbox Code Playgroud)
网址更改为localhost:8080/NavFile/user/department/newdepartment.jsf.但是user/department我的应用程序中没有目录.我的目标是重定向到localhost:8080/NavFile/department/newdepartment.jsf.
这是怎么造成的,我该如何解决?
我有简单的测试网络应用程序:第一页 - 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) 我正在尝试实现一个用户名列表,可以通过单击UP或DOWN链接重新排列.
<ul>
<ui:repeat var="user" value="#{cc.attrs.value}">
<li>
#{user.name}
<h:link outcome = "user" value = "left" onclick="#{accountController.moveDown}">
<f:param name="id" value = "${user.id}" />
</h:link>
</li>
</ui:repeat>
</ul>
Run Code Online (Sandbox Code Playgroud)
这里的问题是我似乎没有正确使用onclick属性.这样做的正确方法是什么?
编辑:根据您的建议,我将所有链接放在一个表单中:
<h:form>
<ui:repeat value="#{cc.attrs.value}" var = "user">
<div class = "user">
<h:commandLink id = "Link1" value = "Up" binding = "#{accountController.ommandLink}" action = "#{accountController.moveUserUp}">
<f:attribute name = "userId" value = "#{user.id}" />
</h:commandLink>
<h:commandLink id = "Link2" value = "Down" binding = "#{accountController.commandLink}" action = "#{accountController.moveUserDown}">
<f:attribute name = …Run Code Online (Sandbox Code Playgroud) 我有三个XHTML页面;
在index.xhtml页面中,我有一个commandButton发送给用户的page_1.xhtml.所有这些都在导航规则中完成faces-config.xml.
如何将用户重定向到page_2.xhtml从index.xhtml使用另一个commandButton假设两者commandButtons的行为都与后备Java类?
我正在使用选择一个菜单来导航到我网站的不同部分:
<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链接不会被跟踪.
当我遇到这个问题/答案时,我正在考虑在我的网络应用程序中使用显式页面导航规则,摘录如下:
...由于新的"隐式导航"功能,导航规则已经过时,因为JSF 2.0.
但是,我已经浏览了大部分的CoreServlets JSF 2.0教程,并且它有一个专门用于显式页面导航的部分,并且它对它有利.要么这违背了上述建议,要么我误解了一些东西.
我不想以过时的方式创建新的Web应用程序.谁能摆脱任何光明?
首先抱歉我的英语.我在JSF2中有两个页面,一个用于列出乘客,另一个用于创建/更新乘客.我还有两个@ViewScoped bean,一个带有乘客列表,另一个用于在pageB中保留所选乘客.我看到了通过viewParam或@ManagedProperty传递乘客的方法,但我不想写一个转换器.
我想知道是否有办法将对象从pageA传递给pageB而不传递乘客的id并写入转换器或没有传递id然后转到DB来检索乘客.我的工作和工作如下.我通过setPropertyActionListener在flash范围内设置所选对象并导航到pageB,在viewScopedBean的@PostConstruct中我获取flashScope并检索该对象.正如我所说,这有效,但我不知道它是否正确.这是代码页面A:
<p:column width="10" style="text-align: center;">
<p:commandButton icon="ui-icon-pencil" action="editClientes?faces-redirect=true">
<f:setPropertyActionListener target="#{flash.pax}" value="#{row}"/>
</p:commandButton>
</p:column>
Run Code Online (Sandbox Code Playgroud)
pageB bean的@PostConstruct
@PostConstruct
private void initBean(){
this.pax = (Passenger) JSFUtils.getFlashScope().get("pax");
if(this.pax == null){
this.pax = new Passenger();
}
}
Run Code Online (Sandbox Code Playgroud)
这是正确的,还是正确的方法是编写转换器?谢谢.
背景:我有一个使用 MyFaces 的 JSF 2.0 应用程序。使用这个应用程序,用户将通过浏览许多不同的 JSF 页面来创建一个复杂的“小部件”。我将托管 bean 存储在会话范围内,并在用户浏览每个页面时填充属性。一旦他们完成组装“小部件”,他们就会想要创建一个全新的“小部件”并重复该过程。我的问题是,如何在不中断 JSF 生命周期的情况下从会话中完全(并且安全地)删除托管 bean?
我已经看到类似问题的其他回复表明您可以使会话无效,并且您还可以通过 FacesContext 访问 HttpSession 并以这种方式删除 bean。我不希望用户必须注销或再次登录,也不想使整个会话无效。假设我需要通过 FacesContext 访问 HttpSession 并删除 bean,那么在 JSF 生命周期中何时是安全执行此操作的最合适的位置,以便问题不会在循环的其余部分级联?我想确保当用户开始创建下一个“小部件”的过程时,JSF 创建一个新的会话 bean 没有问题。
我也很想知道为什么 JSF 中没有一种机制来使这更容易。是否有其他一些我应该采用的方法更符合预期的 JSF 模式?这里我不能使用 View Scope,因为托管 bean 在完成之前会经过几个不同的页面和视图。
提前致谢!
jsf ×8
jsf-2 ×6
redirect ×4
navigation ×2
actionmethod ×1
commandlink ×1
directory ×1
hyperlink ×1
jsf-2.2 ×1
managed-bean ×1
onclick ×1
parent ×1
primefaces ×1
submit ×1
url ×1
view-scope ×1