1.Define the Action
struts-config.xml, add
<
action
path
="/ext/library/add_book"
type
="com.ext.portlet.library.action.AddBookAction"
>
< forward name ="portlet.ext.library.error" path ="portlet.ext.library.error" />
< forward name ="portlet.ext.library.success" path ="portlet.ext.library.success" />
</ action >
< forward name ="portlet.ext.library.error" path ="portlet.ext.library.error" />
< forward name ="portlet.ext.library.success" path ="portlet.ext.library.success" />
</ action >
tiles-defs.xml, add
<
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 >
< 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 >
2.Update existing JSP files
init.jsp, remove the following:
<p>Add commonly used variables and declarations here!</p>
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 />
< 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 />
3.Create success and error JSP files
error.jsp, Contents:
ERROR!
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 >
</ table >
< %
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 >
</ table >
4.Create Action Class to process submit
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;
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 ( null == bookTitle || "".equals(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");
}
}
}
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;
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 ( null == bookTitle || "".equals(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");
}
}
}
Finally, .../ext>ant deploy
Restart Tomcat, ...