Tuesday, August 24, 2010

Setup Prime Faces in Tomcat 6.0 or >




Prime Faces is awesome it rocks!! Components are sleek and simple. I haven't had a chance to deploy any real time applications yet but I am eagerly waiting for an opportunity to work with JSF 2.0 and Prime Faces frameworks.

Setting up prime faces is simple and the documentation for prime faces is good and has all the details but I had to do little bit of digging and it took couple of hours to setup. I will try to reduce your work.

1. Download tomcat 6 or greater.
2. Copy jsf-api-2.0.2-FCS.jar and jsf-impl-2.0.2-FCS.jar to TOMCAT_HOME\lib directory for tomcat 6 only. If you have installed tomcat 7 ignore this step. Additionally, to make JSTL-1.2 to work in tomcat6 you need to copy el-api-2.2.jar to your TOMCAT_HOME\lib folder, and the el-impl-2.2.jar to your WEB-INF/lib folder and add below context param to web.xml depending on which JSF implementation (myfaces or sun RI).


3. Download primefaces JSF 2.0 jar file primefaces-2.1-SNAPSHOT.jar
4. From MyEclipse create a new web application project. Make sure you have JDK 5 or greater. I am using JDK 1.6 version.
5. MyEclipse will create web.xml for you. Copy the below the contents to web.xml




















7. Copy all the required jar file to the project classpath and also copy them to WEB-INF\
lib directory of the web application.
8. Deploy your web app.


Update: PrimeFaces had it’s own resource servlet to load resources, now with 2.2.M1 it is gone now since primefaces migrated resource management to JSF 2.0 Resource APIs.

Tuesday, October 28, 2008

UIX Extending EL Tags

Lets extend EL tags from StringUtils.java

UIX Table with POJOS

Let see the how to use UIX table with POJO.

Monday, March 12, 2007

DWR-2.0.rc2 + Spring 2.0 integration

Steps to integrate DWR with Spring 2.0 using new Name Spaces that were introduced. The integration currently works with Spring 2.0 and DWR-2.0RC-2 versions only. This does not work with Spring 2.0.1 or 2.0.2 or 2.0.3 and DWR-2.0RC-2, in order to work with spring 2.0+ versions you need get the current version of DWR-2.0RC-2 from HEAD of the CVS.

Step 1: Download 2.0 version of Spring form http://www.springframework.org/
Step 2: Download DWR version 2.0-rc2 from http://getahead.org/dwr/download

Copy spring and dwr jar files to the webapplication classpath.

Step 3: Add the below lines into web.xml

<servlet>

<servlet-name>dwr-spring</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dwr-spring</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>dwr-spring</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

<welcome-file-list>

<!-- index page -->

<welcome-file>index.jsp</welcome-file>

<welcome-file>index.html</welcome-file>

</welcome-file-list>


Step 4: Create the default Spring MVC context file dwr-spring-servlet.xml under WEB-INF directory of the webapplication and configure the DWR

<?xml version="1.0"
encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

http://www.directwebremoting.org/schema/spring-dwr

http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">


<!-- DWR CONFIGURATION -->

<dwr:configuration>

<dwr:create
javascript="JDate" type="new"

class="java.util.Date" />
</dwr:configuration>




<dwr:controller id="dwrController" debug="true" />

<bean id="dwrHandlerMappings"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="alwaysUseFullPath"
value="true" />

<property
name="mappings">

<props>

<prop key="/dwr/**/*">dwrController</prop>

</props>

</property>

</bean>

<!-- END OF DWR CONFIGURATION -->

</beans>

Step 5: Deploy the application in Tomcat 5.5 and point the browser to http://localhost:8080/dwr-spring/dwr/index.html

If the integartion is sucessfull it should display the page with JDate link on the page.

Blow links provides more information on how to integrate DWR and Srping

http://bram.jteam.nl/index.php/2007/01/

Saturday, January 6, 2007

Tomcat 5.5 with JDK 1.4

To setup tomcat 5.5 or grater version on JDK 1.4 you need to download JDK 1.4 Compatability Package along with windows installer form http://tomcat.apache.org/. When installing the tomcat 5.5 it will by default look for JDK 5 ver, but it will still install the tomcat but when you run the tomcat it will display a message "This release of Apache Tomcat was packaged to run on J2SE 5.0 or later". To make it run under jdk 1.4 unzip the JDK 1.4 Compatability Package and copy the .jar file bin directory to the TOMCAT_HOME\bin directory and copy the .jar files under the common\endorsed to the TOMCAT_HOME\common directory. Now you should be able to run the tomcat by type in the http://localhost:8080/ in your favorite browser.

Tomcat 5.5 with JDK 1.4

To setup tomcat 5.5 or grater version on JDK 1.4 you need to download JDK 1.4 Compatability Package along with windows installer form http://tomcat.apache.org/. When installing the tomcat 5.5 it will by default look for JDK 5 ver, but it will still install the tomcat but when you run the tomcat it will display a message "This release of Apache Tomcat was packaged to run on J2SE 5.0 or later". To make it run under jdk 1.4 unzip the JDK 1.4 Compatability Package and copy the .jar file bin directory to the TOMCAT_HOME\bin directory and copy the .jar files under the common\endorsed to the TOMCAT_HOME\common directory. Now you should be able to run the tomcat by type in the http://localhost:8080/ in your favorite browser.

