是什么区别action和actionListener,什么时候应该使用action与actionListener?
我用容器管理的安全性编写了简单的应用程序.问题是当我登录并打开另一个我注销的页面时,然后我回到第一页,我点击任何链接等或刷新页面我得到这个例外.我想这是正常的(或者可能不是:))因为我退出了会话被破坏了.我该怎么做才能将用户重定向到例如index.xhtml或login.xhtml,并使他免于看到错误页面/消息?
换句话说,如何在我退出后自动将其他页面重定向到索引/登录页面?
这里是:
javax.faces.application.ViewExpiredException: viewId:/index.xhtml - View /index.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:212)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at filter.HttpHttpsFilter.doFilter(HttpHttpsFilter.java:66)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
Run Code Online (Sandbox Code Playgroud) 我什么时候应该使用<h:outputLink>而不是<h:commandLink>?
我知道a commandLink生成一个HTTP帖子; 我猜这outputLink将产生HTTP获取.也就是说,我读过的大多数JSF教程材料都是commandLink(几乎?)使用的.
上下文:我正在实现一个小的演示项目,它显示了一个到用户页面的标题链接,就像Stack Overflow的......

......我不确定commandLink(可能是?faces-redirect=true用于书签)还是outputLink正确的选择.
有没有办法在加载页面时执行JSF托管bean操作?
如果那是相关的,我现在正在使用JSF 1.2.
我正在学习JSF,当我意识到每当我们使用时<h:form>,JSF的标准行为总是向我显示浏览器中上一页的URL ,而不是当前页面的URL,我感到非常惊讶和困惑.
我知道这与JSF总是将表单发布到同一页面的方式有关,然后只是呈现控制器将其返回给浏览器的任何页面,而浏览器不知道页面位置已更改.
似乎JSF已经存在了足够长的时间,必须有一个干净,可靠的方法来处理这个问题.如果是这样,你介意分享吗?
我找到了各种解决方法,但遗憾的是,这似乎不是一个真正可靠的解决方案.
"?faces-redirect=true"到每个 bean的操作的返回值然后
@RequestScoped用其他东西替换(Flash Scopes,CDI对话,@ SessionScoped,......).如果它"?faces-redirect=true"是如此好,有没有办法配置整个应用程序以这种方式处理所有请求?
从我的bean中的操作,我正在尝试重定向到期望视图参数的另一个页面.在JSF2中这样做的推荐方法是什么?
例如,我的源页面是: http://localhost/page1.xhtml
它有一个调用动作的commandButton:
<h:commandButton value="submit" action="#{myBean.submit}" />
Run Code Online (Sandbox Code Playgroud)
我的bean看起来像:
@ManagedBean
@RequestScoped
public class MyBean {
private int id;
public String submit() {
//Does stuff
id = setID();
return "success";
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望'submit'操作的返回导航到 http://localhost/page2.xhtml?id = 2
我试图在我的导航案例中使用view-param执行此操作,但结果很奇怪.faces-config片段如下所示:
<navigation-rule>
<from-view-id>/page1.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/page2.xhtml</to-view-id>
<redirect>
<view-param>
<name>id</name>
<value>#{myBean.id}</value>
</view-param>
</redirect>
</navigation-case>
</navigation-rule>
Run Code Online (Sandbox Code Playgroud)
奇怪的行为是,即使myBean设置为请求作用域,它只在我第一次加载应用程序时调用myBean.getId(),并为所有后续调用重用相同的值,从而为page2生成不正确的视图参数.
所以我正在寻找一个更好的方法来做到这一点,或者为什么每次都没有从我的bean请求view-param的原因/解决方案.
我已经阅读了如何使用JSF发送参数但是如果用户companyId在访问其登录页面时在URL中键入它们会怎么样?例如,
http://my.company.url/productName/login.faces?companyId = acme.
我们现在的方式是,有一些scriptlet代码可以从请求中获取值,然后在会话中设置它.该参数从登录页面开始改变其外观,因此每个客户可以具有不同的登录页面视图.在切换到JSF之前,我们正在使用extj.
有没有办法使用JSF 2或PrimeFaces?
如果代码是这样的话,我无法重定向到另一个页面:
<h:commandButton type="button" value="Enter" action="index?faces-redirect=true" >
Run Code Online (Sandbox Code Playgroud)
但是,如果代码是:
<h:commandButton type="button" value="Enter" action="index?faces-redirect=true" >
<f:ajax />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)
有人能解释一下吗?谢谢!
- - - - - - - - - - - -编辑 - - - - - - - - - - - -
整个xhtml代码供您参考:
<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>
<h:form id="form">
<h:commandButton id="enterModelButton" type="button" value="Enter" action="index?faces-redirect=true" >
<f:ajax />
</h:commandButton>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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转发到页面?
我有一个基本的流程示例工作:
src/main/webapp
|
|- index.xhtml
|- flow1
|- flow1-flow.xml
|- flow1.xhtml
Run Code Online (Sandbox Code Playgroud)
index.xhtml有一个简单的表单,用参数输入流:
<h:form>
Click to enter flow1
<h:commandButton action="flow1" value="Flow 1">
<f:param name="testInput" value="hi there"/>
</h:commandButton>
</h:form>
Run Code Online (Sandbox Code Playgroud)
flow1.xhtml显示参数,并允许您在流范围中输入值:
<h:form>
Hi this is page 1.
<h:inputText label="Enter something:" value="#{flowScope.testOutput}"/><br/>
Request parameter: #{param['testInput']}<br/>
<h:commandButton action="returnFromFlow1"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
flow1-flow.xml只将返回节点定义为"returnFromFlow1"并将其设置为/index.xhtml.
这似乎有效.我想在输入流时实现post-redirect-get,以便浏览器地址栏与视图保持同步.所以我自然尝试了action ="flow1?faces-redirect = true".此更改阻止流程执行..它只是在单击按钮时重新加载index.xhtml.
然后我尝试了action ="flow1/flow1.xhtml?faces-redirect = true".这会加载页面并按预期重定向,但流程未初始化.当我在流程中提交表单时,我收到有关flowScope解析为null的错误.
做了一点研究,我找到了一个提示,设置"to-flow-document-id"来强制它初始化流程.所以我添加到我的命令按钮.没变.
有关如何实现这一目标的任何想法?
jsf ×10
jsf-2 ×5
action ×2
managed-bean ×2
navigation ×2
redirect ×2
faces-flow ×1
forward ×1
jsf-2.2 ×1
logout ×1
onload ×1
query-string ×1
url ×1