简体   繁体   English

JSF commandButton的“奇怪”行为

[英]JSF commandButton 'strange' behaviour

I'll be very pleased to get a decent explaination for the following case. 对于以下情况,我将很高兴获得体面的解释。

Here's a simple JSF with two forms and text output: 这是一个简单的JSF,具有两种形式和文本输出:

<h:body>
<h:form>
<h:commandButton value="Go" action="#{wierdBean.doWierdStuff}"/>
</h:form>

<h:form>
<h:dataTable value="#{wierdBean.pages}" var="page">
<h:column>
<h:commandButton value="the same go action?" action="#{wierdBean.doWierdStuff}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>

<h:dataTable value="#{wierdBean.pages}" var="page">
<h:column>
<h:outputText value="#{page}"/>
</h:column>
</h:dataTable>

'Go' button at the top is supposed to do the same thing as the 'the same go action?' 顶部的“执行”按钮应该与“相同的执行动作”执行相同的操作? buttons. 纽扣。

Backing WierdBean is: 支持WierdBean的是:

public class WierdBean implements Serializable {

private int buttonsCount;

public WierdBean() {
System.out.println("WierdBean()");
}

@PostConstruct
public void postConstruct() {
System.out.println("postConstruct()");
}

public Integer[] getPages() {
System.out.print("getPages() buttonsCount(): " + buttonsCount);
Integer[] pages = new Integer[buttonsCount];
for (int i = 0; i < pages.length; i++) {
pages[i] = new Integer(i);
}
return pages;
}

public String doWierdStuff() {
System.out.println("doWierdStuff()");
buttonsCount = 2;
return "wierd";
}
}

When I enter the page I get: 当我进入页面时,我得到:

INFO: WierdBean()
INFO: postConstruct()
INFO: getPages() buttonsCount(): 0 (16 times)

and I seen only the 'Go' button. 而且我只看到“开始”按钮。 That's understandable. 这是可以理解的。

After Pressing the 'Go' button I get: 按下“开始”按钮后,我得到:

INFO: WierdBean() 
INFO: postConstruct() 
INFO: getPages() buttonsCount(): 0 (19 times) 
INFO: doWierdStuff() 
INFO: getPages() buttonsCount(): 2 (16 times)

Nice, doWierdStuff is called and then I get 2 'the same go action' buttons and 2 text outputs. 很好,调用了doWierdStuff,然后得到2个“相同的go action”按钮和2个文本输出。 That's fine. 没关系。

However, when I press any of the the 'the same action' buttons, which are supposed to do the same thing as 'Go' button - call the doWierdStuff method - I get: 但是,当我按下“相同动作”按钮中的任何一个时,它们应该执行的功能与“转到”按钮相同-调用doWierdStuff方法-我得到:

INFO: WierdBean() 
INFO: postConstruct() 
INFO: getPages() buttonsCount(): 0 (44 times)

There is only 'Go' button visible. 仅显示“开始”按钮。

Why is that? 这是为什么?

Your concrete problem is two-fold: the bean is clearly request scoped instead of view scoped and you're doing business job inside a getter method instead of the (post)constructor or an (action)listener method. 您的具体问题有两个方面:显然,bean是请求范围的,而不是视图范围的,并且您正在使用getter方法而不是(post)构造函数或(action)侦听器方法进行业务工作。

A request scoped bean get recreated on every individual request and is not reused on subsequent requests on the same view. 在每个单独的请求上都会重新创建请求范围的Bean,并且不会在同一视图上的后续请求上重用该请求范围的Bean。 All properties like buttonsCount are reinitialized to their defaults. 所有属性(如buttonsCount都将重新初始化为其默认值。 When clicking a command link/button in a datatable, you need to make sure that exactly the same datamodel is been preserved as in initial request. 单击数据表中的命令链接/按钮时,需要确保保留与初始请求中完全相同的数据模型。 If you don't do that, JSF won't be able to find the action to be invoked. 如果不这样做,JSF将无法找到要调用的动作。 The bean needs to live as long as you're interacting with the same view by returning null or void . 只要您通过返回nullvoid与同一个视图进行交互,bean就必须存在。 This can be achieved by putting the bean in the view scope by @ViewScoped (or when you're still on obsolete JSF 1.x, by Tomahawk's <t:saveState> component). 这可以通过使用@ViewScoped (或当您仍使用Tomahawk的<t:saveState>组件使用过时的JSF 1.x)将bean放在视图范围中来实现。

A backing bean getter method should only return data which is already prepared beforehand, not to prepare the data itself. 支持bean getter方法应仅返回事先准备好的数据,而不是准备数据本身。 The getter method is supposed to solely be an access point to the data. 假定该getter方法仅是数据的访问点。 This is because the getter method can be called multiple times during a request-response cycle, especially if referenced in an UIData component and/or the rendered attribute. 这是因为在请求-响应周期中,可以多次调用getter方法,尤其是在UIData组件和/或rendered属性中引用该UIData

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM