HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Developing Web Portals in Jetspeed Using JSP"
Vol. 9, Issue 6, p. 32

	



Listing 1

 <%@ taglib uri='/WEB-INF/templates/jsp/tld/template.tld' prefix='jetspeed' %>
...
<%try
{  //  rundata contains session data and data from action class
	RunData rundata = (RunData)request.getAttribute("rundata");
	final String userid = rundata.getUser().getUserName();
	String jspeid = (String) request.getAttribute("js_peid");
	String address = (String) request.getAttribute("address");
<!—action URL is provided by the taglibs of Jetspeed -->
<form name="addressFrm"  method="post" action="<jetspeed:dynamicUri/>">
	<INPUT TYPE="hidden" NAME="js_peid" VALUE="<%=jspeid%>">
	<textarea name="address"><%= address %></textarea>
	<!—Submit button name must start with "eventSubmit_"  -->
	<input type="submit" name="eventSubmit_doUpdate" value="Submit">
</form>
<% }
catch (Exception e)
{
	AppObjects.log("Error:error in address.jsp page",e);
	return;
}%>

Listing 2

 package com.indent.actions.portlets;
...

public class indentJSPPortletAction extends JspPortletAction
{

protected void buildNormalContext(Portlet portlet, RunData rundata) {
}

public void doUpdate(RunData rundata,Portlet portlet)
  {
    Hashtable args = new Hashtable();
    try
    {
      String address = rundata.getParameters().getString("address");
      Class.forName("org.gjt.mm.mysql.Driver");
      Connection con = DriverManager.getConnection
	  ("jdbc:mysql://localhost/document","root","indent");
      Statement stmt=con.createStatement();
      stmt.executeUpdate("insert into dummy (col1) VALUES ('address')");
      rundata.getRequest().setAttribute("address",address);
      stmt.close();      con.close();
    }
    catch(Exception e)
    {
      Log.error(e);
    }
  }
}

Listing 3

 <?xml version="1.0" encoding="UTF-8"?>
<registry>
    <portlet-entry name="Profile Demo App"
	hidden="false" type="ref" parent="JSP" application="false">

					  <meta-info>
            <title>Profile Demo Portlet</title>
            <description>Profile Demo Portlet</description>
        </meta-info>
        <classname>org.apache.jetspeed.portal.portlets.JspPortlet</classname>
        <parameter name="template" value="profileApp\profile.jsp"
		hidden="true" cachedOnName="true" cachedOnValue="true"/>
        <parameter name="action" value="portlets.indentJSPPortletAction"
		hidden="true" cachedOnName="true" cachedOnValue="true"/>
        <media-type ref="html"/>
        <url cachedOnURL="true"/>
        <category group="Jetspeed">demo</category>
        <category group="Jetspeed">jsp.demo</category>
    </portlet-entry>
</registry>

Listing 4

<%@ taglib uri='/WEB-INF/templates/jsp/tld/template.tld' prefix='jetspeed' %>
...
<%try
{
	RunData rundata = (RunData)request.getAttribute("rundata");
	final String userid = rundata.getUser().getUserName();
	String jspeid = (String) request.getAttribute("js_peid");
	String ckey=userid + jspeid;
	boolean refresh_v=false;
	String refresh = (String) request.getAttribute("appName");
	if (refresh!=null && refresh.equals("profileApp")) refresh_v = true;%>
   <cache:cache key="<%=ckey%>" refresh='<%= refresh_v%>' time="-1">
	<%ihds hds = (ihds)request.getAttribute("PageData");%>
   <FORM name="addressFrm"  method="post" action="<jetspeed:dynamicUri/>">
	<INPUT TYPE="hidden" NAME="js_peid" VALUE="<%=jspeid%>">
	<INPUT TYPE="hidden" NAME="appName" VALUE="profileApp">
<INPUT TYPE="hidden" NAME="aspireAction" VALUE="update">
<INPUT TYPE="hidden" NAME="aspireURL" VALUE="saveAddress">
	<table>
	<tr><td><textarea
	name="address"><%=hds.getValue("address")%></textarea>
	</td><td><input type="text" name="phone" value=
	'<%=hds.getValue("phone")%>'> </td></tr>
    	<% ihds addressLoop= (ihds)hds.getChild("addressLoop");
	if (addressLoop.isAtTheEnd()==true)AppObjects.log("Warn: No data");
	else	{
	    for(addressLoop.moveToFirst();!addressLoop.isAtTheEnd();
		addressLoop.moveToNext())  {	%>
	<tr><td><%=addressLoop.getValue("address") %>
	</td><td><%= addressLoop.getValue("phone")%></td></tr>
	 	<% } %>
	<tr><td colspan="2"><input type="submit" name="eventSubmit_doSubmit">
	</td></tr></table>
    	<% } %>
    </form>
    </cache:cache>
<% }   catch (Exception e){ AppObjects.log("Error:error in jsppage",e); }%>

