背景:我有一个使用 MyFaces 的 JSF 2.0 应用程序。使用这个应用程序,用户将通过浏览许多不同的 JSF 页面来创建一个复杂的“小部件”。我将托管 bean 存储在会话范围内,并在用户浏览每个页面时填充属性。一旦他们完成组装“小部件”,他们就会想要创建一个全新的“小部件”并重复该过程。我的问题是,如何在不中断 JSF 生命周期的情况下从会话中完全(并且安全地)删除托管 bean?
我已经看到类似问题的其他回复表明您可以使会话无效,并且您还可以通过 FacesContext 访问 HttpSession 并以这种方式删除 bean。我不希望用户必须注销或再次登录,也不想使整个会话无效。假设我需要通过 FacesContext 访问 HttpSession 并删除 bean,那么在 JSF 生命周期中何时是安全执行此操作的最合适的位置,以便问题不会在循环的其余部分级联?我想确保当用户开始创建下一个“小部件”的过程时,JSF 创建一个新的会话 bean 没有问题。
我也很想知道为什么 JSF 中没有一种机制来使这更容易。是否有其他一些我应该采用的方法更符合预期的 JSF 模式?这里我不能使用 View Scope,因为托管 bean 在完成之前会经过几个不同的页面和视图。
提前致谢!
我在JSF页面之间导航时遇到问题.单击命令按钮时,大多数导航都会发生.命令按钮的操作返回一个字符串.
我的登录页面是我的欢迎页面.这是在我的web.xml中:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/pages/index.xhtml</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
在我浏览器的地址栏中,该页面显示为:
http://localhost:8080/ui/faces/pages/index.xhtml
Run Code Online (Sandbox Code Playgroud)
一旦身份验证发生,该函数返回此字符串:
"/ui/faces/pages/home.xhtml"
Run Code Online (Sandbox Code Playgroud)
我要导航到的文件位于:
pages/home.xhtml
Run Code Online (Sandbox Code Playgroud)
但是当导航应该发生时,我收到此错误:
无法找到与from-view-id'/pages/index.xhtml'匹配的导航案例,用于行动'#{indexPageController.login()}'与结果'/ui/faces/pages/home.xhtml'
任何人都可以帮助我理解正确导航到页面所需的相对路径吗?
我有JSF 2.2功能的问题<f:viewAction action="#{...}/>.我把这个标签放在我的XHTML-Page中,它被称为viewActionStart.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<f:view>
<f:metadata>
<f:viewAction action="#{redirect.getView()}" onPostback="true"
immediate="true" />
</f:metadata>
</f:view>
<h:outputText value="If you see that text, the viewAction was not performed." />
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果我直接访问此XHTML页面,一切正常:redirect.getView()调用该方法并执行重定向到另一个页面.
现在我有了我的登陆页面,它显示在viewActionStart-Page之前.有一个按钮,基本上应该导航到我的viewActionStart-Page来显示<f:viewAction>效果.以下代码来自此着陆页index.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<f:view>
<h:form>
<h:commandButton action="viewActionStart.xhtml"
value="To f:viewAction Page" />
</h:form>
</f:view>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果我点击该命令按钮,则会显示viewActionStart.xhtml的内容.为什么JSF忽略了<f:viewAction>?我不明白为什么viewAction每个页面加载不起作用.我尝试了那些onPostback …
我想从JSF操作方法转发请求到非JSF页面.我在JSF操作中使用下面的代码:
public String submitUserResponse() {
// ...
parseResponse("foo.jsp", request, response);
// ...
return "nextpage";
}
private String parseResponse(String uri, HttpServletRequest request, HttpServletResponse response) {
if (uri != null) {
RequestDispatcher dispatcher = request.getRequestDispatcher(uri);
dispatcher.forward(request, response);
return null;
}
// ...
return "xxxx";
}
Run Code Online (Sandbox Code Playgroud)
在submitUserResponse()当用户点击来自JSF页面的提交按钮被称为动作方法,并且此方法返回nextpage的字符串.这里请求转发到正常流程中的下一个JSF页面.但在我的要求中,我需要将请求转发到下一个非JSF页面.它正在运行,但它在服务器中显示以下异常.
java.lang.IllegalStateException:在提交响应后无法转发
我观察到转发请求后使用的代码行parseResponse(...)和return "nextpage";仍在执行dispatched.forward(uri).同样的事情发生了response.sendRedirect(url).这是怎么造成的,我该如何解决?
我需要2.xhtml在当前 JSF 页面(例如1.xhtml)的新选项卡中打开一个新的JSF 页面(例如)。我应该使用哪个 JSF 组件?<h:commandLink>或者<h:outputLink>?
1.xhtml单击链接以2.xhtml在新选项卡中打开后,我不想失去当前页面的范围。
的豆1.xhtml是@ViewScoped。我应该把它改成@RequestScoped吗?
我正在使用JSF 2.1和Primefaces:
我有一个带有托管属性的视图作用域托管bean,以及一个在其他视图范围内的托管bean上设置内容并转发到引用该托管bean的其他页面的方法:
@ManagedBean
@ViewScoped
public class HelloMB {
@ManagedProperty("otherMB")
private OtherMB other;
public String changeOtherMB() {
otherMB.setAnyObject(new Object());
return "otherPage.xhtml";
}
}
@ManagedBean
@ViewScoped
public class OtherMB {
private Object o;
public void setAnyObject(Object o) {
this.o = o;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当渲染otherPage时o为null.
你知道我怎么能解决这个问题?如何在@ViewScoped托管bean中保留对象并将其保存在其他页面上而不使用@SessionScoped?
我正在探索JSF 2.2 Faces Flow功能,但我仍然不确定使用Faces Flow定义流程而不是使用普通导航系统(在链接或按钮中调用facelets)有什么好处?
我是JSF的新手.我想知道JSF /导航规则的一点.我有四个页面,索引,p1,p2,p3.当我尝试导航到一个页面,其中action ="#{bean.gotoP1()} ",这是错误的;
"无法找到与from-view-id'/index.xhtml'匹配的导航案例,以便对行动'#{bean.gotoP1()}'结果'成功'"
我的问题很简单; 为什么我不能用#{bean.gotoP1()}导航,我必须删除括号#{bean.gotoP1}?
我的代码在下面;
的index.xhtml
<h:body>
<h:form>
<h:commandButton action="#{mybean.gotoP1()}" value="P1"/>
<h:commandButton action="#{mybean.gotoP2()}" value="P2"/>
<h:commandButton action="#{mybean.gotoP3()}" value="P3"/>
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud)
mybean.java
@ManagedBean
@RequestScoped
public class Mybean implements Serializable{
private static final long serialVersionUID=1L;
public Mybean() {
}
public String gotoP1(){
return "success";
}
public String gotoP2(){
return "success";
}
public String gotoP3(){
return "positive";
}
}
Run Code Online (Sandbox Code Playgroud)
faces-config.xml中
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-action>#{mybean.gotoP1}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/p1.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{mybean.gotoP2}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/p2.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{mybean.gotoP3}</from-action>
<from-outcome>positive</from-outcome>
<to-view-id>/p3.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)
谢谢....
我有一个JSF应用程序,我希望在导航中更改浏览器地址栏中的URL.当我打开Home.xhtml并提交表单时,它会显示下一页AppHome.xhtml,但浏览器地址栏中的URL不会更改.
这是提交按钮:
<p:commandButton value="Connect" update="panel" id="ajax" action="#{user.check}" styleClass="ui-priority-primary"/>
Run Code Online (Sandbox Code Playgroud)
这是导航规则:
<navigation-rule>
<from-view-id>/Home.xhtml</from-view-id>
<navigation-case>
<from-outcome>Success</from-outcome>
<to-view-id>/AppHome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>Failure</from-outcome>
<to-view-id>/Home.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)
执行此导航时,如何更改浏览器地址栏中的URL?
我试图从这个问题中理解在JSF动作中返回null和""之间的区别,但是无法实际体验.
以下是OP的报价
据我所知,当JSF操作返回""(空字符串)时,用户会保留在当前页面上,但视图会刷新.但是,当操作返回null时,用户仍然停留在当前页面上,但旧视图将被重用
我在理解返回的区别混淆"",null并"viewid?faces-redirect=true"从JSF支持bean.我试图通过一个小例子来理解差异,但我无法体验到实际的差异.以下是我的示例的代码片段.
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Jsf Questions</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jspx</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/jsfintroapp/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
faces-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean>
<managed-bean-name>userLogin</managed-bean-name>
<managed-bean-class>com.srk.beans.UserLogin</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<description>Contains list of all navigation rules here</description>
<from-view-id>/*</from-view-id>
<navigation-case>
<display-name>WELCOME</display-name>
<from-outcome>welcome</from-outcome>
<to-view-id>/geek_examples/authorized_user.jspx</to-view-id>
</navigation-case>
</faces-config>
Run Code Online (Sandbox Code Playgroud)
我的托管bean是一个请求范围的bean,如上所述.
UserLogin.java(Backing bean)代码片段
public String saveData() {
//String returnString …Run Code Online (Sandbox Code Playgroud) 我有以下导航规则:
<navigation-rule>
<from-view-id>/pages/*</from-view-id>
<navigation-case>
<from-outcome>test</from-outcome>
<to-view-id>/pages/test/edit.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)
TestMB:
public String test() {
return "test?id=1";
}
Run Code Online (Sandbox Code Playgroud)
和test.xhtml:
<h:commandLink action="#{testMB.test}">click</h:commandLink>
Run Code Online (Sandbox Code Playgroud)
但是,如果这是行不通的话return "test?id=1";.我不想使用return "/pages/test/edit.xhtml?id=1";,我想使用抽象名称test.
我是 jsf 的新手,正在尝试构建应用程序。问题是当我单击登录命令按钮时没有任何反应。所以我输入了一些打印行,它告诉我与数据库的连接存在并且参数有效但它不会重定向。
这是 index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Dezurstvo</title>
</h:head>
<h:body>
Login
<br />
<h:form>
<p:growl id="msgs" showDetail="true"/>
<h:panelGrid columns="2" cellpadding="5">
<h:outputText value="Username"/>
<p:inputText value="#{logIn.username}" required="true"/>
<h:outputText value="Password"/>
<p:password id="pass" value="#{logIn.password}" feedback="false" required="true"/>
</h:panelGrid>
<p:commandButton value="Login" action="#{logIn.loadUser()}" update="msgs" />
<p:commandButton value="Regisracija" />
</h:form>
<h:link outcome="adminMain" value="Primefaces welcome page" />
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
NavigationBean 类
package beans;
import dataBeans.Korisnik;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean(name …Run Code Online (Sandbox Code Playgroud) 我正在研究一个小型的webtool练习和导航规则已引起我的注意.所以我看了几个网络教程,并亲自尝试过,这对我不起作用.它不会重定向到所需的页面.
faces-config.xml(只有那个重要的部分)
<navigation-rule>
<from-view-id>/kursleiter.xhtml</from-view-id>
<navigation-case>
<from-action>#{verifyCredentials.save}</from-action>
<from-outcome>ok</from-outcome>
<to-view-id>/teilnehmer.xhtml?faces-redirect=true</to-view-id>
</navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)
返回值的类 <from-outcome>
public class verifyCredentials() {
public String save(Klasse klasse, Module modul) {
//do some other stuff
return "ok";
}
}
Run Code Online (Sandbox Code Playgroud)
按下此commandLink时,应该发生重定向
<p:commandLink actionListener="#{verifyCredentials.save(klasse, modul)}">#{modul.modulnummer} </p:commandLink>
Run Code Online (Sandbox Code Playgroud)
现在,这给我带来了几个问题:
/faces/的<from-view-id>?提前谢谢 - Reteras
jsf ×12
navigation ×7
jsf-2 ×4
jsf-2.2 ×3
view-scope ×2
ajax ×1
faces-flow ×1
forward ×1
jsf-1.2 ×1
managed-bean ×1
new-window ×1
primefaces ×1
redirect ×1
tomcat7 ×1
url ×1
view ×1