Show variable value in the dialog - jsf

I need show a value in an dialog after execute a method that I invoke in the commandbutton but the dialog don't show the variable value.
The variable value is calculate inside the commandbutton.
Managed bean
#ManagedBean
#ViewScoped
public class OrderBean implements Serializable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void eventicus(ActionEvent event) {
this.name ="Value";
}
}
xhtml document
<h:commandButton actionListener="#{orderBean.calculate()}" value="Show" oncomplete="PF('dlg2').show();" />
<p:dialog id="modalDialog" header="Modal Dialog" widgetVar="dlg2" >
<h:panelGrid columns="2" style="margin-bottom:10px">
<h4><h:outputText value="#{orderBean.name}" /></h4>
</h:panelGrid>
<p:commandButton id="submitButton" value="Submit" oncomplete="PF('dlg2').hide();"/>
</p:dialog>
When I click in the commandbutton the dialog is empty.

1 I think you need to add a [update="modalDialog"] attribute inside your commandButton

Related

Validate form when checking p:selectBooleanCheckbox [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
How to perform JSF validation in actionListener or action method?
(5 answers)
JSF doesn't support cross-field validation, is there a workaround?
(2 answers)
Closed 2 years ago.
I spent some days to try to validate part form with a OneSelect like as "Terms and Conditions", for example If are pre-filled Name and Telephone and I selected "Terms and Conditions" in this moment validate the previous fields are filled and enable "Next" button.
I tried using two ways, the first I put p:attribute inside p:selectOneRadio and received in method validateSelection() always null, the second is used validateSelection(String name, String telephone), but both methods show null values,
Does anyone know why this happens?
Thanks a lot.
(this is my view)
<h:form id="frmUser" enctype="multipart/form-data">
<p:outputLabel id="lblName" for="txtName" value="#{msg.lbl_name}" styleClass="lbl8"/>
<p:inputText id="txtName" maxlength="7" required="true" value="#{mainBean.userDTO.name}"/>
<p:message for="txtName" display="text"/>
<p:outputLabel id="lblTel" for="lblTel" value="#{msg.lbl_tele}" />
<p:inputText id="lblTel" maxlength="7" required="true" value="#{mainBean.userDTO.telephone}"/>
<p:message for="lblTel" display="text"/>
<p:outputLabel for="console" value="Accept Terms and Conditions" />
<p:selectOneRadio id="console" unselectable="true" value="#{mainBean.selectTerms}">
<f:selectItem itemLabel="Yes" itemValue="true" />
<f:selectItem itemLabel="No" itemValue="false" />
<f:attribute name="setname" value="#{mainBean.userDTO.name}" />
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection}" />
<!--WAY TWO -- >
<!--
<p:ajax event="change" render="messagesSystem" update="messagesSystem" listener="#{mainBean.validateSelection(mainBean.userDTO.name,mainBean.userDTO.telephone)}" />
-->
</p:selectOneRadio>
</h:form>
This is my Bean
#Named("mainBean")
#SessionScoped
public class mainBean implements Serializable{
private UserDTO userDTO;
public String init() {
userDTO = new UserDTO();
//call url to display
}
/*
* Getters y Setters
* */
public UserDTO getUserDTO() {
return userDTO;
}
public void setUserDTO(UserDTO userDTO) {
this.userDTO = userDTO;
}
public void validateSelection(AjaxBehaviorEvent event) {
System.out.println("fill name"+ userDTO.getName());
System.out.println("fill name in ajax event :"+event.getComponent().getAttributes().get("setname"));
UIComponent component = event.getComponent();
System.out.println("fill name in ajax event component:"+
component.getAttributes().get("setname"));
//If two values are filled, eneabled NEXT BUTTON
}
public void validateSelection(String name, String telephone){
System.out.println("fill name"+name);
System.out.println("fill telephone"+telephone);
//If two values are filled, eneabled NEXT BUTTON
}
}
And this is the UserDTO class
public class UserDTO implements Serializable{
private String name;
private String telephone;
public UserDTO(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

update p:dialog with dynamic title via #widgetVar('...')

I'm trying to set a dialog title at runtime (dynamically) but update with #widgetVar expression cannot do the trick. Any ideas?
The first command button renders the dialog with the dynamic title but the second command fail to render the title! Why? Here the example is simplified but the real page is more complex and it is difficult to specify the id of the dialog to render.
<h:form>
<p:commandButton value="Basic" ajax="true" process="#this"
update=":dlg1" oncomplete="PF('widget-dialog').show();"
actionListener="#{dialogView.changeTitle('Dynamic title')}" />
<p:commandButton value="Widget" ajax="true" process="#this"
update="#widgetVar('widget-dialog')"
oncomplete="PF('widget-dialog-1').show();"
actionListener="#{dialogView.changeTitle('Dynamic title')}" />
</h:form>
<p:dialog id="dlg1" header="#{dialogView.title}"
widgetVar="widget-dialog" dynamic="true">
<h:outputText value="Resistance to PrimeFaces is futile!!!" />
</p:dialog>
#ManagedBean(name = "dialogView")
#ViewScoped
public class DialogView {
private String title = null;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void changeTitle(String title) {
setTitle(title);
}
}

Encountered Exception while using ui:repeat [duplicate]

I am trying to add a PrimeFaces <p:tab> dynamically. While adding the second tab I am getting the following exception:
"java.lang.IllegalStateException: Component ID tab0 has already been found in the view".
How can I solve this?
Here is the view code:
<h:form prependId="false">
<p:tabView id="tabview" dynamic="true" cache="false"
binding="#{testBean.tabView}"
activeIndex="#{testBean.activeTab}" >
<h:commandButton value="Close" action="#{testBean.removeTab}"/>
</p:tabView>
<h:commandButton value="Add Tab" action="#{testBean.addTab}"/>
</h:form>
Here is the bean code:
public String addTab() {
String tabId="tab"+id;
System.out.println("Gen Id: "+tabId);
tab = new Tab();
tab.setTitle("Title: "+tabId);
tab.setId(tabId);
System.out.println("Tab Id: "+tab.getId());
tabView.getChildren().add(id,this.tab);
id++;
return "tabtest.jsf";
}
public String removeTab() {
tabView.getChildren().remove(activeTab);
return "tabtest.jsf";
}
Don't manually create components if everything can just be done in the view. This construct will fail if the bean is in a broader scope than the request scope. See also e.g. Binding attribute causes duplicate component ID found in the view
Follow the showcase example "TabView with Model" which allows you to dynamically populate tabs via a sane model and <p:tabView value="..." var="..."> like as <ui:repeat>/<h:dataTable>.
E.g. this view
<h:form>
<p:tabView value="#{bean.tabs}" var="tab">
<p:tab title="#{tab.title}">
#{tab.content}
<p:commandButton value="Close" action="#{bean.remove(tab)}" update="#form" />
</p:tab>
</p:tabView>
<p:commandButton value="Add Tab" action="#{bean.add}" update="#form" />
</h:form>
with this controller
#ManagedBean
#ViewScoped
public class Bean implements Serializable {
private List<Tab> tabs;
#PostConstruct
public void init() {
tabs = new ArrayList<>();
}
public void add() {
tabs.add(new Tab("tab" + tabs.size(), "some content"));
}
public void remove(Tab tab) {
tabs.remove(tab);
}
public List<Tab> getTabs() {
return tabs;
}
}
and this model
public class Tab {
private String title;
private String content;
public Tab(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
This is because same ID is being generated for new tab(The one which ur adding). To avoid this, append a variable to the id as
<p:tabView id="tabview_#{testBean.i}" dynamic="true" cache="false"binding="#{testBean.tabView}"
activeIndex="#{testBean.activeTab}" >
<h:commandButton value="Close" action="#{testBean.removeTab}"/>
</p:tabView>

How to get dynamically rendered <h:inputText> value in back end in JSF

I am able to render dynamic but don't know how to get those dynamically created values in back end.
test.xhtml
<h:form>
Insert Your Desire Number : <h:inputText value="#{bean.number}">
<f:ajax listener="#{bean.submitAjax}" execute="#form" render="#form" event="keyup" />
</h:inputText>
<br></br>
<h:outputText value="#{bean.text}" />
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:inputText value="#{item.value}" />
</h:column>
</h:dataTable>
<h:commandButton value="Submit" action="#{item.submit}" />
</h:form>
If I render 3 input boxes, and when I submit the button i get the last value only, Can someone guide me how can i
Bean.java
#ManagedBean(name="bean")
#SessionScoped
public class Bean {
private int number;
private List<Item> items;
private Item item;
//getter and setter are omitted
public void submitAjax(AjaxBehaviorEvent event)
{
items = new ArrayList<Item>();
for (int i = 0; i < number; i++) {
items.add(new Item());
}
}
}
Item.java
private String value;
//getter and setter are omitted
public void submit() {
System.out.println("Form Value : "+value);
}
Your submit() method is in the wrong place. You should put it on the managed bean, not on the entity.
Thus so,
<h:commandButton value="Submit" action="#{bean.submit}" />
with
public void submit() {
for (Item item : items) {
System.out.println(item.getValue());
}
}
or more formally,
#EJB
private ItemService service;
public void submit() {
service.save(items);
}

How to dynamically add and remove a tab in p:tabView component

I am trying to add a PrimeFaces <p:tab> dynamically. While adding the second tab I am getting the following exception:
"java.lang.IllegalStateException: Component ID tab0 has already been found in the view".
How can I solve this?
Here is the view code:
<h:form prependId="false">
<p:tabView id="tabview" dynamic="true" cache="false"
binding="#{testBean.tabView}"
activeIndex="#{testBean.activeTab}" >
<h:commandButton value="Close" action="#{testBean.removeTab}"/>
</p:tabView>
<h:commandButton value="Add Tab" action="#{testBean.addTab}"/>
</h:form>
Here is the bean code:
public String addTab() {
String tabId="tab"+id;
System.out.println("Gen Id: "+tabId);
tab = new Tab();
tab.setTitle("Title: "+tabId);
tab.setId(tabId);
System.out.println("Tab Id: "+tab.getId());
tabView.getChildren().add(id,this.tab);
id++;
return "tabtest.jsf";
}
public String removeTab() {
tabView.getChildren().remove(activeTab);
return "tabtest.jsf";
}
Don't manually create components if everything can just be done in the view. This construct will fail if the bean is in a broader scope than the request scope. See also e.g. Binding attribute causes duplicate component ID found in the view
Follow the showcase example "TabView with Model" which allows you to dynamically populate tabs via a sane model and <p:tabView value="..." var="..."> like as <ui:repeat>/<h:dataTable>.
E.g. this view
<h:form>
<p:tabView value="#{bean.tabs}" var="tab">
<p:tab title="#{tab.title}">
#{tab.content}
<p:commandButton value="Close" action="#{bean.remove(tab)}" update="#form" />
</p:tab>
</p:tabView>
<p:commandButton value="Add Tab" action="#{bean.add}" update="#form" />
</h:form>
with this controller
#ManagedBean
#ViewScoped
public class Bean implements Serializable {
private List<Tab> tabs;
#PostConstruct
public void init() {
tabs = new ArrayList<>();
}
public void add() {
tabs.add(new Tab("tab" + tabs.size(), "some content"));
}
public void remove(Tab tab) {
tabs.remove(tab);
}
public List<Tab> getTabs() {
return tabs;
}
}
and this model
public class Tab {
private String title;
private String content;
public Tab(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
This is because same ID is being generated for new tab(The one which ur adding). To avoid this, append a variable to the id as
<p:tabView id="tabview_#{testBean.i}" dynamic="true" cache="false"binding="#{testBean.tabView}"
activeIndex="#{testBean.activeTab}" >
<h:commandButton value="Close" action="#{testBean.removeTab}"/>
</p:tabView>

Resources