如何在基于请求作用域 bean 中的值呈现表单时提交表单
我在我的小程序中发现了一个问题,我想知道是否有人对如何尽可能最好地解决这个问题有任何提示或建议。
我有请求范围内的 bean testBean。 它包含以下内容:
public class testBean {
private boolean internal = false;
private String input = "";
public String internalTrue() {
System.out.println("Set internal true");
setInternal(true);
return null;
}
public String submitForm() {
System.out.println("");
return null;
}
public boolean isInternal() {
return internal;
}
public void setInternal(boolean internal) {
this.internal = internal;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
}
我的文件welcomeJSF.jsp 包含以下内容:
<f:view>
<h:form>
<h:commandButton value="Set internal true" action="#{testBean.internalTrue}" />
</h:form>
<h:panelGrid columns="1" rendered="#{testBean.internal}">
<h:form>
<h:outputText value="JavaServer Faces" /><h:inputText value="#{testBean.input}" />
<h:commandButton value="Go" action="#{testBean.submitForm}" />
</h:form>
</h:panelGrid>
</f:view>
当我运行应用程序时,我会看到按钮“设置内部true”。 我单击它,然后出现一个表单,其中有“Go”按钮。 单击“Go”不会触发我的 bean 中的方法,很可能是因为该字段实际上不再在服务器上呈现,因此它不会运行该方法。 有什么聪明的解决方案吗?
预先感谢您的宝贵时间。
I discovered a problem in my little program, and Im wondering if anyone have any tips or advice on how to solve this problem as best as possible.
I have the bean testBean which is in request scope. It contains the following:
public class testBean {
private boolean internal = false;
private String input = "";
public String internalTrue() {
System.out.println("Set internal true");
setInternal(true);
return null;
}
public String submitForm() {
System.out.println("");
return null;
}
public boolean isInternal() {
return internal;
}
public void setInternal(boolean internal) {
this.internal = internal;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
}
My file welcomeJSF.jsp contains this:
<f:view>
<h:form>
<h:commandButton value="Set internal true" action="#{testBean.internalTrue}" />
</h:form>
<h:panelGrid columns="1" rendered="#{testBean.internal}">
<h:form>
<h:outputText value="JavaServer Faces" /><h:inputText value="#{testBean.input}" />
<h:commandButton value="Go" action="#{testBean.submitForm}" />
</h:form>
</h:panelGrid>
</f:view>
When I run the application Im presented with the button "Set internal true". I click it and Im presented with the form where I have the button "Go". Clicking "Go" does not trigger the method in my bean, most likely because of the field actually not being rendered on the server anymore, and thus it wont run the method. Is there any smart solutions to this?
In advance, thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
面板的子级永远不会解码输入,因为其 rendered 属性在 APPLY REQUEST VALUES 阶段始终计算为 false。 从安全角度来看,这是明智的做法。
(来源:ibm.com)
您可以做的一件事是利用 JSF 组件在视图的生命周期内维护状态这一事实。
新 bean:
绑定到 bean 的新视图:
使用
请注意,您可能需要进行一些测试,具体取决于您的实现和/或库的 StateManager 存储请求之间的视图(这可能会受到 javax.faces.STATE_SAVING_METHOD 初始化参数的影响)。 视图可能存储在表单的隐藏字段中、以视图 ID 为关键字的会话中(这可能会导致与多个浏览器窗口发生冲突)、以 GET 或 JSF 导航创建的唯一 ID 为关键字的会话中,或者通过一些完全自定义的机制。 该框架的可插入性质使其具有多种用途,但在这种情况下,这意味着您需要仔细检查您的实现的行为方式。
The children of the panel will never decode input because its rendered attribute always evaluates to false during the APPLY REQUEST VALUES phase. This is a sensible thing to do from a security point of view.
(source: ibm.com)
One thing you could do is take advantage of the fact that JSF components maintain state for the lifetime of the view.
The new bean:
The new view bound to the bean:
Using the binding attribute on the panelGrid will cause setPanel to be called when the view is created/restored.
Note that you may have some testing to do depending on how your implementation's and/or libraries' StateManager stores views between requests (which may in turn be affected by the javax.faces.STATE_SAVING_METHOD init parameter). The view might be stored in a hidden field in the form, in the session keyed to the view ID (which may cause collisions with multiple browser windows), in the session keyed to a unique ID created by a GET or JSF navigation, or by some completely custom mechanism. The pluggable nature of the framework makes it versatile, but in this case it means you need to double-check how your implementation behaves.