PS:写这些文章的目的是用来记录我学习Liferay Portal 6.x的一个过程,如有错误欢迎指出一起学习。
Liferay版本:6.0.4
开发模式:普通插件模式(非Ext)
Struts版本:2.1.8.1
使用到的Struts2相关jar包如下:
antlr-2.7.2.jar
commons-fileupload-1.2.1.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
struts2-portlet-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
工程目录如下:
参考Struts2自带的portlet示例struts2-portlet-2.1.8.1.war,在Struts解压包下面的apps目录里面,这个示例是基于Pluto(http://portals.apache.org/pluto/)Apache的Portal实现,但开发的都是portlet,所以可以参考这个。
portlet.xml
<portlet> <portlet-name>StrutsPortlet</portlet-name> <display-name>Struts Test Portlet</display-name> <portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class> <!--这里的name名字不能修改,value对应于Struts.xml文件里面的配置--> <init-param> <name>viewNamespace</name> <value>/strutsDemoView</value> </init-param> <!-- 对应Struts.xml中设置的默认action --> <init-param> <name>defaultViewAction</name> <value>index</value> </init-param> <init-param> <name>editNamespace</name> <value>/strutsDemoEdit</value> </init-param> <init-param> <name>defaultEditAction</name> <value>index</value> </init-param> <expiration-cache>0</expiration-cache> <!-- 支持view、edit,和前面的portlet一样 --> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> <portlet-mode>edit</portlet-mode> </supports> <portlet-info> <title>My StrutsPortlet portlet</title> <short-title>SP</short-title> <keywords>struts,portlet</keywords> </portlet-info> </portlet>
liferay-portlet.xml
<portlet> <portlet-name>StrutsPortlet</portlet-name> <instanceable>true</instanceable> </portlet>
liferay-display.xml
<portlet id="StrutsPortlet"></portlet>
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="strutsDemoView" namespace="/strutsDemoView" extends="struts-portlet-default"> <action name="index" method="view" class="org.huqiwen.study.StrutsDemo"> <result name="success">/strutsDemoView/view.jsp</result> </action> <action name="portletView" method="portletView" class="org.huqiwen.study.StrutsDemo"> <result name="view">/strutsDemoView/viewMode.jsp</result> </action> </package> <package name="strutsDemoEdit" namespace="/strutsDemoEdit" extends="struts-portlet-default"> <action name="index" method="edit" class="org.huqiwen.study.StrutsDemo"> <result name="success">/strutsDemoEdit/edit.jsp</result> </action> <action name="portleEdit" method="portletEdit" class="org.huqiwen.study.StrutsDemo"> <result name="edit">/strutsDemoEdit/editMode.jsp</result> </action> </package> </struts>
java文件
import com.opensymphony.xwork2.ActionSupport; public class StrutsDemo extends ActionSupport { private static final long serialVersionUID = 1L; private String message; public String getMessage(){ return message; } public void setMessage(String message){ this.message=message; } public String view() throws Exception { message = "hello Struts2 View Portlet!"; return SUCCESS; } public String portletView(){ return "view"; } public String edit() throws Exception { message = "hello Struts2 Edit Portlet!"; return SUCCESS; } public String portletEdit(){ return "edit"; } }
strutsDemoView/view.jsp
<%@ page contentType="text/html;charset=utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <h2><s:property value="message"/></h2> <s:form action="portletView!portletView.action" method="post" namespace="view"> <s:textfield label="名字" key="message"></s:textfield> <s:submit value="提交"></s:submit> </s:form>
其他的JSP不写了,和普通的Strtus2开发一样