准备
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>