I have the following dataTable:
<h:form>
<h:dataTable id = "notTable" value="#{editCategoryBean.allNotifications}" var="notification">
<h:column>
<f:facet name="header">Key</f:facet>
<h:inputText id = "notkey" value="#{notification.key}" />
</h:column>
<h:column>
<f:facet name="header">Lang</f:facet>
<h:inputText id = "notlanguage" value="#{notification.language}"/>
</h:column>
<h:column>
<f:facet name="header">Value</f:facet>
<h:inputText id = "notvalue" value="#{notification.value}"/>
</h:column>
</h:dataTable>
<h:commandButton action ="#{editCategoryBean.save()}" value = "Save" > </h:commandButton>
I want to edit my notifications from the allNotifications List in the datatable and save all changes with one button click. How can I iterate through the datatable from the editCategoryBean? Or how can I implement this behaviour otherwise?
JSF has already updated the model behind value="#{editCategoryBean.allNotifications}" with the submitted values the usual way. So, all you basically need to do is to pass it through to the service layer for save:
public void save() {
yourNotificationService.save(allNotifications);
}
Otherwise, if you really insist in iterating over it yourself for some reason, maybe to print the submitted values for testing purposes, then just do it the usual Java way:
public void save() {
for (Notification notification : allNotifications) {
System.out.println(notification.getKey());
System.out.println(notification.getLanguage());
System.out.println(notification.getValue());
}
}
Related
I have the following dataTable:
<h:form>
<h:dataTable id = "notTable" value="#{editCategoryBean.allNotifications}" var="notification">
<h:column>
<f:facet name="header">Key</f:facet>
<h:inputText id = "notkey" value="#{notification.key}" />
</h:column>
<h:column>
<f:facet name="header">Lang</f:facet>
<h:inputText id = "notlanguage" value="#{notification.language}"/>
</h:column>
<h:column>
<f:facet name="header">Value</f:facet>
<h:inputText id = "notvalue" value="#{notification.value}"/>
</h:column>
</h:dataTable>
<h:commandButton action ="#{editCategoryBean.save()}" value = "Save" > </h:commandButton>
I want to edit my notifications from the allNotifications List in the datatable and save all changes with one button click. How can I iterate through the datatable from the editCategoryBean? Or how can I implement this behaviour otherwise?
JSF has already updated the model behind value="#{editCategoryBean.allNotifications}" with the submitted values the usual way. So, all you basically need to do is to pass it through to the service layer for save:
public void save() {
yourNotificationService.save(allNotifications);
}
Otherwise, if you really insist in iterating over it yourself for some reason, maybe to print the submitted values for testing purposes, then just do it the usual Java way:
public void save() {
for (Notification notification : allNotifications) {
System.out.println(notification.getKey());
System.out.println(notification.getLanguage());
System.out.println(notification.getValue());
}
}
I want to add a row at the end of my rich:datatable that shows information
and in this row I want to put some input that enable user to add new data
I read this answer :
RICH:dataTable - add new row
but for me is a little different
I don't have any list
my xhtml is like this :
<rich:dataTable value="#{serverMB.allServer}" var="servers1" iterationStatusVar="it" id="table" rows="15"
style="direction: rtl ; text-align: right">
<rich:column>
<f:facet name="header">#</f:facet>
#{it.index+1}
</rich:column>
<f:facet name="header">
<h:outputText value="#{msgs.servers}"/>
</f:facet>
<rich:column filterValue="#{serverMB.serverNameFilter}"
filterExpression="#{fn:containsIgnoreCase(servers1.server_name,serverMB.serverNameFilter )}">
<f:facet name="header">
<h:outputText value="#{msgs.server_name}"/>
<h:inputText value="#{serverMB.serverNameFilter}">
<a4j:ajax event="blur" render="table" execute="#this"/>
</h:inputText>
</f:facet>
<h:outputText value="#{servers1.server_name}"/>
</rich:column>
.
.
.
and my managedbean is like this
public List<Server> getAllServer() {
return serverFacade.findAll();
}
I use this method to show information so there is not any list that I personally can add a row to it and so on ...
so what I must do ?
Your serverList should be populated and set as a class-level variable. Use an #PostConstruct method to populate the list:
#PostConstruct
public void initServerList(){
//declare serverList as a class-scoped variable in your managed bean
serverList = serverFacade.findAll();
}
Declare a method in your managed bean that will add to the serverList
public void incrementList(){
Server server = new Server(); //create a dummy Server object
serverList.add(server); // List has been incremented. Be sure to AJAX-refresh the datatable after calling this method to reflect the new row.
}
Collect information from the JSF view which you'll then use to fill the fields of the dummy object:
public void updateLatestServer(){
Server lastServer = serverList.get(serverList.size-1);
lastServer.setName(serverNameFromView);
lastServer.setIP(IPFromView);
}
The serverNameFromView and IPFromView are hypothetical variables you'll collect in your JSF Form.
Beginner question concerning changing the information in a view (and not going to a different .xhtml page). My .xhtml displays a datatable (which shows a list of people), but I want the user to be able to narrow the amount of info displayed by typing in the start of someones name into an inputtext and then clicking the commandbutton. Both the commandbutton and the datatable execute the same bean method, but if the inputtext has data the sql uses Like (this uses jdbc). My form is below, the problem is that I get an "Unable to find matching navigation case with from-view-id" because my managed bean returns a list of objects, not "success" or "failure". Also, the whole form seems inefficient. This seems like a fairly common scenario - calling managed bean's method that returns objects and not navigation instructions. How do I get rid of the error messages? Is there a better way to do this?
Here is code:
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>MavenWeb</h1>
<h:form>
<h:panelGrid columns="3">
Select a customer:
<h:inputText id="listName" value="#{customer.listName}"
size="20" >
</h:inputText>
<h:commandButton value="Submit"
action="#customer.getCustomerList()}" />
</h:panelGrid>
<h:dataTable value="#{customer.getCustomerList()}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerID}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>
<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>
<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.created_date}
</h:column>
</h:dataTable>
</h:form>
</h:body>
First, make sure your managed bean has ViewScope, so only the form values will be affected on every request while you're in the same view. Second, you can add ajax behavior to your <h:commandButton> and render the <h:datatable> with the new values. Third, never put business logic inside your attribute getters, because the JSF framework could make 2 or more calls of those getters (depending on your design).
Using these rules, you can remake your code like this:
#ViewScoped
#ManagedBean
public class Customer {
private String listName;
private List<CustomerDTO> customerList;
public Customer() {
listName = "";
customerList = null; //maybe you can initialize your list here
}
//getters and setters...
//look that this method returns a void (no need to return a navigation rule)
public void fillCustomerList() {
//your method/way to fill the customerList goes here...
//I'll just put a code example
customerList = new ArrayList<CustomerDTO>();
customerList.add(new CustomerDTO(1, "Luiggi Mendoza", "Lima", new Date());
customerList.add(new CustomerDTO(2, "StackOverflow", "Web", new Date());
}
}
The code fragment for your page
<h:form>
<h:panelGrid columns="3">
Select a customer:
<h:inputText id="listName" value="#{customer.listName}" size="20" />
<h:commandButton value="Submit" action="#customer.fillCustomerList}">
<f:ajax execute="#this" render="dtMyDataTable" />
</h:commandButton>
</h:panelGrid>
<h:dataTable id="dtMyDataTable" value="#{customer.customerList}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerID}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>
<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>
<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.created_date}
</h:column>
</h:dataTable>
</h:form>
More info on this matter:
Does view scope bean survive Navigation JSF
Learning JSF2: Ajax in JSF – using f:ajax tag
How can I pass user entered values from/to JSP where I have used JSF as framework? Here is my code:
<h:dataTable id="dt1" value="#{Ack.list}" var="ack" >
<h:column>
<f:facet name="header">
<h:outputText style=""value=" Number" />
</f:facet>
<h:outputText style="" value="#{ack.number}" ></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Acknowledgement"/>
</f:facet>
<h:inputText value="" > </h:inputText>
</h:column>
<h:column>
<h:outputText value=""/>
<h:commandButton action="" value="Submit"></h:commandButton>
</h:column>
</h:dataTable>
I want to enter some value in the textfield provided for "acknowledgement" and click on submit button. How can I send the user entered "acknowledgment" on button click?
First add a property acknowledgement to the class Ack (at least, the class which represents each item of the datatable, as declared in var attribute of the table).
public class Ack {
private String acknowledgement;
// Add/generate getter and setter.
}
Then bind the input field to that.
<h:inputText value="#{ack.acknowledgement}" />
Ensure that the table is already placed inside a <h:form> and I'd also move the command button outside the table, one button is enough. Bind its action to a backing bean method.
<h:form>
<h:dataTable ...>
...
</h:dataTable>
<h:commandButton action="#{Ack.submit}" value="Submit" />
</h:form>
Finally define the method in the bean. At that point, JSF has already set the submitted values. You just have to save them in the DB or something.
public String submit() {
ackDAO.save(list);
return "outcome";
}
How can I add in commandbutton inside datatable?
<hx:dataTableEx value="#{searchData.searchFriends}" var="s">
<hx:columnEx>
<f:facet name="header">
<h:outputText value="First Name" />
</f:facet>
<hx:requestLink action="#{pc_Search.doAddFriendAction}">
<h:outputText value="Add as Friend" />
<f:param name="friendId" value="#{s.memberId}" />
</hx:requestLink>
</hx:columnEx>
</hx:dataTableEx>
To get the data at backend
String friendId = (String)getRequestParam().get("friendId");
But once I change the requestlink to command button the friedId = null? any idea how can i pass value using command button
Wrap the datatable value in a DataModel. Then you can obtain the selected row by DataModel#getRowData().
public class Bean {
private List<Friend> friends;
private DataModel friendsModel;
public Bean () {
friends = getItSomehow();
friendsModel = new ListDataModel(friends);
}
public void addAsFriend() {
Friend selectedFriend = (Friend) friendsModel.getRowData();
// ...
}
}
with
<h:dataTable value="#{bean.friendsModel}" var="friend">
<h:column>
<h:commandButton value="Add as friend" action="#{bean.addAsFriend}" />
</h:column>
</h:dataTable>
Should work as good with IBM Faces Client Framework (those hx: components).