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
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 7:-Invoke URL http://localhost:9001/hybristraining/product.html?code=100124
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