Thursday 18 February 2016

Exercise to how display user information on JSP page

This exercise will give the idea how to  display an user account on a frontend JSP using UserService. For the same , required to create our own Spring MVC controller which will use UserService for gathering an user information and will display it at a JSP view.

High level below activity would be required to achieve this exercise

 Detail step are given below:-
                   Put the Spring MVC library JAR into the web/webroot/WEB-INF/lib folder of the hybristraining extension
           Copy jar  /acceleratorstorefrontcommons/commonweb/webroot/WEB-INF/lib/spring-webmvc-3.2.5.RELEASE    and put inside folder web/webroot/WEB-INF/lib 




Display some message (User goes here) on JSP first before displaying user information
(1)   Add a servlet definition to the extension's web.xml file located in the hybristraining/web/webroot/WEB-INF directory
    <servlet>
         <servlet-name>springmvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

Run ant clean all , build should be successful


(2) Create front controller UserController.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 UserController implements Controller
{
@Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception
{
return new ModelAndView("user.jsp");
}
}


(3) Create springmvc-servlet.xml to path web/webroot/WEB-INF/   and add below highlighted line

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <bean
    name="/user.html"
    class="org.training.web.controllers.UserController">
  
    </bean>
                
</beans>

(4) Create a new file named user.jsp in the hybristraining/web/webroot

<html>
    <head>
        <title>User</title>
    </head>
    <body>
        User goes here
    </body>
</html>

(5) Call ant clean all
(6) Start the hybris Server

(8) Inject the UserService to get user information .For the add below highlighted code inside UserController.java


 (9)   To obtain the UserService, use a dependency injection, for the same add below highlighted code Snippet to  web/webroot/WEB-INF/springmvc-servlet.xml

(10)   Lookup the current user, add below highlighted code snippet to  UserController.java

               final UserModel user = userService.getCurrentUser();
final Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
return new ModelAndView("user.jsp", model);

(11)  To display the Current User, modify user.jsp with below code                         

                       <html>
                           <head>
                            <title>User</title>
                           </head>
                             <body>
                                 <h1>${user.uid}</h1>
                                <b>Name:</b> ${user.name}
                               <br/>
                              <b>Description:</b> ${user.description}
                        </body>
                    </html>   

(12)       Call ant clean all
(13)    Start the hybris Server
           (14)    Hit the URL http://localhost:9001/hybristraining/user.html 



Look Up and Display a Custom User
To display a specific user instead of the current one, use an URL parameter to pass the desired UID, such as: http://localhost:9001/training/user.html?uid=admin

For the same Modify UserController.java and add below highlighted code

        uid = request.getParameter("uid");
UserModel user = null;
if (uid == null)
{
user = userService.getCurrentUser();
}
else
{
user = userService.getUser(uid);
}


-> Call ant  clean all                                                     
    -> Start the hybris Server                                                 
                                     -> http://localhost:9001/hybristraining/user.html?uid=admin                                 
                     
  


                           

    
          
                          







5 comments:

  1. Thanks good information.keep blogging.For java Professionals Free Hybris Training

    ReplyDelete
  2. "mcafee is an antivirus software providers that secure your computer for virus , worms ,trojens and other mailcious program .it provides full range of
    security product like antivirus , firewall etc .you have to do mcafee antivirus download "

    ReplyDelete
  3. Amazon is an American multinational technology company which focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. It is one of the Big Five companies in the U.S. information technology industry, along with Google, Apple, Microsoft, and Facebook. amazon .com

    ReplyDelete
  4. Hybris Basic: Exercise To How Display User Information On Jsp Page >>>>> Download Now

    >>>>> Download Full

    Hybris Basic: Exercise To How Display User Information On Jsp Page >>>>> Download LINK

    >>>>> Download Now

    Hybris Basic: Exercise To How Display User Information On Jsp Page >>>>> Download Full

    >>>>> Download LINK bw

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

    >>>>> Download Full

    Hybris Basic: Exercise To How Display User Information On Jsp Page >>>>> Download LINK

    >>>>> Download Now

    Hybris Basic: Exercise To How Display User Information On Jsp Page >>>>> Download Full

    >>>>> Download LINK EZ

    ReplyDelete