Spring Annotation Portlet

The following article i will show how to convert from declaration xml base to annotation based. The example will base on the manning portlet in action book.  I pick up the chapter 7 as learning point, you may download the original source code to compare.


The example downloaded from manning website basically is a simple book portlet, the functionality of this portlet is show a list of books, and new book function, remove and edit book functions.

There are totally 7 controllers you can found from the controller packages.

  1. AddBookController
  2. BooksController
  3. EditBookController
  4. EditModeController
  5. HelpModeController
  6. RemoveBookController
  7. ViewModeController

Let’s get started from BooksController. In order to convert/develop a spring annotation portlet, few thing you will need to do first before we can continue.

  1. Add/Edit existing portlet declaration in portlet.xml
  2. Add/Edit existing portlet declaration in liferay-portlet.xml
  3. Create an existing a book-portlet.xml

Next, we quickly look through the following xml files.

  1. portletModeParameterHandlerMappingExample-portlet.xml and
  2. portletModeHandlerMappingExample-portlet.xml

As we can see, portletModeHandlerMappingExample-portlet.xml is mainly declare the portlet mode. ‘VIEW’, ‘EDIT’,'HELP’. We can delete this xml file now because it is able to achieved by using annotation within the controller class later. Next, we will start writing new stuff in our book-portlet.xml base on the  portletModeParameterHandlerMappingExample-portlet.xml 

book-portlet.xml,  all we need to have is show on below.

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans xmlns="http://www.springframework.org/schema/beans
  4. xmlns="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"</span>
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.     "http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9.      "http://www.springframework.org/schema/context
  10.     "http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  11.  
  12. <context:component-scan base-package="com.loongest.book.controller>
  13.       <bean id="viewResolver"
  14. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15.            <property name="viewClass"
  16.                     value="org.springframework.web.servlet.view.JstlView" />
  17.                 <property name="prefix" value="/WEB-INF/view/book/" />
  18.                  <property name="suffix" value=".jsp" />
  19.       </bean>
  20. </beans>
  21.  

 Modify BooksController

  1.  
  2. package com.loongest.book.controller;
  3. import  ……..;
  4.  
  5. @RequestMapping("VIEW")
  6. public class BooksController {
  7.     @Autowired private BookService bookService;
  8.    
  9.     @RequestMapping
  10.     publi void postAction() {
  11.         // – if void method mean ‘POST’, default view mode nothing do post, 
  12.         // – so leave it blank implementation  
  13.     }
  14.  
  15.     @RequestMapping
  16.     // – Return type is String, means we will return books jsp for render.
  17.     public String showForm(ModelMap map) throws Exception {
  18.          List<Books> books = bookService.getBooks();
  19.          map.put("books",books);
  20.          return "books";
  21.     }
  22.    
  23.     // – Refer to the original xml declaration,
  24.     // – addBookForm are map to addBookController to render addBook.jsp
  25.  
  26.     @RequestMapping(params = "myaction=addBookForm")
  27.     public String showAddBookForm() {
  28.          return "addBook";
  29.     }
  30.  
  31.     // – form submit action, when user click add book button, portlet will get form parameter and
  32.     // – validate. if contains errors, will show form again else back to default view. myaction=""
  33.    @RequestMapping(params = "myaction=addBook")
  34.    public void saveBook(
  35.           @RequestParam(required=false, value = "name") String name,
  36.           @RequestParam(required=false, value = "author") String author,
  37.           @RequestParam(required=false, value = "isbnNumber") String isbnNumber,
  38.           ActionRequest request, ActionResponse response) throws Exception {
  39.  
  40.           Map<String, String> errorMap = new HashMap<String, String>();
  41.           …
  42.           …
  43.           // – The rest of implementation are same.
  44.           …
  45.          if(!errorMap.isEmpty()) {
  46.               …
  47.               response.setRenderParameter("myaction","addBookForm");
  48.          } else {
  49.              bookService.addBook(
  50.                       new Book(name,author,Long.valueOf(isbnNumber)));
  51.              response.setRenderParameter("myaction","");
  52.           }
  53.  
  54.  

Configuration

  1.  
  2. <bean id="portletModeParameterHandlerMapping"
  3.         class="org.springframework.web.portlet.handler.PortletModeParameterHandlerMapping">
  4. <property name="parameterName" value="myaction" />;
  5. <property name="order" value="1" />
  6. <property name="interceptors">
  7.      <list>
  8.           <ref bean="parameterInterceptor" />
  9.      </list>
  10. </property>
  11.      <property name="defaultHandler" ref="showBooksController" />
  12.      <property name="portletModeParameterMap">
  13.          <map>
  14.               <entry key="view">
  15.                    <map>
  16.                       <entry key="addBook" value-ref="addBookController" />
  17.                       <entry key="addBookForm" value-ref="addBookController" />
  18.                       <entry key="editBook" value-ref="editBookController" />
  19.                       <entry key="editBookForm" value-ref="editBookController" />
  20.                       <entry key="removeBook" value-ref="removeBookController" />
  21.                    </map>
  22.               </entry>
  23.           </map>
  24. </property>
  25. </bean>
  26.  

PortletModeParameterHandlerMapping

As mentions on above bean definitions, portletModeParameterMap has entry key ‘view. We replace by using @RequestMapping(“VIEW”) annotation in BooksController. Which mean a view mode entry also support other sub actions, such as addBook, addBookForm, editBook, editBookForm, removeBook. We replace by using @RequestMapping(params = “action=[xxx-mappings]“).

(Note: default action name = action,  can be customize in property parameterName).

  1. myactions=addBook
  2. myactions=addBookForm
  3. myactions=editBook
  4. myactions=editBookForm
  5. myactions=removeBook
You can leave a response, or trackback from your own site.

Leave a Reply

Security Code: