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.
-
AddBookController
-
BooksController
-
EditBookController
-
EditModeController
-
HelpModeController
-
RemoveBookController
-
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.
-
Add/Edit existing portlet declaration in portlet.xml
-
Add/Edit existing portlet declaration in liferay-portlet.xml
-
Create an existing a book-portlet.xml
Next, we quickly look through the following xml files.
-
portletModeParameterHandlerMappingExample-portlet.xml and
-
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.
-
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans
-
xmlns="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:p="http://www.springframework.org/schema/p"</span>
-
xmlns:context="http://www.springframework.org/schema/context"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-
"http://www.springframework.org/schema/context
-
"http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
-
<context:component-scan base-package="com.loongest.book.controller>
-
<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/view/book/" />
-
<property name="suffix" value=".jsp" />
-
</bean>
-
</beans>
-
-
Modify BooksController
-
-
package com.loongest.book.controller;
-
import ……..;
-
-
@RequestMapping("VIEW")
-
public class BooksController {
-
@Autowired private BookService bookService;
-
-
@RequestMapping
-
publi void postAction() {
-
// – if void method mean ‘POST’, default view mode nothing do post,
-
// – so leave it blank implementation
-
}
-
-
@RequestMapping
-
// – Return type is String, means we will return books jsp for render.
-
List<Books> books = bookService.getBooks();
-
map.put("books",books);
-
return "books";
-
}
-
-
// – Refer to the original xml declaration,
-
// – addBookForm are map to addBookController to render addBook.jsp
-
-
@RequestMapping(params = "myaction=addBookForm")
-
return "addBook";
-
}
-
-
// – form submit action, when user click add book button, portlet will get form parameter and
-
// – validate. if contains errors, will show form again else back to default view. myaction=""
-
@RequestMapping(params = "myaction=addBook")
-
public void saveBook(
-
-
Map<String, String> errorMap = new HashMap<String, String>();
-
…
-
…
-
// – The rest of implementation are same.
-
…
-
if(!errorMap.isEmpty()) {
-
…
-
response.setRenderParameter("myaction","addBookForm");
-
} else {
-
bookService.addBook(
-
response.setRenderParameter("myaction","");
-
}
-
-
Configuration
-
-
<bean id="portletModeParameterHandlerMapping"
-
class="org.springframework.web.portlet.handler.PortletModeParameterHandlerMapping">
-
<property name="parameterName" value="myaction" />;
-
<property name="order" value="1" />
-
<property name="interceptors">
-
<list>
-
<ref bean="parameterInterceptor" />
-
</list>
-
</property>
-
<property name="defaultHandler" ref="showBooksController" />
-
<property name="portletModeParameterMap">
-
<map>
-
<entry key="view">
-
<map>
-
<entry key="addBook" value-ref="addBookController" />
-
<entry key="addBookForm" value-ref="addBookController" />
-
<entry key="editBook" value-ref="editBookController" />
-
<entry key="editBookForm" value-ref="editBookController" />
-
<entry key="removeBook" value-ref="removeBookController" />
-
</map>
-
</entry>
-
</map>
-
</property>
-
</bean>
-
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).
- myactions=addBook
- myactions=addBookForm
- myactions=editBook
- myactions=editBookForm
- myactions=removeBook

Posted in 


