mardi 25 juin 2013

PrimeFaces 3 : Introduction & form validation

PrimeFaces is a component library for JSF and has huge component support.
Working with PrimeFaces is very much easy because there is a single jar, no mandatory other dependencies, no mandatory configuration is required.

Ok, in this tutorial I am going to explain how to create a Maven based PrimeFaces project and create a simple registration for and validate the form.
I am using JDK1.6.26 and Tomcat 7.0.32.


Step#1
Create Maven Project and add the following repositories, dependencies in pom.xml


<dependencies>  
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-api</artifactId>
   <version>2.1.6</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-impl</artifactId>
   <version>2.1.6</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.primefaces</groupId>
   <artifactId>primefaces</artifactId>
   <version>3.0</version>
  </dependency>
  <dependency>
   <groupId>org.primefaces.themes</groupId>
   <artifactId>bluesky</artifactId>
   <version>1.0.2</version>
  </dependency>
 </dependencies>

 <repositories>
  <repository>
   <id>maven2-repository.dev.java.net</id>
   <name>Java.net Repository for Maven</name>
   <url>http://download.java.net/maven/2</url>
  </repository>
  <repository>
   <id>prime-repo</id>
   <name>Prime Repo</name>
   <url>http://repository.primefaces.org</url>
  </repository>
 </repositories>
 <build>
  <finalName>primefaces-demo</finalName>
 </build>
 Step#2

Configure JSF2's FacesServlet configurtion in web.xml
<servlet>
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <!--Blusky theme for PrimeFaces -->
  <context-param>
 <param-name>primefaces.THEME</param-name>
 <param-value>bluesky</param-value>
  </context-param>
Step#3

Create RegistrationForm bean.
package com.rsuna.pfdemo.mb.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RegistrationForm
{
 private Integer userId;
 private String userName;
 private String password;
 private String firstName;
 private String lastName;
 private String email;
 private String phone;
 private Date dob;
 private String gender;
 private List<String> interests = new ArrayList<String>();
 private boolean subscribeToNewsLetter;
 
 //setters/getters
  
}
Create RegistrationBean which is a Managed Bean.
package com.rsuna.pfdemo.mb;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.rsuna.pfdemo.mb.ui.RegistrationForm;

@ManagedBean
@RequestScoped
public class RegistrationBean
{
 private RegistrationForm registrationForm = null;
 private List<String> interests = null;
 private List<String> genders = null;
 
 public RegistrationBean()
 {
  this.interests = new ArrayList<String>();
  this.interests.add("Sports");
  this.interests.add("Gadgets");
  this.interests.add("Politics");
  this.interests.add("Technology");
  
  this.genders = new ArrayList<String>();
  this.genders.add("Male");
  this.genders.add("Female");
 
 }
 
 public String register()
 {
  System.out.println("register.....");
  //store data in DB
  System.out.println(this.registrationForm);
  return "welcome";//go to welcome.xhtml
 }
 
 public RegistrationForm getRegistrationForm()
 {
  if(this.registrationForm == null){
   this.registrationForm = new RegistrationForm();
  }
  return registrationForm;
 }

 public void setRegistrationForm(RegistrationForm registrationForm)
 {
  this.registrationForm = registrationForm;
 }

 public List<String> getInterests()
 {
  return interests;
 }

 public void setInterests(List<String> interests)
 {
  this.interests = interests;
 }

 public List<String> getGenders()
 {
  return genders;
 }

 public void setGenders(List<String> genders)
 {
  this.genders = genders;
 }
  
}
Step#4: Create registration.xhtml JSF page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"> 

<h:head>

