Breaking News
recent

Spring MVC Portlet Application in Websphere Portal Using RAD

  1. In RAD go to newàothersàportlet project

2.Select Portlet project and click next
Enter project name (here Sample) and uncheck Create a Portlet checkbox.Click Finish.
So we can see in RAD one project folder and an EAR.


Copy all spring library fils into lib folder.
       SampleàWebContentàWEB-INFàlib


Next Add Servlet.In Spring portlet Application we are using ViewRendererServlet.
      
ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support.
For usage with Portlets, this Servlet is necessary to force the portlet container to convert the PortletRequest to a ServletRequest, which it has to do when including a resource via the PortletRequestDispatcher. This allows for reuse of the entire Servlet-based View support even in a Portlet environment.
The actual mapping of the bridge servlet is configurable in the DispatcherPortlet, via a "viewRendererUrl" property. The default is "/WEB-INF/servlet/view", which is just available for internal resource dispatching.
For adding servlet  double click on SampleàDeployment Descriptor:Sample
Select servlet and click add u can see the window like this.

check Use existing Servlet Class and click browse button

Select ViewRendererServlet and click OK.U can see screen like

Click next .


Select /ViewRendererServlet and click edit and change the value to /WEB-INF/servlet/view

Click Ok and click finish.

Next Add ContextLoaderListener for the application,Double click on WEB-INF/web.xml.Click variblesàlistener add.


Click Browse and select ContextLoaderListener class.Click Ok.


ContextLoaderListener need one context file to run.Default Context File is appplicationContext.xml.If you want to change the name of the file by giving
Contextparam in web.xml

context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/newfilename.xml</param-value>
</context-param>

Here we are not mentioning any context param so we need to create applicationContext.xml.



Next Add Portlet Class . Double Click on SampleàPortlet Deployment descriptor


Click on portlets and click Add.Select DispatchetPortlet click Ok.

DispatcherPortlet is a standard portlet (extending GenericPortlet), and as usual is declared in the portlet.xml of your web application.


And save(use Ctrl S).

  1. Next create Spring bean definition file default file name should be portletname-portlet.xml. We can change the name by init-param value in portal.xml. Here creating default one,and name should be Dispatcherportlet-portlet.xml.


In either the web application context or the Portlet context three things should be defined, a ViewResolver, a HandlerMapping and one or more Controllers. The ViewResolver tells the framework both how to translate simple names to full resource names and what kind of resource it is. In the example Portlet context below the ViewResolver is defined to look for JSP pages in the WEB-INF/jsp directory of the web application.

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
        </bean>




HandlerMapping tells the framework how to map incoming requests to the Portlet to different controllers. The Spring Portlet MVC Framework comes with a variety of interesting HandlerMapping implementations for mapping from Portlet Modes as well as arbitrary parameters. In the example, we are using ParameterHandlerMapping. Although Spring Portlet MVC allows you to use other frameworks by defining special types of Handler implementations,

<bean id="ingLoginController" class="nl.ing.viking.presentation.controller.IngLoginController"/>

<bean id="ingHomePageController" class="nl.ing.viking.presentation.controller.IngHomePageController"/>

<bean id="parameterHandlerMapping" class="org.springframework.web.portlet.handler.ParameterHandlerMapping">
<property name="parameterName" value="action"/>
<property name="defaultHandler" ref="ingLoginController"/>
<property name="parameterMap">
<map>
<entry key="homepage" value-ref="ingHomePageController"/>
</map>
</property>
</bean>               

Here property parameterName we set as “action”(it is default value we can give any  value).So when request is coming handler check the value of parameter “action” with entry key value,if it matches it will call particular controller otherwise it will invoke defaultHandler controller. So Dispatcherportlet-portlet.xml looks like


Next create controller class .

  1. Create package nl.ing.viking.presentation.controller
  2. Create Two Classes under above package IngLoginController and IngHomePageController (we mention these classes in Dispatcherportlet-portlet.xml)

10.Spring Providing many controller classes and an interface Controller
 So you can implements Controller interface or extends one of the classes given by spring jar.

Some of controller classes  are

