Category Archives: Spring 3.1 portlet

Spring 3.1 Portlet with Liferay 6.1 Part 2

Previous Spring Introduction
Lets have an example:
We will now create a small Employee Registration portlet using Spring 3.1 with Liferay 6.1.

  • We need Spring-specific JAR files, you can download Spring 3.1 release JAR files from Spring source website (http://www.springsource.org/download).
  • Create a Liferay Plugin Portlet in Eclipse. Add the JARs files in WEB-INF/lib directory of the project structure. Below is the Spring Portlet Project Structure. Where you can see the WEB-INF/lib with required JARs for the portlet
  • Spring Portlet Structure

Create Service Builder ie service.xml and copy the content of the below code.
service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" 
"http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.navin.example.liferay">
	<author>NavinAgarwal</author>
	<namespace>emp</namespace>
	<entity name="Employee" local-service="true" remote-service="true">
		<!-- PK fields -->
		<column name="employeeId" type="long" primary="true" id-type="identity"/>
		<!-- Audit fields -->
		<column name="companyId" type="long" />
		<column name="userId" type="long" />
		<column name="firstName" type="String" />
		<column name="lastName" type="String" />
		<column name="eMail" type="String" />
		<column name="phoneNo" type="String" />
		
		<!-- Order -->
		<order by="asc">
			<order-column name="firstName" />
		</order>
		<!-- Finder methods -->
		<finder name="firstNameCollection" return-type="Collection">
			<finder-column name="firstName" />
		</finder>
	</entity>
</service-builder>

Build the service.xml and the service and other imple Classes and interfaces will be generated.

Create the EmployeePortlet-context.xml file inside WEB-INF/context/portlet/employee/ folder structure. and paste the below code.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
	
	<context:component-scan base-package="com.navin.example.action.EmployeeController" />
	<bean id="portletModeHandlerMapping"
		class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
		<property name="portletModeMap">
			<map>
				<entry key="view">
					<ref bean="EmployeeController" />
				</entry>
			</map>
		</property>
	</bean>	
	<bean id="EmployeeController" class="com.navin.example.action.EmployeeController" />		
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.InternalResourceView" />
		<property name="prefix" value="/html/employee/" />
		<property name="suffix" value=".jsp" />
	</bean>	
</beans>

Now create the applicationContext.xml file inside WEB-INF/context/ . In applicationContext.xml we are not mentioning anything now. Since, we are not doing any configuration of any database and declaring and beans.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    	http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

</beans>

Now WEB.xml . In web.xml we need to write the ViewRendererServlet Class for the ViewRendererServlet servlet mapping.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>EmployeeExample-portlet</display-name>	
	<servlet>
           <servlet-name>ViewRendererServlet</servlet-name>
           <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
           <load-on-startup>1</load-on-startup>
      </servlet>
    
     <servlet-mapping>
        <servlet-name>ViewRendererServlet</servlet-name>
        <url-pattern>/WEB-INF/servlet/view</url-pattern>
     </servlet-mapping>

    <jsp-config>
      <taglib>
         <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
  	 <taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
      </taglib>
     </jsp-config>
</web-app>

Portlet.xml here we have to mention the portlet class name org.springframework.web.portlet.DispatcherPortlet.
And the contextConfigLocation Value which is /WEB-INF/context/portlet/employee/EmployeePortlet-context.xml

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd 
       http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
	
	<portlet>
		<portlet-name>employeedetails</portlet-name>
		<display-name>Employee Details</display-name>
		<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
		<init-param>
			<name>contextConfigLocation</name>
            <value>/WEB-INF/context/portlet/employee/EmployeePortlet-context.xml</value>
		</init-param>
		<expiration-cache>0</expiration-cache>
		<supports>
			<mime-type>text/html</mime-type>
			<portlet-mode>view</portlet-mode>
		</supports>
		<portlet-info>
			<title>Employee Details</title>
			<short-title>Employee Details</short-title>
			<keywords></keywords>
		</portlet-info>
		<security-role-ref>
			<role-name>administrator</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>guest</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>power-user</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>user</role-name>
		</security-role-ref>
	</portlet>
</portlet-app>

Next Page for the JAVA Controller and JSP

Advertisements