0

I have a JSF form, in which we need to select list of products(using checkbox). The products are displayed using the c:forEach iteration. I am storing the detail of each selected product in a spearate bean called BuyProduct.java. I want each of this selected bean/product to be passed to the action attribute of form. Or is there any alternate way to implement this?

<!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:ui = "http://java.sun.com/jsf/facelets"
xmlns:c = "http://java.sun.com/jsp/jstl/core">
<h:head>
 <h:title>Shop</h:title>
 <h:outputStylesheet name="/css/products.css"></h:outputStylesheet>
</h:head>
<h:body>
 <h:form id="productForm">
  <c:forEach items="#{products}" var="prod">
  <div id="productDiv">
   <div id="nameFacet" name="nameFacet">
    <h:selectBooleanCheckbox value="#{buyProduct.code}">
     <f:selectItems itemLabel="#{prod.code}" itemValue="#{prod.name}"></f:selectItems>
    </h:selectBooleanCheckbox>
    <h:outputText id="prodName" value="#{prod.name}"/>
   </div>
   <div id="priceFacet" name="priceFacet">
    <h:outputText id="prodPrice" value="#{prod.price}">
     <f:convertNumber type="currency" currencySymbol="₹"></f:convertNumber>
    </h:outputText>
   </div>
   <div id="qtDiv">
    <h:inputText id="prodQuantity" value="#{buyProduct.quantity}"></h:inputText>
   </div>
    <ui:remove><h:graphicImage id="prodImage" value="#{prod.image}"/></ui:remove>
  </div>
  </c:forEach>
   <div id="submit">
    <h:commandButton value="Checkout" action="#{checkout.prodCheckout}"/>
   </div>
 </h:form>

</h:body>
</html>

package com.test.student.beans;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.test.student.utilities.SessionUtils;
import com.test.student.utilities.ShoppingDAOImpl;


@ManagedBean(name="accounts")
@SessionScoped
public class Accounts {
 
 private String username;
 private String password;
 private String role;
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getRole() {
  return role;
 }
 public void setRole(String role) {
  this.role = role;
 }

 public String userLogin() {
  Map<String,String> requestMap = buildRequestMap();
  //String username = requestMap.get("loginForm:username");
  //String password = requestMap.get("loginForm:password");
  if((username!=null&&username!="") && (password!=""&&password != null)) {
   createSession();
   return "Home.xhtml?faces-redirect=true";
  }
  else {
   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Incorrect Username and Passowrd","Please enter correct username and Password"));
   return FacesContext.getCurrentInstance().getViewRoot().getViewId();
  }
 }
 
 private void createSession() {
  HttpSession session = SessionUtils.getSession();
  session.setAttribute("username",username);
  
 }
 
 public String logout() {
  HttpSession session = SessionUtils.getSession();
  session.invalidate();
  return "Login";
 }

 private Map<String, String> buildRequestMap() {
  ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
  Map<String,String> requestMap = new HashMap<String,String>();
  Map<String,String> params = extContext.getRequestParameterMap();
  Set<Entry<String,String>> entries =  params.entrySet();
  Iterator<Entry<String,String>> iterator = entries.iterator();
  while(iterator.hasNext()) {
   Entry<String,String> next = iterator.next();
   requestMap.put(next.getKey(), next.getValue());
  }
  return requestMap;
 }
 
 public String showItems() {
  
  List<Products> products = getProducts();
  HttpSession session = SessionUtils.getSession();
  session.setAttribute("products", products);
  return "Shop";
  
 }
 private List<Products> getProducts() {
  ShoppingDAOImpl shopping = new ShoppingDAOImpl();
  return shopping.getProducts();
 }
 

}

package com.test.student.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="buyProduct")
@RequestScoped
public class BuyProduct {

 private String code;
 private String name;
 private String quantity;
 public String getCode() {
  return code;
 }
 public void setCode(String code) {
  this.code = code;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getQuantity() {
  return quantity;
 }
 public void setQuantity(String quantity) {
  this.quantity = quantity;
 }
 
 
}
sanjeeda
  • 131
  • 1
  • 10
  • Are you taking a course in jsf webdevelopment? You seem to be using very old things and many basic things wrong. If it is a tutorial somewhere, please point out which one and pick a newer one here: https://stackoverflow.com/tags/jsf/info – Kukeltje Oct 25 '18 at 09:06
  • https://stackoverflow.com/questions/28879656/multiple-instances-of-managed-bean – Kukeltje Oct 25 '18 at 09:11
  • I am getting started with JSF. It's not from any tutorial. Just doing things on my own. @Kukeltje – sanjeeda Oct 25 '18 at 09:12
  • Hmmm... If you are sites like coreservlets or similar, don't use them. Google is not the best searchengine in several ways. Clicks on links and the time between clicking on another search results ranks these sites. If You find one and due to you being new to the technology think the first link is probably the best, it gets higher in the ranking again. Unrightfullly. Just use things/sites mentioned here: https://jsf.zeef.com/bauke.scholtz. I'll try to write an 'answer' to your question and other things wrong in your code over the weekend so it might be a guide for others in the future. – Kukeltje Oct 25 '18 at 09:29
  • @Kukeltje sure, that would be helpful! Found a JSF application relevant to the one I am trying to build. http://learningprogramming.net/java/jsf/build-shopping-cart-with-hibernate-in-jsf-framework/ – sanjeeda Oct 25 '18 at 10:06
  • This is a not to bad example, better separation of responsibilities. But it still contains lots of 'old' things. HibernateUtil e.g. should not be used but JPA entitymanagers, jsf managed beans should be replaced by CDI ones, jsf-2.2 namespaces should be used. Check these: https://github.com/omnifaces/definitive-guide-to-jsf-in-javaee8/ (and the corresponding book, it is good!) – Kukeltje Oct 25 '18 at 11:49
  • If we were to use Dependency Injection instead of Managed Bean then the class "ProductManagedBean.java" cannot be called from the .xhtml file inside #{}. – sanjeeda Nov 02 '18 at 10:49
  • 'ManagedBean' also is dependency injection. But if you mean CDI managed beans are not working, then fix that.... It's an honest serious suggestion to **not start with `@ManagedBean` if you are starting from scratch**. You introduce legacy even before your application is operational – Kukeltje Nov 02 '18 at 10:53

0 Answers0