Listing 5
package com.indent.actions.portlets; ... import com.ai.htmlgen.*; import com.ai.application.utils.*; import com.ai.application.interfaces.*; import com.ai.data.*; ... public class indentJSPPortletAction extends JspPortletAction { // This is the last method called by JSPPortlet protected void buildNormalContext(Portlet portlet, RunData rundata) { IDataCollection col; try { String appNameOrig = (String) portlet.getPortletConfig(). getInitParameters().get("appName"); String aspireAction = rundata.getParameters().get("aspireAction"); // Handle case when portlet page is initially loaded if (aspireAction == null) { final String userid = rundata.getUser().getUserName(); rundata.getParameters().append("userid",userid); aspireAction = (String) portlet.getPortletConfig(). getInitParameters().get("aspireAction"); rundata.getParameters().add("aspireAction",aspireAction); } String appName = rundata.getParameters().get("appName"); if(aspireAction.equals("select") && appName.equals(appNameOrig)) doSelect(portlet,rundata); } catch (Exception e) { System.out.println("In Exception: buildnormalcontext"); Log.error(e); } } // Method is called when JSP page is submitted; aspireAction may be update or select public void doSubmit(RunData rundata, Portlet portlet) { try { String aspireAction = rundata.getParameters().get("aspireAction"); if(aspireAction.equals("update")) doUpdate(portlet,rundata); } catch (Exception e) { AppObjects.log("Error:In doIndent",e); } } private void doSelect(Portlet portlet,RunData rundata) throws RequestExecutionException { Hashtable args = new Hashtable(); // get all parameters from the request, then call Aspire args = getParms(rundata); doSelectInAspire(portlet, rundata, args); } private void doSelectInAspire(Portlet portlet,RunData rundata,Hashtable args) { String aspireURL = (String) rundata.getParameters().get("aspireURL"); // Aspire returns a Hierarchical data set ihds pageData=(ihds)AppObjects.getIFactory().getObject(aspireURL,args); if(pageData==null) System.out.println("pagedata is null"); // The template JSP page is picked up from config file String jspPage = (String)AppObjects.getIConfig().getValue("request." + aspireURL + ".jspURL",null); this.setTemplate(rundata, jspPage, true); rundata.getSession().setAttribute("PageData",pageData); } private void doUpdate(Portlet portlet,RunData rundata) throws RequestExecutionException { String redirectURL=null; String urlName=null; Hashtable args = new Hashtable(); try { String aspireURL = (String) rundata.getParameters().get("aspireURL"); args = getParms(rundata); // Call Aspire to execute Update request RequestExecutorResponse r =(RequestExecutorResponse)AppObjects. getIFactory().getObject(aspireURL,args); // After update find the redirect request redirectURL = (String)AppObjects.getIConfig(). getValue("request."+urlName+".redirectURL",null); } catch(RequestExecutionException x) { redirectURL = (String)AppObjects.getIConfig(). getValue("request."+urlName+".failureRedirectURL",null); AppObjects.log("Error: Could not get data collection. Factory error",x); } // Save the redirect request and then call Aspire rundata.getParameters().add("aspireURL",redirectURL); doSelectInAspire(portlet, rundata, args); } // Method to gather all the parameters in a request and save in Hashtable // Hashtable is then passed to Aspire private Hashtable getParms(RunData rundata) { Hashtable args = new Hashtable(); ParameterParser pmp = rundata.getParameters(); Object[] keys = pmp.getKeys(); for (int i = 0; i < keys.length; i++) args.put(keys[i], pmp.getString((String)keys[i])); return args; } } Listing 6 <?xml version="1.0" encoding="UTF-8"?> <registry> <portlet-entry name="Profile Demo App" hidden="false" type="ref" parent="JSP" application="false"> <meta-info> <title>Profile Demo Portlet</title> <description>Profile Demo Portlet</description> </meta-info> <classname>org.apache.jetspeed.portal.portlets.JspPortlet</classname> <parameter name="template" value="profileApp\Aprofile.jsp" hidden="true" cachedOnName="true" cachedOnValue="true"/> <parameter name="action" value="portlets.indentJSPPortletAction" hidden="true" cachedOnName="true" cachedOnValue="true"/> <parameter name="aspireAction" value="select" hidden="true" cachedOnName="true" cachedOnValue="true"/> <parameter name="aspireURL" value="AddressURL" hidden="true" cachedOnName="true" cachedOnValue="true/> <parameter name="appName" value="profileApp" hidden="true" cachedOnName="true" cachedOnValue="true"/> ... </portlet-entry> </registry> Listing 7 #************************************************************** # Declarative Specification for Saving address and phone number #************************************************************** request.SaveAddress.className=com.ai.db.DBPreTranslateArgsMultiRequestExecutor request.SaveAddress.query_type=update request.SaveAddress.db=dprDB # Two step process, get userID and then insert request.SaveAddress.request.1=GetUserID request.SaveAddress.request.2=InsertAddress # username,phone,address are passed in through URL; userid is internal param request.GetUserID.className=com.ai.db.DBRequestExecutor2 request.GetUserID.stmt= select user_id as userid from turbine_user where \ login_name = '{username}' request.InsertAddress.className=com.ai.db.DBRequestExecutor2 request.InsertAddress.query_type=update request.InsertAddress.stmt= INSERT INTO profileT (id, userid, phone, address) \ VALUES (LAST_INSERT_ID(),{userid},'{phone}','{address}') # if insert succeeds go back to addressURL, otherwise errorURL request.SaveAddress.redirectURL=addressURL request.SaveAddress.failureRedirectURL=errorURL #************************************************************** # Retrieve data in three steps for display in JSP page #************************************************************** request.addressURL.className=com.ai.htmlgen.DBHashTableFormHandler1 request.addressURL.maindatarequest.className=com.ai.db. DBPreTranslateArgsMultiRequestExecutor request.addressURL.maindatarequest.db=dprDB request.addressURL.maindatarequest.request.1=getMaxID request.addressURL.maindatarequest.request.2=getLastAddress # step 1 gets maxid for the logged in user request.getMaxID.className=com.ai.db.DBRequestExecutor2 request.getMaxID.stmt=select max(id) as maxid from profileT p, \ turbine_user tu where tu.user_id=p.userid and tu.login_name='{userid}' # step 2 gets phone and address for record with id=maxid request.getLastAddress.className=com.ai.db.DBRequestExecutor2 request.getLastAddress.stmt=select phone,address from profileT where id={maxid} # step 3 gets all records for a user request.addressURL.addressLoop.class_request.className=com.ai. htmlgen.GenericTableHandler6 request.addressURL.addressLoop.query_request.className= com.ai.db.DBRequestExecutor2 request.addressURL.addressLoop.query_request.db=dprDB request.addressURL.addressLoop.query_request.stmt= \ SELECT p.phone, p.address from profileT p, turbine_user tu where tu.user_id=p.userid and tu.login_name='{userid}'
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.