</h:head> 
<h:body> 
 <h2>Registration Form</h2>
 <h:form>
  <p:fieldset legend="Registration Form" widgetVar="regWidget" style="width: 600px;">
   <h:panelGrid columns="3" width="550" border="0">
    <h:outputLabel value="UserName" />
    <p:inputText value="#{registrationBean.registrationForm.userName}" 
       id="userName"
        required="true" 
        requiredMessage="UserName is required"
        validatorMessage="UsesrName should be of length from 5 to 15 chars"
        >
     <f:validateLength minimum="5" maximum="15" for="userName"></f:validateLength>
    </p:inputText>
    <p:message for="userName"/>
    
    
    <h:outputLabel value="Password" />
    <p:password value="#{registrationBean.registrationForm.password}"
       id="password"
       required="true" 
       requiredMessage="Password is required"
       validatorMessage="Password should be of length from 5 to 15 chars"
       >
      <f:validateLength minimum="5" maximum="15" for="password"></f:validateLength>
    </p:password>
    <p:message for="password" />
    
    <h:outputLabel value="FirstName" />
    <p:inputText value="#{registrationBean.registrationForm.firstName}"
       id="firstName"
       required="true" 
       requiredMessage="FirstName is required"
       validatorMessage="FirstName should be of length from 5 to 15 chars"
       >
      <f:validateLength minimum="5" maximum="15" for="firstName"></f:validateLength>
    </p:inputText>
    <p:message for="firstName" />
    
    
    <h:outputLabel value="LastName" />
    <p:inputText value="#{registrationBean.registrationForm.lastName}"
       id="lastName"></p:inputText>
    <p:message for="lastName" />
    
    <h:outputLabel value="Email" />
    <p:inputText value="#{registrationBean.registrationForm.email}"
       id="email"
       validatorMessage="Invalid Email">
     <f:validateRegex pattern="[a-zA-Z0-9]+@[a-zA-Z]+.[a-zA-Z]{2,3}"></f:validateRegex>   
    </p:inputText>
    <p:message for="email" />
    
    <h:outputLabel value="Phone" />
    <p:inputText value="#{registrationBean.registrationForm.phone}"
       id="phone"></p:inputText>
    <p:message for="phone" />
    
    <h:outputLabel value="DOB" />
    <p:calendar value="#{registrationBean.registrationForm.dob}"
       id="dob"
       converterMessage="Invalid Date"
       pattern="dd-MM-yyyy">
     
    </p:calendar>
    <p:message for="dob" />
    
    <h:outputLabel value="Gender" />
    <h:selectOneRadio id="gender" 
          value="#{registrationBean.registrationForm.gender}" >
     <f:selectItems value="#{registrationBean.genders}" 
         var="gOp"
         itemLabel="#{gOp}"
         itemValue="#{gOp}"/>
    </h:selectOneRadio>
    <p:message for="gender" />
    
    
    <h:outputLabel value="Interests" />
    <p:selectManyCheckbox id="interests"
           value="#{registrationBean.registrationForm.interests}"
           layout="pageDirection">
     <f:selectItems value="#{registrationBean.interests}" var="intrOp"></f:selectItems>
    </p:selectManyCheckbox>
    <p:message for="interests" />
    
    <p:commandButton value="Register" action="#{registrationBean.register}" ajax="false"></p:commandButton>
   </h:panelGrid>
  </p:fieldset>
 
 </h:form>
</h:body>  
</html>
Step#5:
Now go to http://localhost:8080/primfaces-demo/registration.xhtml

Here key things to note are:
1. We need to add primefaces taglib using xmlns:p="http://primefaces.org/ui"
2. PrimeFaces Command Button/Links bydefault use Ajax submit. So to do non-ajax submit we should use ajax="false".
3. To enable autocompletion for <p:> tags, right click on project --> Properties --> ProjectFacests --> Select Java Server Faces 2.0 checkbox and Apply.

Src -> sivalabs.in

lundi 24 juin 2013

How to configure the WebLogic resource types


1_ From the Access System Console, click the Access System Configuration tab, then click Common Information Configuration, Resource Type Definitions.
The List All Resource Types page appears.

2_ From the List All Resource Types page, click Add.
The Define a new Resource Type page appears.

3_ Define and save the first resource type:
Name: wl_url
Display name: wl_url
Resource matching: case insensitive
Resource operation: GET
Resource operation (second): POST

4_ Define and save the second resource type:
Name: wl_svr
Display name: wl_svr
Resource matching: case insensitive
Resource operation: BOOT
Resource operation (second): DEFAULT

5_ Define and save the third resource type:
Third resource:
Name: wl_adm
Display name: wl_adm
Resource matching: case insensitive
Resource operation: DEFAULT

