---------------------------------------------------------------------------------------------------------------------------src/main/java/com/mycompany/EditBookmarkAction.java public class EditBookmarkAction extends DefaultActionSupport implements PortletPreferencesAware, Preparable, ParameterAware { private String oldName; private String name; private String url; private PortletPreferences portletPreferences; private Map<String, String[]> parameters; public String getOldName() { return oldName; } public void setOldName(String oldName) { this.oldName = oldName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public void setName(String name) { this.name = name; } public void setPortletPreferences(PortletPreferences portletPreferences) { this.portletPreferences = portletPreferences; } public void setParameters(Map<String, String[]> parameters) { this.parameters = parameters; } public void prepare() throws Exception { // Since the prepare interceptor is run before the parameter interceptor, // we have to get the parameter "manually". this.oldName = parameters.get("oldName")[0]; this.url = portletPreferences.getValue(oldName, null); } public String execute() throws Exception { // The modification is handled as remove/add portletPreferences.reset(oldName); portletPreferences.setValue(name, url); portletPreferences.store(); return SUCCESS; } }
------------------------------------------------------------------src/main/resources/struts.xml <action name="editBookmark" class="com.mycompany.EditBookmarkAction"> <result type="redirectAction"> <param name="actionName">index</param> </result> <result name="input">/WEB-INF/jsp/edit/edit.jsp</result> </action>
src/main/resources/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="default" extends="struts-portlet-default" namespace="/view"> <action name="index" class="com.mycompany.HelloAction"> <result>/WEB-INF/jsp/view/index.jsp</result></action> </package> <package name="edit" extends="struts-portlet-default" namespace="/edit"><action name="index" class="com.mycompany.UpdateNameAction"> <result type="redirectAction"> <param name="actionName">index</param><param name="portletMode">view</param> </result> <result name="input">/WEB-INF/jsp/edit/index.jsp</result></action> </package></struts>-----------------------------------------------------------------------------------------------------------------------rc/main/java/com/mycompany/domain/Bookmark.java public class Bookmark { private String name; private String url; public Bookmark(String name, String url) { this.name = name; this.url = url; } public String getName() { return name; } public String getUrl() { return url; } }
---------------------------------------------------------------------------------------------------------src/main/java/com/mycompany/AddBookmark.java public class AddBookmarkAction extends DefaultActionSupport { private String name; private String url; public void setName(String name) { this.name = name; } public void setUrl(String url) { this.url = url; } @Override public String execute() throws Exception { return SUCCESS; } }
------------------------------------------------------------------------------------------------------struts.xml <package name="edit" extends="struts-portlet-default" namespace="/edit"> <action name="index" class="com.mycompany.AddBookmarkAction"> <result name="input">/WEB-INF/jsp/edit/index.jsp</result> </action> </package>
------------------------------------------------------------------------------------------------------src/main/webapp/WEB-INF/jsp/edit/index.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <h2>Manage bookmarks</h2> <s:form action="index"> <table> <s:textfield name="name" label="Name"/> <s:textfield name="url" label="URL"/> <s:submit value="Add"/> </table> </s:form>
----------------------------------------------------------------------------------------------------------src/main/java/com/mycompany/AddBookmarkAction.java public class AddBookmarkAction extends DefaultActionSupport implements PortletPreferencesAware { private String name; private String url; private PortletPreferences portletPreferences; public void setName(String name) { this.name = name; } public void setUrl(String url) { this.url = url; } public void setPortletPreferences(PortletPreferences portletPreferences) { this.portletPreferences = portletPreferences; } @Override public String execute() throws Exception { portletPreferences.setValue(name, url); portletPreferences.store(); return SUCCESS; } }
-----------------------------------------------------------------------------------------------------------src/main/resources/struts.xml <package name="edit" extends="struts-portlet-default" namespace="/edit"> <action name="index" class="com.mycompany.AddBookmarkAction"> <result type="redirectAction"> <param name="actionName">index!input</param> </result> <result name="input">/WEB-INF/jsp/edit/index.jsp</result> </action> </package>
-----------------------------------------------------------------------------------------------------------------------src/main/java/com/mycompany/ListBookmarksAction.java public class ListBookmarksAction extends DefaultActionSupport implements PortletPreferencesAware { private List<Bookmark> bookmarks = new ArrayList<Bookmark>(); private PortletPreferences portletPreferences; public List<Bookmark> getBookmarks() { return bookmarks; } public void setPortletPreferences(PortletPreferences portletPreferences) { this.portletPreferences = portletPreferences; } @Override public String execute() throws Exception { // For simplicity, we'll assume that only bookmarks are stored in the preferences. Map<String, String[]> preferencesMap = portletPreferences.getMap(); for(Map.Entry<String, String[]> entry : preferencesMap.entrySet()) { bookmarks.add(new Bookmark(entry.getKey(), entry.getValue()[0])); } return SUCCESS; } }
src/main/webapp/WEB-INF/jsp/view/index.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <strong>Bookmarks</strong> <p> <table> <s:iterator value="%{bookmarks}" var="bookmark"> <tr> <td><s:property value="%{name}"/></td> <td><a href="<s:property value="%{url}"/>" target="_blank"><s:property value="%{url}"/></a></td> </tr> </s:iterator> </table> </p>
-----------------------------------------------------------------------------------------------------------------------src/main/resources/struts.xml <package name="view" extends="struts-portlet-default" namespace="/view"> <action name="index" class="com.mycompany.ListBookmarksAction"> <result>/WEB-INF/jsp/view/index.jsp</result> </action> </package>
------------------------------------------------------------------------------------------------------------------------------src/main/webapp/WEB-INF/jsp/edit/index.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <h2>Manage bookmarks</h2> <p> <table> <s:iterator value="%{bookmarks}" var="bookmark"> <s:url action="editBookmark!input" id="editUrl"> <s:param name="oldName" value="%{name}"/> </s:url> <s:url action="deleteBookmark" portletUrlType="action" id="deleteUrl"> <s:param name="bookmarkName" value="%{name}"/> </s:url> <tr> <td><s:property value="%{name}"/></td> <td><a href="<s:property value="%{url}"/>" target="_blank"><s:property value="%{url}"/></a></td> <td><a href="<s:property value="%{editUrl}"/>">Edit</a></td> <td><a href="<s:property value="%{deleteUrl}"/>">Delete</a></td> </tr> </s:iterator> </table> </p> <s:form action="addBookmark"> <table> <s:textfield name="name" label="Name"/> <s:textfield name="url" label="URL"/> <s:submit value="Add"/> </table> </s:form>
src/main/resources/struts.xml <package name="edit" extends="struts-portlet-default" namespace="/edit"> <action name="index" class="com.mycompany.ListBookmarksAction"> <result>/WEB-INF/jsp/edit/index.jsp</result> </action> <action name="addBookmark" class="com.mycompany.AddBookmarkAction"> <result type="redirectAction"> <param name="actionName">index</param> </result> </action> </package> ------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------src/main/webapp/WEB-INF/portlet.xml <!-- The default action to invoke in edit mode. --> <init-param> <name>defaultEditAction</name> <value>index</value> </init-param>
src/main/java/com/mycompany/DeleteBookmarkAction.java public class DeleteBookmarkAction extends DefaultActionSupport implements PortletPreferencesAware{ private String bookmarkName; private PortletPreferences portletPreferences; public void setBookmarkName(String bookmarkName) { this.bookmarkName = bookmarkName; } public void setPortletPreferences(PortletPreferences portletPreferences) { this.portletPreferences = portletPreferences; } @Override public String execute() throws Exception { portletPreferences.reset(bookmarkName); portletPreferences.store(); return SUCCESS; } } ---------------------------------------------------------------------------------------------------------------------------------
src/main/resources/struts.xml <action name="deleteBookmark" class="com.mycompany.DeleteBookmarkAction"> <result type="redirectAction"> <param name="actionName">index</param> </result> </action>
----------------------------------------------------------------------------------------------------------------------------src/main/webapp/WEB-INF/jsp/edit.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <h2>Edit bookmark</h2> <s:form action="editBookmark"> <input type="hidden" name="oldName" value="<s:property value="%{oldName}"/>"/> <table> <s:textfield name="name" label="Name" value="%{oldName}"/> <s:textfield name="url" label="URL"/> <s:submit value="Update"/> </table> </s:form>
src/main/webapp/WEB-INF/portlet.xml <?xml version="1.0" encoding="UTF-8"?> <portlet-app version="1.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" 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" id="bookmark-portlet"> <portlet id="HelloPortlet"> <description xml:lang="EN">Simple hello world portlet</description> <portlet-name>HelloPortlet</portlet-name> <display-name xml:lang="EN">bookmark-portlet</display-name> <portlet-class>org.apache.struts2.portlet.dispatcher.Jsr168Dispatcher</portlet-class> <!-- The namespace for the actions configured for view mode --> <init-param> <name>viewNamespace</name> <value>/view</value> </init-param> <!-- The default action to invoke in view mode. --> <init-param> <name>defaultViewAction</name> <value>index</value> </init-param> <!-- The namespace for the actions configured for edit mode --> <init-param> <name>editNamespace</name> <value>/edit</value> </init-param> <!-- The default action to invoke in edit mode. --> <init-param> <name>defaultEditAction</name> <value>index!input</value> </init-param> <expiration-cache>0</expiration-cache> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> <portlet-mode>edit</portlet-mode> </supports> <supported-locale>en</supported-locale> <portlet-info> <title>HelloPortlet</title> <short-title>HelloPortlet</short-title> <keywords>struts 2,portlet,hello,world</keywords> </portlet-info> </portlet> </portlet-app>
创建 simple Bookmark Portlet
最新推荐文章于 2022-06-06 23:40:52 发布