I am using JSF 2.1.7 and Myfaces CODI 1.0.5 on JBoss AS 7.1.1. My <h:commandButton> is not working. I have read the requirements and have through examples in many blogs all to no avail. My facelets code is as follows
<ui:define name="pagecontent">
<h1 class="title ui-widget-header ui-corner-all">Upload Bulk Contact File</h1>
<div class="entry">
<h:form enctype="multipart/form-data" id="upload">
<p:panel closable="false" collapsed="false" header="Excel Contact Uploader"
id="pnlupload" rendered="true" toggleable="false" visible="true" widgetVar="pnlupload">
<p:growl id="msg" showDetail="true" life="3000" showSummary="true"/>
<p:fileUpload auto="true"
allowTypes="/(\.|\/)(xls)$/"
sizeLimit="1024000"
mode="advanced"
multiple="true" invalidFileMessage="Invalid file type" invalidSizeMessage="File too
large" dragDropSupport="true"
fileUploadListener="#{excelFileController.handleFileUpload}" showButtons="true"
update="msg, tblcontacts" required="false"/>
<p:scrollPanel rendered="true" style="height:200px;">
<p:dataTable draggableColumns="false" editable="false" emptyMessage="No
Contacts Uploaded" id="tblcontacts" rendered="true" rows="8"
selection="#{excelFileController.contactsSelected}"
value="#{excelFileController.contactDataModel}" var="contact" style="width:50pc;">
<p:column selectionMode="multiple" style="width:18px" />
<p:column headerText="File Name">
#{contact.groupName}
</p:column>
<p:column headerText="Number of Contacts">
#{contact.numberofentries}
</p:column>
<p:column>
<h:button outcome="blkedit?faces-redirect=true" rendered="true" value="Edit">
<f:param name="contact" value="#{contact.contactId}"/>
</h:button>
</p:column>
</p:dataTable>
</p:scrollPanel>
<br />
</p:panel>
<h:commandButton value="Delete" id="btndelete"
action="#{excelFileController.removeContact}" type="button" immediate="true"
disabled="false" rendered="true"/>
<h:message for="btndelete" />
</h:form>
</div>
</ui:define>
The code for ExcelFileController is as follows:
@Named
@ViewAccessScoped
public class ExcelFileController implements Serializable, IFileController {
/**
*
*/
private static final long serialVersionUID = -8117258104485487921L;
@Inject
PhoneNumberFormatter formatter;
@Inject
@Authenticated
UserProfile profile;
public PhoneNumberFormatter getFormatter() {
return formatter;
}
public void setFormatter(PhoneNumberFormatter formatter) {
this.formatter = formatter;
}
@EJB
BulkContactDeleter deleter;
@Inject
Logger logger;
@Inject
@CurrentContext
FacesContext context;
@Inject
BulkSMSContactListProducer listProducer;
@Inject
ConfigurationListProducer producer;
private BulkSMSContacts[] contactsSelected;
private BulkContactDataModel contactDataModel;
public BulkSMSContacts[] getContactsSelected() {
return contactsSelected;
}
public void setContactsSelected(BulkSMSContacts[] contactsSelected) {
this.contactsSelected = contactsSelected;
}
public BulkContactDataModel getContactDataModel() {
return contactDataModel;
}
@PostConstruct
public void init() {
logger.log(Level.INFO, "Entering excel file controller");
contactDataModel = new BulkContactDataModel(
listProducer.getBulkSMSContacts());
}
/*
* (non-Javadoc)
*
* @see
* org.jboss.tools.examples.controller.IFileController#handleFileUpload(
* org.primefaces.event.FileUploadEvent)
*/
@Override
public void handleFileUpload(FileUploadEvent event) {
StringBuffer buffer = new StringBuffer();
// create a new file input stream with the input file specified
// at the command line
InputStream fin = null;
try {
fin = event.getFile().getInputstream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// create a new org.apache.poi.poifs.filesystem.Filesystem
POIFSFileSystem poifs = null;
try {
poifs = new POIFSFileSystem(fin);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(poifs);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int numberofsheets = wb.getNumberOfSheets();
for (int i = 0; i < numberofsheets; i++) {
HSSFSheet sheet = wb.getSheetAt(i);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING :
if (!cell.getStringCellValue().isEmpty())
buffer.append(formatter.formatPhoneNumber(cell
.getStringCellValue()));
buffer.append(producer.getConfiguration(
SettingsName.SMS_PHONENUMBERDELIMITER
.toString()).getValue());
break;
case Cell.CELL_TYPE_NUMERIC :
if (cell.getNumericCellValue() != 0) {
buffer.append(formatter
.formatPhoneNumber(String.valueOf(cell
.getNumericCellValue())));
buffer.append(producer.getConfiguration(
SettingsName.SMS_PHONENUMBERDELIMITER
.toString()).getValue());
break;
}
default :
break;
}
}
}
}
BulkSMSContacts contacts = new BulkSMSContacts();
contacts.setAccount(profile.getSmsAccount());
int number = formatter.splitPhoneNumbers(buffer.toString()).length;
contacts.setContacts(buffer.toString());
String filenameString = event.getFile().getFileName();
int index = filenameString.indexOf(".");
filenameString = filenameString.substring(0, index);
contacts.setGroupName(filenameString);
contacts.setNumberofentries(number);
try {
deleter.addContact(contacts);
List<BulkSMSContacts> temp = listProducer.getBulkSMSContacts();
contactDataModel.setWrappedData(temp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Success", number
+ " entries processed. Please refresh page to view"));
}
/*
* (non-Javadoc)
*
* @see org.jboss.tools.examples.controller.IFileController#removeContact()
*/
@Override
public String removeContact() {
int contactsdeleted = 0;
if (contactsSelected != null) {
for (BulkSMSContacts contacts : contactsSelected) {
if (contacts != null) {
deleter.deleteContact(contacts);
contactsdeleted += 1;
}
}
List<BulkSMSContacts> temp = listProducer.getBulkSMSContacts();
contactDataModel.setWrappedData(temp);
logger.log(Level.INFO, "Deleted " + contactsdeleted + " Contacts");
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Success", contactsdeleted
+ " entries where deleted successfully"));
} else {
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error",
"No contact file was selected!"));
}
return null;
}
}
All the methods work fine except for the last one "removeContact" where the CommandButton concerned does not even initiate a postback.
How is this caused and how can I solve it?
You need to remove type="button" from the <h:commandButton>. It should have been type="submit", which is the default already.
The type="button" makes it an <input type="button"> instead of <input type="submit"> which is only useful for client side handlers which you usually attach using onclick and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With