·         AbstractCommandController - a command controller you can use to create your own command controller, capable of binding request parameters to a data object you specify. This class does not offer form functionality, it does however offer validation features and lets you specify in the controller itself what to do with the command object that has been filled with the parameters from the request.
·         AbstractFormController - an abstract controller offering form submission support. Using this controller you can model forms and populate them using a command object you retrieve in the controller. After a user has filled the form, AbstractFormController binds the fields, validates, and hands the object back to the controller to take appropriate action. Supported features are: invalid form submission (resubmission), validation, and normal form workflow. You implement methods to determine which views are used for form presentation and success. Use this controller if you need forms, but don't want to specify what views you're going to show the user in the application context.
·         SimpleFormController - a concrete AbstractFormController that provides even more support when creating a form with a corresponding command object. The SimpleFormController lets you specify a command object, a viewname for the form, a viewname for the page you want to show the user when form submission has succeeded, and more.
·         AbstractWizardFormController – a concrete AbstractFormController that provides a wizard-style interface for editing the contents of a command object across multiple display pages. Supports multiple user actions: finish, cancel, or page change, all of which are easily specified in request parameters from the view.

In the example I just implements interface Controller.

Here we need to implement two abstract methods handleActionRequest(for handling action request) and handleRenderRequest(for handling render request).

We are given default handler as ingLoginPageController ,So this controller call first, Coe below show sample render request

public ModelAndView handleRenderRequest(RenderRequest arg0, RenderResponse arg1) throws Exception {
            // TODO Auto-generated method stub
          return new ModelAndView("login");
                             }


This method return a ModelAndView Object.In this example when we are creating ModelAndView passing a string login, the viewResolver bean in the DispatchetPrtlet-portlet.xml handle this one .(in our example it will invoke the jsp reside in /WEB-IBNF/jsp/login.jsp).If we need to pass any value to login.jsp we can pass value like

    return new ModelAndView("login",”name”,”value”);

                      OR

         ModelAndView mv=new ModeAndView(“login”);
         mv.addObject(“name1”,”value1”);
         mv.addObject(“name2”,”value2”);
         return mv;

                      OR

       Map map=new HashMap();
       Map.put(“name”,”value”);
       Map.put(“name1”,”value1”);
       ModelAndView mv=new ModeAndView(“login”);
       mv.addObject(map);
       return mv;



11. Create login.jsp in WebContent/Web-INF/jsp






Login .jsp
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<portlet:defineObjects />
<form method="post" action="<portlet:actionURL><portlet:param name="action" value="login"></portlet:param></portlet:actionURL>">
<table width="500" border="0" cellpadding="0" cellspacing="0">
      <tbody>
            <tr>
                  <td width="216">User Name</td>
                  <td width="284"><br>
                  <input type="text" name="username" size="20"><br>
                  </td>
            </tr>
            <tr>
                  <td width="216">Password<br>
                  </td>
                  <td width="284"><input type="text" name="password" size="20"><br>
                  </td>
            </tr>
            <tr>
                  <td width="216"><input type="submit" name="submit" value="login"><br>
                  </td>
                  <td width="284"></td>
            </tr>
      </tbody>
</table>
</form>




In login.jsp we can see include of taglib portlet.

<%@taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<portlet:defineObjects />

For enable this we need to include tablig in web.xml.

Go to web.xml àvariblesàtag lib à add


Location à/WEB-INF/tld/std-portlet.tld


                                                    
When submit the login form it will call the renderActionMethod of IngloginController.

<form method="post" action="<portlet:actionURL><portlet:param name="action" value="login"></portlet:param></portlet:actionURL>">

Here handler check whether any handler is there for the parameterName “action”,
In this example no handler is there with entry key login so it will call defaultHandler.

IngLoginLoginController ActionRequesthandler method


public void handleActionRequest(ActionRequest request, ActionResponse response) throws Exception {
            // TODO Auto-generated method stub
            // Do whatever action we need.
            System.out.println("inside login action");
            response.setRenderParameter( "action" , "homepage" ) ; // set render parameter           
                                }  



Here do the whatever actions we want,if we need to render the home page then we want to set  RenderParameter.

response.setRenderParameter( "action" , "homepage" )

Where “action” is parametername we given in the  DispatcherPortlet-portlet.xml.”homepage” is the value of  “action”.

In DispatcherPortlet-portlet.xml,

<map>
      <entry key="homepage" value-ref="ingHomePageController"/>
</map>


So it will render the ingHomePageController”.

public ModelAndView handleRenderRequest(RenderRequest arg0, RenderResponse arg1) throws Exception {
            // TODO Auto-generated method stub
            return new ModelAndView("home");
                                }


It will invoke “/WEB-INF/jsp/home.jsp”.
Sample home.jsp



******************************* END*********************************************
Unknown

Unknown

No comments:

Post a Comment

Powered by Blogger.