New Version of Eclipse Portal Pack with Action Support
I have uploaded the source code for the new version of Eclipse-Portalpack. I have been playing with the idea of making portlet development easy... and not just for rendering the view mode or the edit mode of the portlet but also in processing actions easily from the portlets. To this extent this is my first attempt in making this happen. Check it out and let me know if you find it useful and suggest any enhancements requests if any. I will be enhancing this further myself ( as there are areas I think that need it, but do let me know your suggestions )
Okay back to the new version. The new version supports the following new things :
- It is now built on the latest version of JAXB .i.e version 2.1.
- It has a new feature of adding a new portlet action very easily to your portlet, and to implement the action you just need to implement one method "execute()" in your action class and almost no code change to your portlet class is needed. You just specify what page to go to once the action is processed and the portlet has the code to automatically send you to the new page once the action is executed sucessfully. In case of an error that happens in your action's "execute()" method, the portlet will redirect you to the error page that gets created automatically for you when the portlet project was initially created.
Enhancements to this :
There are a couple of enhancements that I am still working on. If you think you can contribute to any of these please join in and contribute.
- There are enhancements to the GUI for action creation needs some work.
Suppose I want to develop a simple portlet which shows a form to collect some inputs from the user. The form might ask the user for his first name, last name and the email address. When the user submits this form, the data is collected at the server, and persisted to a data store. Once the data is persisted the user is shown a thank you page. Now assume the above needs to be done in a portlet. The scenario described above can be done using a portlet as shown below in the picture:

When developing this portlet the developer needs to write code in his processAction() method to do the following :
- Recognise the type of action this request is. In our case there is only one action so this case is simple.
- Collect the data from the form and populate a bean with that data.
- Put the bean in session data to be accessed in doView() mode.
- Now when the portlet container calls the doView() method, access the bean stored in session in previous step and do the actual action
- Dispatch the request to the destination JSP page.
All the above needs to be done by the developer whenever he writes a particular action. ( This is one way of doing this )
I wanted to make this a little easier to do, so I have added new code to the Eclipse Portal Pack project to make this easier. This is a very early version of it, so please bear with it, as I make further improvements. But feel free to try it out and give suggestions, or better still join on the project and contribute.
Developing the above portlet with the Eclipse Portal Pack Project
A screencast of creating a simple portlet project and a sample portlet is shown here.
Now that we have a sample portlet ready. Let's go about adding this new action that we wanted. Let's first create the JSP page that will show the form to the user in the view mode. By default the project would have created a JSP for the view mode called view.jsp in your /WEB-INF/jsps/ folder. Open this JSP file and add the following code to it.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>
<%@page import="javax.portlet.*" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>
<portlet:actionURL var="portletUrl" />
<form id="form1" name="form1" method="post" action="<%=portletUrl.toString()%>">
First Name : <input type="text" name="fname" type="text"><br/>
Last Name : <input type="text" name="lname" type="text"><br/>
Email : <input type="text" name="email" type="text"><br/>
<input type="submit" name="Submit">
</form>
The lines in bold indicate the form that will be displayed to the user in the view mode. It presents the user with fields to accept his first name, last name and email address and then there is the submit button which submits the form to the portlet via the portlet:actionURL variable defined above.
Now this is where the new Eclipse Plugin can help you in making your life easier. Let's call the action to collect the user data and put it to datastore as "CollectData". Right click on the portlet project in Eclipse, and down at the bottom you will see a menu entry called "Portlets->Create new Portlet Action" as shown below in the picture.

So here is what we want to do, we want to define a new action called CollectData, which we want implemented by a class of our choice, in this case lets say com.sun.portal.test.actions.CollectDataAction, and when our action is complete we want the portlet to take us to the page defined in /WEB-INF/jsps/done.jsp. So fill up the dialog box as shown below and click Ok. ( Like I said this is the first version so I haven't added any error checking as of yet, it will be coming pretty soon though ).

On clicking Ok, you will see a new action class called CollectDataAction created under the package com.sun.portal.test.actions. Open this class in Eclipse and it will appear as below:
package com.sun.portal.test.actions;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.sun.portlet.core.actions.ActionExecuteException;
import com.sun.portlet.core.actions.BaseActionFormBean;
public class CollectDataAction extends BaseActionFormBean { public CollectDataAction(ActionRequest request) {
super(request);
}@Override
public boolean execute(RenderRequest portletRequest,
RenderResponse portletResponse) throws ActionExecuteException {
// Put your logic of executing the action for this request here.
return true;
}
So as you can see, all you need to do is implement the method "execute()" in your CollectDataAction.java class. In the execute() method, you can access the form parameters that your view.jsp sent in as getParameter(String), getParameterNames() and getParameterValues(String) as usual.
Also to invoke this action the only other thing you will need to do is, add a hidden field in your view.jsp form called ACTION_NAME, and its value must exactly be the action you want executed when the form is submitted, which in our case is "CollectData". So our view.jsp page in completion will look as below :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>
<%@page import="javax.portlet.*" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>
<portlet:actionURL var="portletUrl" />
<form id="form1" name="form1" method="post" action="<%=portletUrl.toString()%>">
First Name : <input type="text" name="fname" type="text"><br/>
Last Name : <input type="text" name="lname" type="text"><br/>
Email : <input type="text" name="email" type="text"><br/>
<input type="hidden" name="ACTION_NAME" value="CollectData">
<input type="submit" name="Submit">
</form>
The only changed line is shown in Bold and Italic.
That's all that is needed to implement this action from your side. Ofcourse, you need to write the done.jsp file to which the portlet will dispatch the view after the action is executed.
To summarise, to implement any new action, all you need to do in addition to writing the source JSP and the target JSP is :
- A hidden variable called ACTION_NAME in the source JSP page form.
- Implement the execute() method in your Action Bean.
Rest of the stuff is handled for you!!
Let me know if you like it, by posting your comments.
Posted by insidemyhead [Sun] ( July 03, 2007 04:03 PM ) Permalink | Comments[46]
Possible answer to the 123 black hole
Here is a explanation of the mathemagical black hole (123) in my opinion. If we consider the transformation on x as a function f(x), such that f(x) = E(x) O(x) T(x)
where E(x) = Number of even digits in x, O(x) = Number of odd digits in x and T(x) = Number of digits in x
Then for all x,
f(x) < x
So eventually as we recurse this process, at some point f(x) will fall below 1000. In that case the only possibilities for E(x) O(x) and T(x) are
1 2 3
2 1 3
3 0 3
Each of the above in the next iteration will give 123. Hence the proof.
Posted by insidemyhead [Personal] ( July 03, 2007 02:39 PM ) Permalink | Comments[0]