Tuesday, February 21, 2006

Ajax + JSP to populate dependent dropdown

This example refers to the same as Startups + Ajax by example but here I am using JPG instead of Struts. The example has only dependency to prototype.js and is more helpful if you are using just JSP. You can download the latest code of prototype.js Prototype is wonderful to have in your toolkit as it makes life easy for a JSP or JavaScript developer.

The example is about how to populate the dependent dropdown list without refreshing the entire page when one of the options is selected. The example uses 2 jsp pages, first jsp (ajaxOptions.jsp) and uses prototype.js file and which has AJAX calls to the second jsp(ajaxOptionResult.jsp) this jsp returns XML as a response.

1. ajaxOptions.jsp
Make sure you have the prototype.js file as a part of you web application and reference the path in this jsp page in my case I have the js file under js/ajax/

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>Address Form</title>
<!-- include the AJAX JS File -->
<script src="js/ajax/prototype.js" type="text/javascript"></script>
<SCRIPT LANGUAGE="JavaScript">

/*
onChange event of the dropDownList will calls this function
which has AJAX call to Struts Action class
@param: dropDownList object (this)
@param: URL or Struts Action
*/
function depedentDropDown(obj, url) {
var stateValue = $F(obj)
var pars = "state=" + stateValue;
//alert(stateValue);
var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars,
onComplete: showResponse});
}

/*
Upon completing the request the AJAX will call this method
which is responsible for loading the depedent list from the XML
*/
function showResponse(originalRequest)
{
var list = $('city');
var xmlString = originalRequest.responseXML;
var items = xmlString.getElementsByTagName('labelValueBean');
clearList(list);
if (items.length > 0)
{
for (var i = 0; i < items.length; i++)
{
var node = items[i];
var value = "";
var label = "";
if (node.getElementsByTagName("label")[0].firstChild.nodeValue) {
value = node.getElementsByTagName("label")[0].firstChild.nodeValue;
label = node.getElementsByTagName("value")[0].firstChild.nodeValue;
}
addElementToList(list, value, label);
}
}
else
{
addElementToList(list, "", "-- Select is Empty --");
}
}

/**
remove the content of te list
*/
function clearList(list)
{
while (list.length > 0)
{
list.remove(0);
}
}

/**
Add a new element to a selection list
*/
function addElementToList(list, value, label)
{
var option = document.createElement("option");
option.value = value;
var labelNode = document.createTextNode(label);
option.appendChild(labelNode);
list.appendChild(option);
}



</SCRIPT>
</head>
<body>
<form method="POST" action="">
<fieldset style="padding: 2">
<legend>Address Form</legend>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="58%">
<tr>
<td width="24%">Address</td>
<td width="76%">
<input type="text" name="address1" size="20"/>
</td>
</tr>
<tr>
<td width="24%">Apt#</td>
<td width="76%">
<input type="text" name="address2" size="20"/>
</td>
</tr>
<tr>
<td width="24%">State</td>
<td width="76%">
<select size="1" name="state" onChange="depedentDropDown(this,'ajaxOptionResult.jsp')">
<option>Select State</option>
<option value="VA">VA</option>
<option value="MD">MD</option>
</select>
</td>
</tr>
<tr>
<td width="24%">City</td>
<td width="76%">
<select size="1" name="city">
<option>Select City</option>
</select>
</td>
</tr>
<tr>
<td width="24%">Zip</td>
<td width="76%">
<input type="text" name="zip" size="20"/>
</td>
</tr>
</table>
</fieldset>
<p>
<input type="submit" value="Submit" name="B1"/>
<input type="reset" value="Reset" name="B2"/>
</p>
</form>
</body>
</html>

2. ajaxOptionResult.jsp this JSP page response is of type "text/xml"

<%

String state = request.getParameter("state");
String[] vaArray = new String[]{"Reston", "Centerville", "Chantilly", "Fairfax"};
String[] mdArray = new String[]{"Silver Spring", "Rockwill", "Bethesda", "Brandywine" };
// pouplate the state
StringBuffer buffer = new StringBuffer();
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buffer.append("\n");
buffer.append("<root>");
buffer.append("\n");
if(state.equals("VA")){
for(int i=0; i < vaArray.length; i++){
buffer.append("<labelValueBean>");
buffer.append("\n");
buffer.append("<label>" + vaArray[i] + "</label>");
buffer.append("\n");
buffer.append("<value>"+ vaArray[i]+"</value>");
buffer.append("\n");
buffer.append("</labelValueBean>");
}
}else if(state.equals("MD")){
for(int i=0; i < mdArray.length; i++){
buffer.append("<labelValueBean>");
buffer.append("\n");
buffer.append("<label>" + mdArray[i] + "</label>");
buffer.append("\n");
buffer.append("<value>"+ mdArray[i]+"</value>");
buffer.append("\n");
buffer.append("</labelValueBean>");
}
}else{
buffer.append("<labelValueBean>");
buffer.append("\n");
buffer.append("<label>Selet is empty</label>");
buffer.append("\n");
buffer.append("<value>Empty</value>");
buffer.append("\n");
buffer.append("</labelValueBean>");
}
buffer.append("\n");
buffer.append("</root>");
System.out.print(buffer.toString());
response.addHeader("Content-Type", "text/xml");
response.setContentType("text/xml; charset=windows-1252");
out.write(buffer.toString());
%>

It is as simple as it is.

PF