I'm usign Mojarra 2.1.23 and Primefaces 3.5. I'm trying to make this code work, but for some reason I can't get the listener to be invoked.
<h:form id="menu" >
<p:growl id="messages" autoUpdate="true" showDetail="true" />
<p:panelMenu>
<p:submenu label="Ajax Menuitems">
<p:menuitem value="#{MenuController.test}"
actionListener="#{MenuController.save}" ajax="true"
update="messages" />
<p:menuitem value="Update" actionListener="#{MenuController.save}"
update="messages" />
</p:submenu>
</p:panelMenu>
</h:form>
MenuController is a session scoped bean managed by Spring 3 and even its properties are properly displayed on the same xhtml (button shows the text "Save").
The MenuController class is as follows:
public class MenuController() implements Serializable{
public static Logger log;
public String test="Save";
public MenuController() {
log = LoggerFactory.getLogger(this.getClass());
log.debug("Hello MenuController.");
}
public void save(ActionEvent event) {
addMessage("Data saved");
log.debug(" saving");
}
public void addMessage(String summary) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
summary, null);
FacesContext.getCurrentInstance().addMessage(null, message);
}
//getters and setters...
}
I'm also using facelets, the following is Layout.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><ui:insert name="title" /></title>
</h:head>
<h:body>
<div id="layout">
<table>
<tr>
<td>
<div id="menu">
<ui:include src="menu.xhtml"/>
</div>
</td>
<td>
<div id="content">
<ui:insert name="content">
</ui:insert>
</div>
</td>
</tr>
<tr>
<td>
<div id="footer">
<p>© Footer</p>
</div>
</td>
</tr>
</table>
</div>
When I click the menu entry, the method is not being called.
The problem was solved by changing the id on the form from "menu" to "menuForm", to not be confused by the one on the layout with id "menu"
<h:form id="menuForm" >
<p:growl id="messages" autoUpdate="true" showDetail="true" />
<p:panelMenu>
<p:submenu label="Ajax Menuitems">
<p:menuitem value="#{MenuController.test}"
actionListener="#{MenuController.save}" ajax="true"
update="messages" />
<p:menuitem value="Update" actionListener="#{MenuController.save}"
update="messages" />
</p:submenu>
</p:panelMenu>
</h:form>
Related
I'm sure that the question is asked before, but I can't fin my explicit problem, I'm afraid it is only a little typo and I can't see it.
I have a simple page in primefaces 10 with a toolbar, with an add-button, a table and a dialoge for adding the data. The dialog has a rendered tag that the requiered field are only rendered if there is a new variable. Everything I used on several other pages of my project.
The new Variable is initialized perfectly but the inside of the dialog isn't rendered, if I remove the rendered tag the field is rendered, but I have put some test-content into my new Variable and this isn't shown either, so I think that my update doesn't get called but I don't see why.
So here is my code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="layout.xhtml">
<ui:define name="header">
<div id="header">
ChangeLog
</div>
</ui:define>
<ui:define name="main">
<h:form id="changelogfrm">
<p:growl id="messages" showDetail="true" life="3000" />
<div class="card">
<p:toolbar>
<p:toolbarGroup>
<p:commandButton value="Neu" icon="pi pi-plus" actionListener="#{changeloghandler.newChangelog}"
update=":changelogfrm:manage-change-content" oncomplete="PF('manageChangeDialog').show()"
styleClass="ui-button-success" style="margin-right: .5rem" />
</p:toolbarGroup>
</p:toolbar>
<p:dataTable var="change" value="#{changeloghandler.changeLogs}"
showGridlines="true" size="small" id="dtChange">
<p:column headerText="Version">
<h:outputText value="#{change.version}" />
</p:column>
<p:column headerText="Typ">
<h:outputText value="#{change.changeType}"/>
</p:column>
<p:column headerText="Beschreibung">
<h:outputText value="#{change.description}"/>
</p:column>
<p:column headerText="Datum">
<h:outputText value="#{change.date}">
<f:convertDateTime type="date" pattern="dd.MM.yyyy HH:mm"/>
</h:outputText>
</p:column>
</p:dataTable>
<p:dialog header="Change" showEffect="fade" modal="true"
widgetVar="manageChangeDialog" responsive="true">
<p:outputPanel id="manage-change-content" class="ui-fluid">
<p:outputPanel rendered="#{not empty changeloghandler.selected}">
<div class="p-field">
<p:outputLabel for="chBeschreibung">Beschreibung</p:outputLabel>
<p:inputTextarea id="chBeschreibung" value="#{changeloghandler.selected.description}" required="true"/>
</div>
</p:outputPanel>
</p:outputPanel>
<f:facet name="footer">
<p:commandButton value="Save" icon="pi pi-check" actionListener="#{changeloghandler.save}"
update="manage-change-content" process="manage-change-content #this"/>
<p:commandButton value="Cancel" icon="pi pi-times" onclick="PF('manageChangeDialog').hide()"
class="ui-button-secondary"/>
</f:facet>
</p:dialog>
</div>
</h:form>
</ui:define>
</ui:composition>
The save-function in the dialog isn't called either.
I've also checked the html on the site, there is no double-form and the ids are set correctly.
Hope you can help me.
!!!EDIT!!!
Hi, here you have the Bean-Class:
package main.java.handler;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.primefaces.PrimeFaces;
import main.java.persistence.entities.ChangeLog;
import main.java.persistence.entities.enums.ChangeType;
import main.java.persistence.entities.enums.Version;
import main.java.persistence.interfaces.ChangeLogDbInterface;
#Named("changeloghandler")
#SessionScoped
public class ChangeLogHandler implements Serializable{
private static final long serialVersionUID = 1L;
private List<ChangeLog> changeLogs;
private ChangeLog selected;
private List<Version> versions;
private ChangeType[] changeTypes;
#PostConstruct
private void init() {
changeLogs = ChangeLogDbInterface.loadChangeLogItems();
versions = new ArrayList<>();
for(Version v : Version.values()) {
if(v.isActive()) versions.add(v);
}
changeTypes = ChangeType.values();
}
public void newChangelog() {
ChangeLog newLog = new ChangeLog();
newLog.setDate(new Timestamp(System.currentTimeMillis()));
this.selected = newLog;
}
public void save() {
if (this.selected.getId() == 0) {
ChangeLogDbInterface.createChangeLogItem(selected);
this.changeLogs.add(this.selected);
}
else {
ChangeLogDbInterface.updateChangeLogItem(selected);
}
PrimeFaces.current().executeScript("PF('manageChangeDialog').hide()");
PrimeFaces.current().ajax().update("changelogfrm:dtChange");
}
public List<ChangeLog> getChangeLogs() { return changeLogs; }
public ChangeLog getSelected() { return selected; }
public List<Version> getVersions() { return versions; }
public ChangeType[] getChangeTypes() { return changeTypes; }
public void setChangeLogs(List<ChangeLog> changeLogs) { this.changeLogs = changeLogs; }
public void setSelected(ChangeLog selected) { this.selected = selected; }
public void setVersions(List<Version> versions) { this.versions = versions; }
public void setChangeTypes(ChangeType[] changeTypes) { this.changeTypes = changeTypes; }
}
!!! EDIT 2 !!!
Here is the template, but I think that doesn't help much either:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Asset Tool Connector</title>
<link type="text/css"
href="${facesContext.externalContext.requestContextPath}/resources/css/style.css"
rel="stylesheet" />
<link type="image/x-icon"
href="${facesContext.externalContext.requestContextPath}/resources/images/netzwerk.png"
rel="shortcut icon" />
</h:head>
<h:body>
<ui:insert name="header"/>
<f:event listener="#{sessionHandler.checkLoggedIn}" type="preRenderView" />
<div id="menu">
<ui:include src="menu.xhtml" />
</div>
<div id="main">
<ui:insert name="main"/>
</div>
<div id="footer">
<div>
<h:form id="footerForm">
<p:commandButton value="ChangeLog" action="/changelog?faces-redirect=true"/>
</h:form>
</div>
© ATC - by 5332
</div>
</h:body>
</html>
Except the button in the footer links to the page with my issue, if that would be an issue.
I have to call a remote command through javascript. The remote command is working fine but it invokes on page load instead of the button's onclick event.
Following is my xhtml file code as well as the bean code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Crowd Flow</title>
</h:head>
<f:event listener="#{task.loadTaskByTaskId(param['task_id'])}"
type="preRenderView" />
<h:body bgcolor="white">
<h:outputScript>
function getCurrentLocation() {
testingCommand();
}
</h:outputScript>
<h:panelGroup id="header" layout="block">
<h3>Welcome to the task.</h3>
</h:panelGroup>
<h:panelGroup id="taskLayout" layout="block">
<h:outputText value="Category is #{task.taskCategory}"></h:outputText>
<h:outputText value="Task id is is #{task.id}"></h:outputText>
<c:if test="#{param['task_id']=='9'}">
<h:form style="width:400px;" id="newTagTaskForm">
<p:remoteCommand name="testingCommand"
actionListener="#{task.testingCommand()}" autoRun="false" />
Task Name : <h:outputText id="taskName"
value="#{task.newPlaceTagTask.name}"></h:outputText>
<br />
Task Description : <h:outputText id="taskDescription"
value="#{task.newPlaceTagTask.description}"></h:outputText>
<br />
Address : <h:outputText id="taskAddress"
value="#{task.newPlaceTagTask.location.address}"></h:outputText>
<br />
Image : <h:graphicImage width="50" height="50"
url="#{task.newPlaceTagTask.location.image}" />
<br />
<h:inputHidden value="#{task.userId}" id="taskPlaceTagUserId"
name="taskPlaceTagUserId" />
<h:inputHidden value="#{task.newPlaceTagTask.location.id}"
id="taskPlaceTagLocationId" name="taskPlaceTagLocationId" />
<h:commandButton id="testing" value="submit" type="submit" onclick="getCurrentLocation();return false;" ></h:commandButton>
</h:form>
</c:if>
</h:panelGroup>
</h:body>
</html>
Here is the bean function, bean is view scoped.
package com.project.crowdsource.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean(name="task")
#ViewScoped
public class TaskBean implements Serializable {
public TaskBean() {
//initiating user's data access object.
taskDao = new TaskDAO();
}
public String testingCommand() {
System.out.println("in action testing command");
return "";
}
}
Any help would be highly appreciated.
Thanks,
Hello I have an inputText on blur event that will send a signal to the backing bean, this bean contains a Boolean variable that determines if another input text will be enabled or not ... but I can not make this work, this is my code:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/Template.xhtml">
<ui:define name="content">
<h:form id="someForm">
<p:growl id="msg" showDetail="true" life="3000"/>
<p:panelGrid border="0" id="panel">
<p:row>
<p:column width="350">
Title
</p:column>
<p:column colspan="2">
<p:inputText id="someId" value="#{someBean.somePropertie}">
<p:ajax event="blur" update="anotherInput" listener="#{someBean.onEvent}" />
</p:inputText>
</p:column>
</p:row>
<p:row>
<p:column width="350">title 2</p:column>
<p:column colspan="2">
<p:inputText id="anotherInput" converter="toUpperCaseConverter" value="#{someBean.somePropertie2}"
disabled="#{someBean.bDisabled}"
/>
</p:column>
</p:row>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
Backing bean:
#ManagedBean
#SuppressWarnings("serial")
public class SomeBean implements Serializable{
private boolean bDisabled;
private String somePropertie;
private String somePropertie2;
public void onEvent(){
System.out.println("DO SOMETHING");
this.bDisabled = true;
}
... getters and setters of properties and boolean ....
}
this is the main tamplate:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
<title>..:: XXXXX ::..</title>
<meta http-equiv="Pragma" content="no-cache" />
<link rel="stylesheet" type="text/css" href="#{request.contextPath}/css/default.css"/>
<style type="text/css">
.ui-growl{
position:absolute;
top:20%;
left:50%;
z-index:9999;
}
.ui-widget,.ui-widget .ui-widget {
font-family: Trebuchet MS;
}
</style>
</h:head>
<h:body style="background-color: #E1E1E1;">
<div id="page">
<div id="divHeader" style="height: 70px;">
<ui:insert name="header" >
<ui:include src="header.xhtml" />
</ui:insert>
</div>
<div id="divMenu" style="height: 50px;">
<ui:insert name="menu">
<ui:include src="menu.xhtml" />
</ui:insert>
</div>
<div id="divContent">
<ui:insert id="content" name="content" >
<ui:include src="content.xhtml" />
</ui:insert>
</div>
</div>
</h:body>
</html>
the converter:
#FacesConverter("toUpperCaseConverter")
public class ToUpperCaseConverter implements Converter {
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (String) value;
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return (value != null) ? value.toUpperCase() : null;
}
}
any idea ???
Please help :(
Your question is not complete, since it does not provide all the elements to reproduce the bug. You haven't provided the page that calls your element, neither the converter, etc.
JSF's debugger best friend is firebug. Always check for javascript errors and enable the network tab to see each request response body, which may contain "silent" error messages.
I'll take my risk trying to guess your problem :-)
Here's your code, slightly changed to run in my primefaces 4.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>You NEED a header</title>
</h:head>
<h:body>
<h:form>
<ui:include src="index2.xhtml" />
</h:form>
</h:body>
</html>
and
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<p:growl
id="msg"
showDetail="true"
life="3000" />
<p:panelGrid
border="0"
id="panel">
<p:row>
<p:column width="350">
Title
</p:column>
<p:column colspan="2">
<p:inputText
id="someId"
value="#{someBean.somePropertie}">
<p:ajax
event="blur"
update="anotherInput"
listener="#{someBean.onEvent}" />
</p:inputText>
</p:column>
</p:row>
<p:row>
<p:column width="350">title 2</p:column>
<p:column colspan="2">
<p:inputText
id="anotherInput"
value="#{someBean.somePropertie2}"
disabled="#{someBean.bDisabled}" />
</p:column>
</p:row>
</p:panelGrid>
</ui:composition>
The managed bean is pretty much the same
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
#ManagedBean
#SuppressWarnings("serial")
public class SomeBean implements Serializable {
private boolean bDisabled;
private String somePropertie;
private String somePropertie2;
public void onEvent() {
System.out.println("DO SOMETHING");
this.bDisabled = true;
}
public boolean isbDisabled() {
return this.bDisabled;
}
public void setbDisabled(boolean bDisabled) {
this.bDisabled = bDisabled;
}
public String getSomePropertie() {
return this.somePropertie;
}
public void setSomePropertie(String somePropertie) {
this.somePropertie = somePropertie;
}
public String getSomePropertie2() {
return this.somePropertie2;
}
public void setSomePropertie2(String somePropertie2) {
this.somePropertie2 = somePropertie2;
}
}
I've noticed that if you don't omit the HEAD section from the caller page, it seems to render and work.
But if you forget the HEAD section (you must not, see Primefaces FAQ item #2), you'll render the page (quite) but you'll get some javascript errors and your page won't work. The HEAD section I am talking about is this
<h:head>
<title>You NEED a header</title>
</h:head>
This is how it looks without the HEAD section.
With the HEAD section, it seems to work. Also notice the subtle primefaces look and feel.
I've got template which has: header, footer, menu (left side), cart (right side) and content at center. I select category from menu and send it via parameter to view page which overrides content from template. Then i use commandButton to add any product to cart. It works but next I select different category from menu view page updates with other products but my cart (included in template) does not. It remains as it was before when I was on the view page with this category. How can I overcome this problem? Thanks for any solution.
template:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
<f:viewParam name="category"/>
</f:metadata>
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h:outputStylesheet name="./css/default.css"/>
<h:outputStylesheet name="./css/cssLayout.css"/>
<title>Drummers Store</title>
</h:head>
<h:body>
<div id="container">
<div id="top">
<div id="top-content">
<div style="text-align: left; margin-left: 50px; margin-top: 20px;">
<p:graphicImage value="./resources/images/logo.png" height="80px" width="80px" />
</div>
<h:form>
<p:menuButton style="font-size: 10px;" value="account_name">
<p:menuitem value="Account Settings" />
<p:separator />
<p:menuitem value="Logut" />
</p:menuButton>
</h:form>
</div>
</div>
<div id="left">
<div id="left-content">
<h:form id="form" style="width: 290px;" >
<p:panelMenu id="categories" style="text-align: center;">
<c:forEach items="#{dSServerBean.categories}" var="c">
<p:submenu label="#{c.category_name}">
<c:forEach items="#{c.sub_categories}" var="s">
<c:if test="#{not s.sub_categories.isEmpty()}">
<p:submenu label="#{s.subCategory_name}">
<c:forEach items="#{s.sub_categories}" var="si">
<p:menuitem value="#{si.subCategory_name}" outcome="productView">
<f:param name="category"
value="#{si.subCategory_name.toLowerCase()}" />
</p:menuitem>
</c:forEach>
</p:submenu>
</c:if>
<c:if test="#{s.sub_categories.isEmpty()}">
<p:menuitem value="#{s.subCategory_name}" outcome="productView">
<f:param name="category"
value="#{s.subCategory_name.toLowerCase()}" />
</p:menuitem>
</c:if>
</c:forEach>
</p:submenu>
</c:forEach>
</p:panelMenu>
</h:form>
</div>
</div>
<div id="right">
<div id="right-content">
<h:form style="width: 85%; margin: 0 auto; margin-top: 50px;">
<p:panel>
<f:facet name="header">Cart</f:facet>
<f:facet name="footer">To pay: 0 $</f:facet>
<p:scrollPanel id="scroll2"
style="height: 200px;
max-height: 200px;
overflow-wrap: break-word;">
<p:dataGrid value="#{dSBean.cart}" var="p"
columns="1" emptyMessage="No products added..."
style="border: 0;">
<h:panelGrid>
<h:outputText style="font-size: 10px;" value="#{p.name}" />
</h:panelGrid>
</p:dataGrid>
</p:scrollPanel>
</p:panel>
</h:form>
</div>
</div>
<div id="center">
<div id="center-content">
<ui:insert name="content" />
</div>
</div>
<div id="bottom">
<div id="bottom-content">
<p style="padding: 1px;">Copyright © <b>Drummers Store</b> 2013</p>
</div>
</div>
</div>
</h:body>
</html>
view:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<f:metadata>
<f:viewParam name="category"/>
</f:metadata>
<body>
<ui:composition template="./templates/mainLayout.xhtml">
<ui:define name="right">
right
</ui:define>
<ui:define name="content">
<h:form>
<p:scrollPanel id="scroll1" style="height: 700px; max-height: 700px; width: 100%;">
<p:dataGrid value="#{dSServerBean.getProducts(param.category)}"
var="product" columns="1">
<p:panel header="#{product.name}">
<h:panelGrid style="width:100%;">
<h:outputText value="#{product.description}" />
<h:commandButton value="DODAJ" action="#{dSBean.addToCart(product)}" >
<f:param name="category" value="#{param.category}" />
</h:commandButton>
</h:panelGrid>
</p:panel>
</p:dataGrid>
</p:scrollPanel>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
DSServerBean:
#ManagedBean
#ApplicationScoped
public class DSServerBean {
public Set<Category> categories = new CategoriesTemplate().categoriesTemplate;
public static Set<Product> products = new ProductsTemplate().productsTemplate;
public Map<String, Set<Product>> category_products;
public DSServerBean() {
category_products = new TreeMap<>();
for(Category category : categories){
category_products.put(category.getCategory_name().toLowerCase(), new TreeSet<Product>());
for(SubCategory subCategory : category.getSub_categories()){
category_products.put(subCategory.getSubCategory_name().toLowerCase(), new TreeSet<Product>());
for(SubCategory subSubCategory : subCategory.getSub_categories()){
category_products.put(subSubCategory.getSubCategory_name().toLowerCase(), new TreeSet<Product>());
}
}
}
for(Product product : products){
for(String category : product.getCategories().split(",")){
category_products.get(category).add(product);
}
}
}
public Set<Category> getCategories() {
return categories;
}
public Map<String, Set<Product>> getCategory_products() {
return category_products;
}
public List<Product> getProducts(String category){
List<Product> view = new ArrayList<>();
for(Product p : category_products.get(category))
view.add(p);
return view;
}
}
DSBean:
#ManagedBean
#SessionScoped
public class DSBean {
private User logged_user;
private List<Product> cart;
public DSBean(){
logged_user = new User("Marian");
cart = new ArrayList<>();
}
public User getLogged_user() {
return logged_user;
}
public List<Product> getCart() {
return cart;
}
//METHODS
public String addToCart(String product){
System.out.println(product);
for(Product p : DSServerBean.products)
if(p.toString().equals(product))
cart.add(p);
return "productView";
}
}
Ok. I'll found alternative solution, just as I mentioned before. I change view via action attribute (instead outcome attribute) of menuitem which has linked parameter which I obtain in managed bean (not in view like before). Thanks for interest.
I'm using JSF2 with Primefaces3.4.2 I have created a layout in layoutComplex.xhtml as below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
<f:facet name="first">
<meta http-equiv="X-UA-Compatible" content="EmulateIE8" />
<meta content='text/html; charset=UTF-8' http-equiv="Content-Type" />
<title>PrimeFaces - ShowCase</title>
</f:facet>
<h:outputScript library="js" name="jscolor.js" target="head" />
<script type="text/javascript">
function handleValidateRequest(xhr, status, args) {
//alert("");
//jscolor.addEvent(window, 'load', jscolor.init);
}
</script>
</h:head>
<h:body>
<p:layout fullPage="true">
<p:layoutUnit id="left" position="west" size="300" resizable="true"
closable="true" collapsible="true" header="Options" minSize="200">
<h:form>
<p:slideMenu style="width:235px;margin-left:-3px;margin-top:-6px;"
id="tree">
<p:submenu label="Product" icon="ui-icon-play">
<p:menuitem value="test color picker"
update=":centerContentPanel " action="#{navigationBean.doNav}"
oncomplete="handleValidateRequest(xhr, status, args)"
icon="ui-icon-arrow-4-diag">
<f:param name="urlParam" value="colorPicker" />
</p:menuitem>
</p:submenu>
</p:slideMenu>
</h:form>
</p:layoutUnit>
<p:layoutUnit id="center" position="center">
<p:panel header="Colors">
<h:panelGrid columns="2" cellpadding="10">
<h:inputText class="color">
<p:ajax event="change" update="osssutcolor" />
</h:inputText>
<h:outputText style="display: none" id="osssutcolor" />
</h:panelGrid>
</p:panel>
<h:form id="centerContentPanel">
<ui:include src="#{navigationBean.pageName}.xhtml" />
</h:form>
</p:layoutUnit>
</p:layout>
</h:body>
</f:view>
</html>
Yes,I can dynamically change the source of centerContentPanel without refreshing the whole page and just the centerContentPanel i.e for on click of menuitem present in the layoutComplex.xhtml,and then the colorPicker page's content will be displayed in the centerContenPanel. But issue is: I added a colorpicker.js in the layoutComplex.xhtml head and hope it can work when update centerContent, but actually, it's not working ..
but after refresh all page by press F5 ,it works fine as I expected. Why? How can i fix this?
Following is colorPicker.xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:outputScript library="js" name="jscolor.js" target="head" />
<p:panel header="Colors">
<h:panelGrid columns="2" cellpadding="10">
<h:inputText class="color">
<p:ajax event="change" update="osssutcolor" />
</h:inputText>
<h:outputText style="display: none" id="osssutcolor" />
</h:panelGrid>
</p:panel>
</ui:composition>
and NavigationBean.java
package com.singtel.eshop.control;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
#SessionScoped
#ManagedBean
public class NavigationBean {
private String pageName = "blank";
public NavigationBean() {
}
public void doNav() {
String urlStr = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("urlParam");
this.pageName = urlStr;
}
public String getPageName() {
return pageName;
}
public void setPageName(String pageName) {
this.pageName = pageName;
}
}
You should call the jscolor.init after ajax call too. Seems like it is being called right after page load and needs to be called after your ajax call or inside your component. You can achieve this by calling jscolor.init in your colorPicker.xhtml file like this.
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:outputScript library="js" name="jscolor.js" target="head" />
<script>
jscolor.init;
</script>
<p:panel header="Colors">
<h:panelGrid columns="2" cellpadding="10">
<h:inputText class="color">
<p:ajax event="change" update="osssutcolor" />
</h:inputText>
<h:outputText style="display: none" id="osssutcolor" />
</h:panelGrid>
</p:panel>
</ui:composition>