0%

使用springmvc开发portlet

本文将介绍使用springmvc开发一个portlet。项目全部代码已托管在github,地址为:https://github.com/zhaiyz/springmvc-portlet

开发环境

  • OS:ubuntu 12.04 64位
  • Liferay Portal:6.2 CE GA2(6.2.1)
  • JDK:1.6.0_45
  • Maven:3.0.5
  • Eclipse:3.7.2(indigo)
  • Spring:3.2.4.RELEASE

创建一个portlet

依照使用eclipse开发liferay maven项目中的方法,创建一个名为springmvc-portlet的portlet。

添加springmvc-portlet依赖

pom.xml中添加spring-webmvc-portlet依赖,以及jstl依赖。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<project>
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
...

</project>

添加web.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0"?>

<web-app 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">
<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
</web-app>

创建SpringMvcPortletController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.zhaiyz;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

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

/**
* SpringMvcPortletController
*
* @author zhaiyz
*/
@Controller
@RequestMapping(value = "VIEW")
public class SpringMvcPortletController {

@RenderMapping
public String showForm(RenderRequest request, RenderResponse response) throws Exception {
return "view";
}
}

创建springmvc-portlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.zhaiyz" />

<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>
</beans>

修改portlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>springmvc-portlet</portlet-name>
<display-name>springmvc-portlet</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>classpath:springmvc-portlet.xml</value>
</init-param>
<expiration-cache>0</expiration-cache>
.
.
.
</portlet>
</portlet-app>

创建jsp目录及view.jsp文件

WEB-INF下创建jsp目录,并把自动创建的在webapp目录下的view.jsp移动到jsp目录。此时,项目的目录结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
.
├── pom.xml
└── src
└── main
├── java
│   └── com
│   └── zhaiyz
│   └── SpringMvcPortletController.java
├── resources
│   └── springmvc-portlet.xml
└── webapp
├── css
│   └── main.css
├── icon.png
├── js
│   └── main.js
└── WEB-INF
├── jsp
│   └── view.jsp
├── liferay-display.xml
├── liferay-plugin-package.properties
├── liferay-portlet.xml
├── portlet.xml
└── web.xml

11 directories, 12 files

打包发布

1
mvn clean pakcage liferay:deploy

启动tomcat,启动完成后。可应用程序的示例分类中找到新创建的portlet,名为springmvc-portlet。添加此portlet到页面,可以看到如下显示效果。
springmvc_portlet

参考文献

  1. Development of a Simple Portlet with SpringMVC
  2. Liferay 6.1开发学习(十七):基于注解的SpringMVC portlet开发
  3. java.lang.ClassNotFoundException : javax.servlet.jsp.jstl.core.Config

EOF