6_ Define and save the fourth resource type:
Name: wl_ejb
Display name: wl_ejb
Resource matching: case insensitive
Resource operation: EXECUTE

7_ Define and save the fifth resource type:
Name: wl_authen
Display name: wl_authen
Resource matching: case insensitive
Resource operation: LOGIN

8_ Define and save the sixth resource type:
Name: http
Display name: http
Resource matching: case insensitive
Resource operation: GET
Resource operation: POST
Resource operation: PUT
Resource operation: HEAD
Resource operation: DELETE
Resource operation: TRACE
Resource operation: OPTIONS
Resource operation: CONNECT
Resource operation: OTHER

Installing Liferay on Oracle WebLogic 10.3

Liferay Home is one folder above the domain to which you will be installing Liferay.
For example, if your domain location is /Oracle/Middleware/user_projects/domains/base_domain, then your Liferay Home is /Oracle/Middleware/user_projects/domains.
For this section, we will use $WEBLOGIC_HOME to refer to your WebLogic server’s installation /Oracle/Middleware.
Before you begin, make sure you have downloaded the latest Liferay .war file and Liferay Portal dependencies fromhttp://www.liferay.com/downloads/liferay-portal/additional-files. The Liferay .war file should be called liferay-portal-6.1.x-<date>.war and the dependencies file should be called liferay-portal-dependencies-6.1.x-<date>.zip.
These instructions assume you have already configured a domain and server and that you have access to the WebLogic console.
If you still have the mainWebApp module installed, remove it first.
Let’s get started by installing the JAR files Liferay needs.

Dependency Jars

Liferay needs the JAR files contained in the Liferay Dependencies Archive and the driver JAR file applicable for your database.
  1. Navigate to the folder that corresponds to the domain to which you will be installing Liferay. Inside this folder is a libfolder. Unzip the Liferay Dependencies Archive to this folder so the dependency .jar files reside in the lib folder.
  2. If WebLogic does not already have access to the JDBC driver for your database, copy the driver to your domain’s libfolder as well.
So far so good. Your JAR files are in place and ready for Liferay.
Start Oracle WebLogic if you want to configure your database and/or mail session within Oracle WebLogic.

Database Configuration

If you want WebLogic to manage your data source, use the following procedure. If you want to use Liferay’s built-in data source, you can skip this section.
02-weblogic-10-3-data-sources.png
Figure 14.44: WebLogic Data Sources
  1. Select Services → Data Sources. Click New → Generic Data Source.
  2. Give your data source a name, such as Liferay Data Source. The JNDI name should be jdbc/LiferayPool.
  3. Choose the type of database and click Next. From the screenshot, you can see we have chosen MySQL. The database driver class is selected automatically.
02-creating-a-data-source-in-weblogic.png
Figure 14.45: Creating a data source in WebLogic 10.3
  1. Click Next three times. You should be on the Connection Properties screen. Enter the database name, the host name, the port, the database user name and the password. WebLogic uses this information to construct the appropriate JDBC URL to connect to your database. Click Next.
  2. WebLogic next confirms the information you provided. For MySQL, some additional parameters must be added to the URL. Modify the JDBC URL so it has the proper parameters. Provide your database name, host name, user name and password as the values. Click Next.
  3. Click Test Configuration to make sure WebLogic can connect to your database successfully. When it does, clickFinish.
  4. You will be back to the list of data sources. Notice your new data source has no value in the Target column. Click on your data source to edit it.
  5. Click the Targets tab and check off the server instance(s) to which you wish to deploy your data source. Then clickSave.
Next, let’s configure a mail session in WebLogic.

Mail Configuration

If you want WebLogic to manage your mail sessions, use the following procedure. If you want to use Liferay’s built-in mail sessions, you can skip this section.
02-weblogic-10-3-mail-sessions.png
Figure 14.46: WebLogic Mail Sessions
  1. Select Mail Sessions and create a new mail session which points to your mail server.
  2. Give it the name Liferay Mail and give it the JNDI name of mail/MailSession and click Next.
  3. Choose your server and then click Finish.
