<action path="/ext/library/add_book" type="com.ext.portlet.library.action.AddBookAction">
<forward name="portlet.ext.library.view" path="portlet.ext.library.view" />
<forward name="portlet.ext.library.error" path="portlet.ext.library.error" />
<forward name="portlet.ext.library.success" path="portlet.ext.library.success" />
</action>
2.tiles-defs.xml
<definition name="portlet.ext.library.error" extends="portlet.ext.library">
<put name="portlet_content" value="/portlet/ext/library/error.jsp" />
</definition>
<definition name="portlet.ext.library.success" extends="portlet.ext.library">
<put name="portlet_content" value="/portlet/ext/library/success.jsp" />
</definition>
=============================
And create these JSP files:(location : /ext/ext-web/docroot/html/portlet/ext/library)
init.jsp
(remove the text line added earlier)
<%@ include file="/html/common/init.jsp" %>
view.jsp
<%@ include file="/html/portlet/ext/library/init.jsp" %>
<br/>
Add a book entry to the Library:
<br/><br/>
<form action="<portlet:actionURL windowState="<%= WindowState.MAXIMIZED.toString() %>">
<portlet:param name="struts_action" value="/ext/library/add_book" />
</portlet:actionURL>" method="post" name="<portlet:namespace />fm">
Book Title:
<input name="<portlet:namespace />book_title" size="20" type="text" value=""><br/><br/>
<input type="button" value="Submit" onClick="submitForm(document.<portlet:namespace />fm);">
</form>
<br/>
success.jsp
<%@ include file="/html/portlet/ext/library/init.jsp" %>
<%
String bookTitle = request.getParameter("book_title");
%>
<table align="center" cellspacing="10" cellpadding="3">
<tr>
<td style="font-weight:bold">Book Title: </td>
<td><%= bookTitle %></td>
</tr>
error.jsp
<%@ include file="/html/portlet/ext/library/init.jsp" %>
<font color="red">Error in page...</font>
=============================
Create this Java file:
AddBookAction.java
(location: ext/ext-ejb/src/com/ext/portlet/library/action/AddBookAction.java)
package com.ext.portlet.library.action;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.kernel.util.Validator;
public class AddBookAction extends PortletAction {
public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception {
String bookTitle = req.getParameter("book_title");
if (Validator.isNull(bookTitle)) {
setForward(req, "portlet.ext.library.error");
} else {
setForward(req, "portlet.ext.library.success");
}
}
public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
if (getForward(req) != null && !getForward(req).equals("")) {
return mapping.findForward(getForward(req));
} else {
return mapping.findForward("portlet.ext.library.view");
}
}
}
Now you can get, simple portlet with struts acton.
- Gnaniyar Zubair
