2009年12月3號
Struts2.1.8.1 + Pluto1.1.7 + portletV1.0(JSR 168) CRUD Demo(20091203止)
本來想用portlet2.0(JSR 268)整合Struts2的,但是Struts2的最新版的Portlet插件JAR包也僅僅支持JSR 168規範,所以只能退回去用戶JSR168了。。。
由於Pluto1.X系列和Pluto2.X系列配置PortletV1.0和V2.0的配置文件不同,這裡不闡述1系列的配置步驟,只提供已經由手工配置好的apache-tomcat-6.0.20版本+Pluto1.1.7版本+PortletV1.0版本的TOMCAT應用服務器(注意附件要將三部份全部下載完才可以應用)
如圖所示:展示Struts2與Portlet1.0 整合的數據CRUD操作(增刪改查)
DEMO源代碼結構圖:
部份源代碼展示:
package cn.myhongkongzhen.demo.portlet.action; import java.util.List; import cn.myhongkongzhen.demo.portlet.bean.UserBean; import cn.myhongkongzhen.demo.portlet.service.IUserService; import cn.myhongkongzhen.demo.portlet.service.impl.UserServiceImpl; import com.opensymphony.xwork2.ActionSupport; /******************************************************************************* * User Actions * * @author Jane * */ public class UserAction extends ActionSupport { private IUserService service; private String userName; private String password; private String userId; private List<UserBean> listUser; private static final long serialVersionUID = -1750501914156696345L; public String add() { UserBean user = new UserBean(); user.setPassword(password); user.setUserName(userName); service = new UserServiceImpl(); service.addUser(user); return "success"; } public String delete() { service = new UserServiceImpl(); service.delete(Integer.parseInt(userId)); return "success"; } public String editReady() { return "editReady"; } public String edit() { service = new UserServiceImpl(); UserBean user = new UserBean(); user.setId(Integer.parseInt(userId)); user.setUserName(userName); user.setPassword(password); service.edit(user); return "success"; } public String listUser() { service = new UserServiceImpl(); listUser = service.listAllUser(); return "list"; } public String viewIndex() { return "viewIndex"; } public String addReady() { return "addReady"; } public String editIndex() { return "editIndex"; } public String helpIndex() { return "helpIndex"; } public IUserService getService() { return service; } public void setService(IUserService service) { this.service = service; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List<UserBean> getListUser() { return listUser; } public void setListUser(List<UserBean> listUser) { this.listUser = listUser; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }
<?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> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="user" extends="struts-portlet-default" namespace="/user" abstract="true"> <default-class-ref class="cn.myhongkongzhen.demo.portlet.action.UserAction"> </default-class-ref> </package> <package name="view" extends="user" namespace="/user/view"> <action name="user_*" method="{1}"> <result name="viewIndex">/jsp/view/index.jsp</result> <result name="list">/jsp/view/list.jsp</result> </action> </package> <package name="edit" extends="user" namespace="/user/edit"> <action name="user_*" method="{1}"> <result name="editIndex">/jsp/edit/index.jsp</result> <result name="addReady">/jsp/edit/add.jsp</result> <result name="editReady" type="redirect"> /jsp/edit/edit.jsp?userId=${userId}&userName=${userName}&password=${password} </result> <result name="success" type="redirect"> /user/view/user_listUser.action </result> </action> </package> <package name="help" extends="user" namespace="/user/help"> <action name="user_*" method="{1}"> <result name="helpIndex">/jsp/help/index.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"> <portlet id="Struts2PortletDemo"> <description>Struts 2 Portlet Demo</description> <portlet-name>Struts2PortletDemo</portlet-name> <portlet-class> org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher </portlet-class> <init-param> <name>portletNamespace</name> <value>/user</value> </init-param> <init-param> <name>viewNamespace</name> <value>/view</value> </init-param> <init-param> <name>defaultViewAction</name> <value>user_viewIndex</value> </init-param> <init-param> <name>editNamespace</name> <value>/edit</value> </init-param> <init-param> <name>defaultEditAction</name> <value>user_editIndex</value> </init-param> <init-param> <name>helpNamespace</name> <value>/help</value> </init-param> <init-param> <name>defaultHelpAction</name> <value>user_helpIndex</value> </init-param> <expiration-cache>1</expiration-cache> <supports> <mime-type>text/html</mime-type> <portlet-mode>EDIT</portlet-mode> <portlet-mode>VIEW</portlet-mode> <portlet-mode>HELP</portlet-mode> </supports> <portlet-info> <title>Struts 2 Portlet Demo</title> </portlet-info> </portlet> </portlet-app>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- <servlet id="Struts2PortletDispatcherServlet"> <servlet-name>Struts2PortletDispatcherServlet</servlet-name> <servlet-class> org.apache.struts2.portlet.dispatcher.DispatcherServlet </servlet-class> </servlet> --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>Struts2PortletV1Demo</servlet-name> <servlet-class> org.apache.pluto.core.PortletServlet </servlet-class> <init-param> <param-name>portlet-name</param-name> <param-value>Struts2PortletDemo</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Struts2PortletV1Demo</servlet-name> <url-pattern>/PlutoInvoker/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <h2> User List Page </h2> <table> <tr> <th> Id </th> <th> UserName </th> <th> Operate </th> </tr> <s:iterator value="%{listUser}" var="user"> <tr> <td> <s:property value="%{id}" /> </td> <td> <s:property value="%{userName}" /> </td> <td> <s:url action="user_editReady" portletUrlType="action" namespace="/user/edit" id="editUrl"> <s:param name="userId" value="%{id}" /> <s:param name="userName" value="%{userName}" /> <s:param name="password" value="%{password}" /> </s:url> <a href='<s:property value="%{editUrl}" />'>Edit</a> </td> <td> <s:url action="user_delete" portletUrlType="action" namespace="/user/edit" id="deleteUrl"> <s:param name="userId" value="%{id}" /> </s:url> <a href='<s:property value="%{deleteUrl}" />'>Delete</a> </td> </tr> </s:iterator> </table> <p> <ul> <li> <a href='<s:url action="user_addReady" portletMode="edit"/>'>Go to Add User Page</a> </li> <p> <li> <a href='<s:url action="user_viewIndex" portletMode="view" />'>Go to View Index Page</a> </li> <li> <a href='<s:url action="user_editIndex" portletMode="edit" />'>Go to Edit Mode</a> </li> <li> <a href='<s:url action="user_helpIndex" portletMode="help" />'>Go to Help Mode</a> </li> </ul>
<%@ page language="java" isErrorPage="true" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <h2> Add User </h2> <s:form action="user_add"> <table> <s:textfield name="userName" label="userName" /> <s:password name="password" label="password" /> <s:submit value="Add User" /> </table> </s:form>
<%@ page language="java" isErrorPage="true" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <h2> Edit User </h2> <s:form action="user_edit"> <table> <s:hidden name="userId" value="%{userId}" /> <s:textfield name="userName" label="userName" value="%{userName}" /> <s:password name="password" label="password" value="%{password}" /> <s:submit value="Edit User" /> </table> </s:form>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <h1> Struts 2 Portlet Demo : <br> Mode Edit </h1> <ul> <li> <a href='<s:url action="user_addReady" />'>Go to Add User Page</a> </li> <p> <li> <a href='<s:url action="user_helpIndex" portletMode="help" />'>Go to Help Mode</a> </li> <li> <a href='<s:url action="user_viewIndex" portletMode="view" />'>Go to View Mode</a> </li> </ul> <<s:url></s:url>
其餘代碼在附件中的源代碼中
注意:DEMO中的相關jar包是通過tomcat中lib中引入的,將附件中的DEMO源碼下載下來后腰手工修改引入jar的位置,最好將附件中的tomcat-pluto-1三個部份zip下下來,重新制定tomcat的位置,然後修改工程中引入jar包的位置
歡迎討論
參考:
Struts2 Portlet Plugin(http://struts.apache.org/2.x/docs/portlet-plugin.html)
Pluto1.1 Getting Start(http://portals.apache.org/pluto/v11/getting-started.html)
Portlet API 1.0
Struts2 API
Struts---Portlet---Plugin API