Thursday 18 February 2016

Exercise how to display a Product on JSP page

This exercise describe detail about how to display a product on a frontend JSP using ProductService.
The main difference to the How to Display a User is that the product type is managed by catalogs which implies additional logic for setting the correct catalog versions at the session context.
     High level activity required to achieve this exercise are 
    (a) Create a Frontend Controller and a View
    (b) Inject the ProductService
    (c) Look Up the Product
    (d) Display the Product
    (e) First Attempt to Run
    (f) Set Up the Catalog Version
    (g) Second Attempt to Run
   Detail step are given below:-
 Step 1:-
    (a) Create a Frontend Controller and a View  (ProductController.java)

ProductController.java             
      package org.training.web.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class ProductController implements Controller
{
@Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception
{
return new ModelAndView("product.jsp");
}
}



(b)    Create a new file named product.jsp in the hybristraining/web/webroot directory
            <html>
                   <head>
                      <title>Product</title>
                   </head>
                <body>
                     Product goes here
              </body>
      </html>

           (c) Create entry of controller in springmvc-servlet.xml   
                                    <bean
                                               name="/product.html"
                                                    class="org.training.web.controllers.ProductController"/>


(d) Call ant clean all and start the hybris server                                  

(e)     Invoke the URL http://localhost:9001/hybristraining/product.html.        


     Step 2:- Inject the ProductService                                                              
                                                To display a product with a certain code, passed through an URL
                                                such as http://localhost:9001/hybristraining/product.html?code=somproductcode

       (a)    Add an instance variable and a setter method to the ProductController.java
         
private ProductService productService;

                               public void setProductService(final ProductService productService)
                               {
                                this.productService = productService;
                               }


     (b) To obtain the ProductService, use a dependency injection, modify spring bean

                  <bean name="/product.html" class="org.training.web.controllers.ProductController">
                                       <property name="productService" ref="productService"/>
<                          /bean>
         
             
Step3:- Lookup the product                                                                                          
                   (a) To extract the product code from the URL, add the following piece of code to the handleRequest(...) method of our ProductController.java                                                         

private ProductModel product = null;
                     final String code = request.getParameter("code");

if (code != null)
{
                                                        product = productService.getProductForCode(code);
}
final Map<String, Object> model = new HashMap<String, Object>();
model.put("product", product);



                           Step 4:  Display the Product , Add below code to JSP product.jsp    
<html>
    <head>
                                     <title>Product</title>
    </head>
    <body>
                                       <h1>${product.name}</h1>
                                      ${product.description}
    </body>
           </html>   

Step 5:Call  ant clean all  and start the hybris server 
 Step 6:- Open HMC and collect the test data                   








          Step 8:- Set Up the Catalog Version
                        (a)    Add an instance variable and a setter method to the ProductController of CatalogService
                                       private CatalogService catalogService;
                               public void setCatalogService(final CatalogService catalogService)
                              {
                                     this.catalogService = catalogService;
                              }

                              catalogService.setSessionCatalogVersion("apparelProductCatalog", "Online");


                                  (b)    Add the dependency of the CatalogService to the springmvc-servlet.xml file  

                     <bean name="/product.html" class="org.training.web.controllers.ProductController">
    <property name="productService" ref="productService"/>
    <property name="catalogService" ref="catalogService"/>
</bean>      

(c) Stop server, call ant clean all and start the hybris server                 
                               (d)   Invoke URL  http://localhost:9001/hybristraining/product.html?code=100124            

              















5 comments:

  1. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. It is really very helpful for us and I have gathered some important information from this blog. hybris training in hyderabad

    ReplyDelete
  2. Thank you for the post, i have set up my store based on the electronics store however i want to add an extra page between the first checkout step and second checkout step if i create my page using the steps above how do i add this page between the step one and two. If i click check out i would like it to open my page and from my page if you click next it goes to the previous step 2.

    ReplyDelete
  3. If the above is possible how do i display the cart on the custom jsp page.

    ReplyDelete
  4. The main motive of the Hadoop consulting services is to spread the knowledge so that they can give more big data engineers to the world.

    ReplyDelete
  5. Hybris Basic: Exercise How To Display A Product On Jsp Page >>>>> Download Now

    >>>>> Download Full

    Hybris Basic: Exercise How To Display A Product On Jsp Page >>>>> Download LINK

    >>>>> Download Now

    Hybris Basic: Exercise How To Display A Product On Jsp Page >>>>> Download Full

    >>>>> Download LINK ER

    ReplyDelete