Tag Archives: Spring 3.1

Spring 3.1 Portlet with Liferay 6.1 part 3

Previous Post

Now we will write the JSP and Java Controller with business logics.

JSPs as per folder you may put either inside or outside the /WEB-INF. Here I have mention outside the WEB-INF Below is the structure.

Spring Portlet

JSP createEmployee.jsp is a simple Action form , in which we are taking the Employee Details.

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<portlet:defineObjects />
<portlet:actionURL var="createEmployeeDetailURL">
	<portlet:param name="action" value="createEmployee" />
</portlet:actionURL>
<aui:form action="<%=createEmployeeDetailURL %>" method="post" name="createEmp">
	<aui:layout>			
	     <aui:fieldset>
		   <aui:column>
		 	<aui:input type="text" name="firstName" label="First Name" />
                        <aui:input type="text" name="LastName" label="Last Name" />
         		<aui:input type="text" name="email" label="Email" />
			<aui:input type="text" name="phoneNo" label="Phone No" />
	        	<aui:button type="submit" value="Save"  />
		   </aui:column>
            </aui:fieldset>	
	</aui:layout>	
</aui:form>

And when the Action is trigged Its search for the controller and map with the action param createEmployee. Once it land into the method it execute the business logic of the method.

Here is the EmployeeController.java Controller.
Inside the @ActionMapping we are getting all the params valus and setting them into the employee object
[Employee employee = new EmployeeImpl();]

Then we are calling the EmployeeLocalServiceUtil.addEmployee(employee); to add the Employee Details into database.

package com.navin.example.action;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.navin.example.liferay.model.Employee;
import com.navin.example.liferay.model.impl.EmployeeImpl;
import com.navin.example.liferay.service.EmployeeLocalServiceUtil;

/**
 * Portlet implementation class EmployeeController
 * 
 * @author NavinAgarwal
 *
 */
@Controller
public class EmployeeController {
 
	
	@RenderMapping
	public String createEmployee(RenderRequest request,RenderResponse response) {	
		return "createEmployee";    
	}
	
	@RenderMapping(params = "redirectPage=welcome")
	public String registration(RenderRequest request,RenderResponse response) {
		return "welcome";
	}
	
	@ActionMapping(params = "action=createEmployee")
	public void createEmployee(ActionRequest request,ActionResponse response)throws SystemException
	{
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		Employee employee = new EmployeeImpl();
		employee.setCompanyId(themeDisplay.getCompanyId());		
		employee.setUserId(themeDisplay.getUserId());
		employee.setFirstName(request.getParameter("firstName"));
		employee.setLastName(request.getParameter("LastName"));
		employee.setEMail(request.getParameter("email"));
		employee.setPhoneNo(request.getParameter("phoneNo"));
						
		EmployeeLocalServiceUtil.addEmployee(employee);
		response.setRenderParameter("redirectPage", "welcome");
		request.setAttribute("EmployeeName",request.getParameter("firstName")+" "+request.getParameter("LastName"));
	}
	
}


Thanks

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