Now let’s make sure Liferay can access this mail session.

Domain Configuration - Continued

Let’s revisit domain configuration to make sure we’ll be able to access your data source and mail session from Liferay Portal.
  1. Create a portal-ext.properties file in the Liferay Home folder, which is one folder up from your domain’s home folder.
If you are using WebLogic to manage your data source, add the following to your portal-ext.properties file in Liferay Home to refer to your data source:
    jdbc.default.jndi.name=jdbc/LiferayPool
If you are using Liferay Portal to manage your data source, follow the instructions in the Deploy Liferay section for using the setup wizard.
If want to use Liferay Portal to manage your mail session, you can configure the mail session in the Control Panel. After starting your portal as described in the Deploy Liferay section, go to Control Panel → Server Administration → Mail and enter the settings for your mail session.
If you are using WebLogic to manage your mail session, add the following to your portal-ext.properties file to reference that mail session:
    mail.session.jndi.name=mail/MailSession
  1. Lastly, you must provide WebLogic a reference to Java Server Faces (JSF) to use that library. Insert the following deployment descriptor within the <weblogic-web-app> element of WEB-INF/weblogic.xml found in your Liferay Portal .war :
    <library-ref>
        <library-name>jsf</library-name>
        <specification-version>1.2</specification-version>
        <implementation-version>1.2</implementation-version>
        <exact-match>false</exact-match>
    </library-ref>
    
Now its the moment you’ve been waiting for: Liferay deployment!

Deploy Liferay

This section provides instructions for deploying Liferay to your application server domain.
Before you deploy Liferay Portal, consider whether you want to use the setup wizard.
  • Start the setup wizard along with Liferay Portal - Do this if you want to configure your portal, set up your site’s administrative account and/or manage your database within Liferay.
If this is your first time starting Liferay Portal 6.1, the setup wizard is invoked on server startup. If you want to re-run the wizard, specify setup.wizard.enabled=true in your properties file (e.g. portal-setup-wizard.properties).
setup.wizard.enabled=true
The setup wizard is invoked during server startup.
  • Start Liferay Portal without invoking the setup wizard - Do this if want to preserve your current portal settings.
To start the server without triggering the setup wizard, specify setup.wizard.enabled=false in your properties (e.g.portal-setup-wizard.properties or portal-ext.properties file).
    setup.wizard.enabled=false
The portal-setup-wizard.properties file the setup wizard creates has setup.wizard.enabled=false conveniently specified for you.


Property values in portal-setup-wizard.properties override property values in portal-ext.properties.

Now, let’s deploy Liferay Portal.
  1. Start WebLogic.
  2. Select Deployments and click the Install button. Upload jsf-1.2.war from WebLogic’s common files directory and select Install this deployment as a library.
02-weblogic-10-3-deployments.png
Figure 14.47: WebLogic Deployments
  1. After installing the JSF libraries, go back to deployments and select the Liferay .war file from the file system or click the Upload Your File(s) link to upload it and then click Next.
  2. Select Install this deployment as an application and click Next.
  3. If the default name is appropriate for your installation, keep it. Otherwise, give it a name of your choosing and clickNext.
  4. Click Finish. After the deployment finishes, click Save.
    Liferay launches in one of the following manners:
  • If the setup wizard was disabled, your site’s home page opens in your browser at http://localhost:7001.
  • Otherwise, the setup wizard opens in your browser.
Please see the section above for how to use the setup wizard.


Note: After Liferay is installed, you may see an error initializing the Web Proxy portlet. Because the XSL parser configured by default within WebLogic cannot compile a style sheet in this portlet, Liferay disables it by default. To re-enable this portlet, extract xalan.jar and serializer.jar from the Liferay .war archive and copy them to your JDK’s endorsed folder for libraries. If you are using JRockit, this folder may be [$WEBLOGIC_HOME]/jrockit_160_05/jre/lib/ext; if your are using Sun JDK, this folder may be [$WEBLOGIC_HOME]/jdk160_24/jre/lib/ext.

Congratulations! You are now running Liferay on Oracle WebLogic.

Source : Liferay Portal 6.1 - User Guide

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More