从我的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的原因/解决方案.
我试图通过执行以下操作将参数传递给mongodb脚本:
mongo --eval="foo='hello world'" test.js
Run Code Online (Sandbox Code Playgroud)
但是我希望变量foo是可选的(即,foo如果它存在则使用,否则在没有它的情况下继续),因此在test.js我测试变量的存在时:
if (foo)
print(foo)
//do stuff with foo
Run Code Online (Sandbox Code Playgroud)
如果我没有在命令行上指定foo,则会引发错误:
$ mongo test.js
ReferenceError: foo is not defined test.js:1
failed to load: test.js
Run Code Online (Sandbox Code Playgroud)
这通常适用于普通的JS,我如何在一个用于mongo的脚本中执行此操作?
基本上,我希望能够在mongo test.js不设置foo变量的情况下仍然可以使脚本仍然有效.