<<

. 59
( 87 .)



>>

portlet view mode is controlled from the base struts con¬guration. You will
P1: FCH
WY009-18 WY009-BenNatan-v1.cls May 11, 2004 14:51




352 Chapter 18


need to change the struts-config.xml ¬le in the WebContent/WEB-
INF directory to handle the view mode actions.
The view mode con¬guration requires two actions, one to display the
poll and one to register a user™s vote. Two JSPs are used by the portlet in
view mode, one to render the poll question and answer choices (allowing
the user to vote) and one to render the poll results. A poll bean is also used
in this con¬guration.
We have also created a simple JSP to display information when an ex-
ception is encountered. The main view action, as well as the exception JSP,
is globally de¬ned since they could get invoked from other Actions in the
portlet. For example, when you return from edit or con¬gure mode you
want to invoke the main view action to render the view page. The follow-
ing listing shows the struts con¬guration.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ========== -->
<form-beans>
<form-bean name="pollBean"
type="com.ibm.sample.portlet.poll.beans.PollBean" />
</form-beans>
<!-- ======== Global Forward Definitions ========== -->
<global-forwards>
<forward name="mainview" path="/mainview.do" />
<forward name="exception" path="/exception.jsp" />
</global-forwards>
<!-- ======== Action Mapping Definitions ========== -->
<action-mappings>
<action path="/mainview"
type="com.ibm.sample.portlet.poll.actions.ViewAction"
name="pollBean" scope="request" validate="false">
<forward name="question" path="/viewQuestion.jsp" />
<forward name="results" path="/viewResults.jsp" />
</action>
<action path="/vote"
type="com.ibm.sample.portlet.poll.actions.VoteAction"
name="pollBean" scope="request" validate="false">
<forward name="results" path="/viewResults.jsp" />
</action>
</action-mappings>
<!--========== Controller Configuration ========== -->
<controller
processorClass="com.ibm.wps.portlets.struts.WpsRequestProcessor">
</controller>
<!-- ========= Message Resources Definitions ===== -->
<message-resources parameter="nls.polling" />
P1: FCH
WY009-18 WY009-BenNatan-v1.cls May 11, 2004 14:51




Struts Portlet Framework 353


<!-- =========== Plug Ins Configuration === ==== -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
</struts-config>



Struts Con¬guration in Portlet Edit Mode
The edit mode con¬guration is very simple. The portlet edit mode is a single
page that allows the user to select a color scheme for the poll result graph.
Therefore, your struts con¬guration will only need to contain an action to
save the user preference and the JSP to render the selection page. There is
no action needed to render the selection page since the creation of this page
does not need any prior application logic processing or bean creation. An
edit data form bean is also de¬ned for the user input form.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="editFormData"
type="com.ibm.sample.portlet.poll.beans.EditFormData"/>
</form-beans>
<action-mappings>
<action
path="/editsave"
type="com.ibm.sample.portlet.poll.actions.EditSaveAction"
name="editFormData"
scope="request"
validate="false" >
</action>
</action-mappings>
<controller
processorClass="com.ibm.wps.portlets.struts.WpsRequestProcessor">
</controller>
<message-resources parameter="nls.polling"/>
</struts-config>




Struts Con¬guration in Portlet Con¬gure Mode
The struts con¬guration for the portlet con¬gure mode is a bit more compli-
cated. Actions are needed for the creation of each of the three con¬guration
pages, to verify the datasource, to add an answer choice, to delete an answer
P1: FCH
WY009-18 WY009-BenNatan-v1.cls May 11, 2004 14:51




354 Chapter 18


