Skip to main content

CommandButton does not work in DataTable

6 replies [Last post]
wlmang
Offline
Joined: 2010-10-19
Points: 0

After I enter something in the search criteria and press find. The table display the rows but then Edit command button does not seem to work.

Any of you know why? I am using JSF 2 mojarra 2.0.3 and GlassFish v3.

Thanks

I have the following ManagedBean and web page:

package example.ui.web;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("testFinderAM")
@Scope("request")
public class TestFinder {
private String searchCriteria;

public String getSearchCriteria() {
return searchCriteria;
}

public void setSearchCriteria(String searchCriteria) {
this.searchCriteria = searchCriteria;
}

public String find() {
return null;
}

public String edit() {
return null;
}

public List getAddresses() {
List addresses = new ArrayList();
if (hasSearchCriteria()) {
for (int i = 1; i <= 50; i++) {
addresses.add(createAddressUsing(i));
}
}
return addresses;
}

public SimpleAddress createAddressUsing(Integer id) {
return new SimpleAddress(id, "Room " + id, id + " Simple Road", "Shanghai", "Shanghai", "China");
}

public boolean hasSearchCriteria() {
return searchCriteria != null && !searchCriteria.isEmpty();
}
}

Action

Address Line 1
#{address.line1}

Address Line 2
#{address.line2}

City
#{address.city}

State
#{address.state}

Country
#{address.country}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
andrewlaughlin
Offline
Joined: 2010-03-19
Points: 0

Can you try a JavaScript debugger (e.g. Firebug) and see if an error is given when clicking 'Edit'?

wlmang
Offline
Joined: 2010-10-19
Points: 0

Yes, I have looked at the request and response after clicking the 'Edit' button in FireBug. There is no javascript error. I have also put a break point at "getAddresses()". I do get the break point but it never call the 'edit' method.

Thanks for replying.

andrewlaughlin
Offline
Joined: 2010-03-19
Points: 0

hmmm... not sure why it wouldn't fire. You could try removing the 'immediate' attribute from the edit command button. It may be that your request-scoped bean isn't yet created when the JSF run-time attempts to call the edit method. Since the immediate attribute is set, JSF will attempt to call the bean method during the 'Apply Request Values' phase of processing and not during the 'Invoke Application' phase where it would normally be called.

wlmang
Offline
Joined: 2010-10-19
Points: 0

Actually, I have tried removing the immediate attribute. That's what it was initially before I added it to see if it will make any difference.

Again, thanks for replying.

Mang

andrewlaughlin
Offline
Joined: 2010-03-19
Points: 0

Then at this point I would download the Mojarra 2.0.3 source and plug it into your IDE. For example, I use NetBeans and have added the path to the Mojarra source code under project libraries. Then watch the logs (IDE and/or EE container) during the request and see if any exceptions are thrown. There are places in the current Mojarra code base where it eats exceptions or replaces them with a less descriptive exceptions. For example see http://forums.java.net/jive/message.jspa?messageID=481625#481625

Andrew

wlmang
Offline
Joined: 2010-10-19
Points: 0

Ok, I have spend quite sometime debugging this. And I think I have more understanding on what is going on here.

I notice that whenever a button is pressed, it is recorded in the requestParameterMap.
During the ApplyRequestValuePhase, this button pressed is converted to an Action event which is enqueue to an event queue in the UIViewRoot.

Normally, this should be ok. But for a DataTable, it seems like during the restore phase there is no rows in the DataTable and the DataTable needs to get the rows again.

If it tries to get the rows during the ApplyRequestValuePhase, it will get nothing because the ManagedBean does not have all the required information to get the rows. Hence, no Action event will be created for the 'Edit' button pressed.

I found that one way to fix this is to use f:param in the command button to pass the search criteria information which is needed to build the rows. And add a method in the Managed Bean to get the search criteria information from the request parameters whenever it needs to get the rows.

After this two changes, it seems to work. But It seems more complicated than it needs to be. I guess it is more complicated maybe because I am using a request scope. If I use a view scope or higher then I properly will not have this problem.

I like to use the request scope so I am wondering is there any other better solution or cleaner solution. Or I am doing something incorrect here.

I also noticed that the method for getting the rows get called quite a lot of times, e.g. 3 times in ApplyRequestValuePhase, 3 times in ProcessValidationsPhase, 3 times in UpdateModelValuesPhase and 3 times in RenderResponsePhase. I did not look in to the details why it needs to call so many times.

Thanks