I have the following Java code:
#Named
#SessionScoped
public class TestClass implements Serializable {
private String stringValue;
#PostConstruct
private void init() {
initStringValueDefault();
}
public void initStringValue1() {
stringValue = "ABCD";
}
public void initStringValue2() {
stringValue = "EFGH" + '\u001c';
}
public void initStringValueDefault() {
stringValue = "LABEL TEXT";
}
public String getStringValue() {
return stringValue;
}
}
and .xhtml
<h:form id="testForm" prependId="true">
<h:outputText id="stringValueLabel" value="#{testClass.stringValue}" escape="false"/>
<br/>
<h:commandButton value="Set ABCD" actionListener="#{testClass.initStringValue1()}">
<f:ajax execute="#form" render="stringValueLabel"/>
</h:commandButton>
<span style="margin-left: 10px"/>
<h:commandButton value="Set EFGH + \u001c" actionListener="#{testClass.initStringValue2()}">
<f:ajax execute="#form" render="stringValueLabel"/>
</h:commandButton>
<span style="margin-left: 10px"/>
<h:commandButton value="Set default" actionListener="#{testClass.initStringValueDefault()}">
<f:ajax execute="#form" render="stringValueLabel"/>
</h:commandButton>
</h:form>
When I click on the 'Set ABCD' button, update works successfully. But after click on the 'Set EFGH + \u001c' I get errors:
Chrome: 'The page at localhost:8080 says: emptyResponse: An empty response was received from the server.'
Firefox: 'malformedXML: XML Parsing Error: not well-formed'
and the label haven't been updated. And after page reload the label updates to 'EFGH'.
Anybody knows, why due to adding '\u001c' to the variable, update doesn't work after button click?
With escape="true" everything works fine, but I need escape="false".
The problem was solved by treating the variable with method escapeXml11(String input) of org.apache.commons.lang3.StringEscapeUtils:
import static org.apache.commons.lang3.StringEscapeUtils.escapeXml11;
...
public void initStringValue2() {
stringValue = "EFGH" + '\u001c';
stringValue = escapeXml11(stringValue);
}
...
Related
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);
}
}
This question already has answers here:
How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)
(3 answers)
Closed 6 years ago.
I try to update component panelgroup after clicking on commandLink, but it doesn't succeed. I have already tryed many ways but nothing helps. Please, say me, what should I do that to update panelgroup?
<h:panelGroup layout="block" id="content">
<h:panelGroup layout="block" id="nav">
<h:form id="itemsMenu">
<h:commandLink value="View" update="workplace" actionListener="#{main.determineAction}">
<f:param name="link" value="Viewing telephone book"/>
</h:commandLink>
</h:form>
</h:panelGroup>
<h:panelGroup id="workplace">
<h:panelGroup layout="block" rendered="#{main.responseRendered}">
<ui:include src="#{main.linkPage}"/>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
Bean code:
#ManagedBean(name = "main")
#ViewScoped public class MainBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<String> listItemsMenu;
private String linkPage;
private boolean responseRendered = false;
public boolean isResponseRendered() {
return responseRendered;
}
public void setResponseRendered(final boolean responseRendered) {
this.responseRendered = responseRendered;
}
public String getLinkPage() {
return linkPage;
}
public void setLinkPage(final String linkPage) {
this.linkPage = linkPage;
}
public void determineAction(final ActionEvent event) {
final Locale currentLocale = SessionBean.getCurrentLocale();
final MessageManager messageManager = new MessageManager(currentLocale);
final Map<String, String> mapParameters = FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
final String linkType = mapParameters.get(Constants.ATTRIBUTE_LINK_TYPE);
if (linkType.equals(messageManager.getProperty(Constants.MESSAGE_MENU_VIEWING))) {
linkPage = Constants.PAGE_VIEW;
}
...
responseRendered = true;
}
}
Ok well command link doesn't have an attribute update, so that should have thrown an error here:
<h:commandLink value="View" update="workplace">
I'm assuming you want an ajax solution it would be:
<h:commandLink value="View">
<f:param name="link" value="Viewing telephone book"/>
<f:ajax render="workplace" listener="#{main.determineAction}" />
</h:commandLink>
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>
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);
}
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>