choice, to save the con¬guration settings, and to cancel the con¬guration
changes. The con¬guration data form bean is also de¬ned.
The forward de¬nitions for the JSPs for the ¬rst and third con¬guration
pages are de¬ned globally since they are used from multiple actions.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean
name="configureForm"
type="com.ibm.sample.portlet.poll.beans.ConfigureFormData"/>
</form-beans>
<global-forwards>
<forward name="configure" path="/configure.do" />
<forward name="configure_pg1" path="/configure_pg1.jsp" />
<forward name="configure_pg3" path="/configure_pg3.jsp"/>
</global-forwards>
<action-mappings>
<action path="/configure"
type="com.ibm.sample.portlet.poll.actions.ConfigPg1Action"
name="configureForm" scope="request"
validate="false">
</action>
<action path="/configure2"
type="com.ibm.sample.portlet.poll.actions.ConfigPg2Action"
name="configureForm" scope="request"
validate="false">
<forward
name="configure_pg2"
path="/configure_pg2.jsp"/>
</action>
<action path="/configure3"
type="com.ibm.sample.portlet.poll.actions.ConfigPg3Action"
name="configureForm" scope="request"
validate="false" >
</action>
<action path="/configure_verify"
type="com.ibm.sample.portlet.poll.actions.ConfigVerifyDatasourceAction"
name="configureForm" scope="request"
validate="false" >
</action>
<action path="/configure_cancel"
type="com.ibm.sample.portlet.poll.actions.CancelAction"
name="configureForm" scope="request"
validate="false" >
</action>
<action path="/configure_save"
P1: FCH
WY009-18 WY009-BenNatan-v1.cls May 11, 2004 14:51




Struts Portlet Framework 355


type="com.ibm.sample.portlet.poll.actions.ConfigSaveAction"
name="configureForm" scope="request"
validate="false" >
</action>
<action path="/configure_delete"
type="com.ibm.sample.portlet.poll.actions
.ConfigDeleteChoiceAction"
name="configureForm" scope="request"
validate="false" >
</action>
<action path="/configure_add"
type="com.ibm.sample.portlet.poll.actions.ConfigAddChoiceAction"
name="configureForm" scope="request"
validate="false" >
</action>
</action-mappings>
<controller
processorClass="com.ibm.wps.portlets.struts.WpsRequestProcessor">
</controller>
<message-resources parameter="nls.polling"/>
</struts-config>




Action Classes
Your struts action classes will extend org.apache.struts.action
.Action and override the execute method. With a struts portlet imple-
mentation you no longer implement a central controller class such as the
PollPortlet class from the previous implementation. This responsibility is
managed by the struts request processor, which we have con¬gured as
WpsRequestProcessor.
You will de¬ne an abstract action class to include common functions that
should occur prior to any action getting invoked. This is the function you
had in the PollPortlet™s actionPerformed or service methods.
The AbstractAction class then implements the execute method and
performs any needed processing before dispatching to the subclass. The
listing below shows the AbstractAction class for our struts portlet. No-
tice that the execute method is passed HttpServletRequest and
HttpServletResponse objects. In order to have access to the portlet API,
you need to get the PortletRequest and PortletResponse objects
from these.
public abstract class AbstractAction extends Action
implements PollConstants {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
P1: FCH
WY009-18 WY009-BenNatan-v1.cls May 11, 2004 14:51




356 Chapter 18


try {
// Check for existing session with logged-in user.
PortletRequest portletRequest = (PortletRequest) request;
PortletSession session = portletRequest.getPortletSession(false);
User user = portletRequest.getUser();
if (session == null | | user == null) {
ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE,
portletRequest.getLocale());
String msg = bundle.getString("exception.login");
throw new PollMessageException(msg);
}

// Invoke the action subclass performAction method
return performAction(mapping, form, user, request, response);
}

catch (Exception e) {
PortletRequest portletRequest = (PortletRequest) request;
PortletSession session = portletRequest.getPortletSession();
session.setAttribute(POLL_EXCEPTION,e);
return (mapping.findForward("exception"));
}
}

public abstract ActionForward performAction(ActionMapping mapping,
ActionForm form, User user, HttpServletRequest request,
HttpServletResponse response) throws Exception;

}

<<

. 59
( 87 .)



>>