Wednesday, September 26, 2012

All about navigational flows - JSF 2.0

Many tutorial online but most of them are JSF 1.2 and would not work as expected.
Navigational output:
Note: Please pay attention how URL changes
Faces-config.xml this file is by default when you have your project supported with JavaServer Faces 2.0.

If not you can have it by Project Properties Project Facets Check Javaserver Faces



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

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
 version="2.0">
 
 
</faces-config>


Navigation without bean Page1.xhtml Page2.xhtml without url change display
Page1.xhtml
<!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:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:head></h:head> 
<body>
    <rich:panel>
        <f:facet name="header">
        Experimenting with JSP navigation
        </f:facet>
  <h:outputText value="This is Page 1" /> 
  <br />
  <br />
  <h:form>
   <h:commandButton action="Page2" id="Button1" value="Navigate to Page2"></h:commandButton>
  </h:form>
  <br />
  <br />
  <h:form>
   <h:commandButton action="Page2?faces-redirect=true" id="Button2" value="Navigate to Page2 with updatedURL"></h:commandButton>
  </h:form>
  <br />
  <br />
  <h:form>
   <h:commandButton action="#{testbean.navigate}" id="Button3" value="Navigate using Bean"></h:commandButton>
  </h:form>
  <br />
  <br />
  <h:form>
   <h:commandButton action="#{testbean.navigate_changeurl}" id="Button4" value="Navigate using Bean and updated URL"></h:commandButton>
  </h:form>
  
 </rich:panel>
</body> 
</html>

Page2.xhtml
<!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:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:head></h:head> 
<body>
    <rich:panel>
        <f:facet name="header">
        Experimenting with JSP navigation
        </f:facet>
  <h:outputText value="This is Page 2" /> 
  
 </rich:panel>
</body> 
</html>

TestBean.java
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.*;

@ManagedBean(name="testbean")
@RequestScoped


public class TestBean {

 public String navigate()
 {
  return "Page2";
 }
 
 public String navigate_changeurl()
 {
  return "Page2?faces-redirect=true";
 }
}

Tuesday, September 25, 2012

Rich Faces 4.2.2 - Trying New Stuff

Output on Jboss server:


Ajax String Example:
Client Side Code:
<h:form id ="form1">
 <h:inputText value="#{testbean.name}">
  <a4j:ajax event="keyup" render="form2">
         </a4j:ajax>
 </h:inputText>
</h:form>
   
<h:form id = "form2">
 <h:outputText value="#{testbean.name}" />
</h:form>


Code for Backing Bean:

import javax.enterprise.context.RequestScoped;
import javax.faces.bean.*;
@ManagedBean(name="testbean")
@RequestScoped

public class TestBean {
 private String name="tosha";

 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

PickList:
Client Side Code:
<h:form>   
    <h:outputText value="Pick list Example"></h:outputText>      
            <rich:pickList sourceCaption="Available" targetCaption="Selected" listWidth="165px" listHeight="100px" >
              <!-- static list -->
    <f:selectItem itemLabel="Option 1" itemValue="1"/>
              <f:selectItem itemLabel="Option 2" itemValue="2"/>
              <f:selectItem itemLabel="Option 3" itemValue="3"/>
             <f:selectItem itemLabel="Option 4" itemValue="4"/>
              <f:selectItem itemLabel="Option 5" itemValue="5"/>
   
   <!-- Dynamic list from a backing bean -->
             <f:selectItems value="#{listSelectBean.capitals}"/>
        </rich:pickList>   
</h:form>
Code for backing Bean:
package com.trial;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
@ManagedBean(name="listSelectBean")
@ApplicationScoped

public class ListSelectBean {    
    private List capitals;
    private List selectedCapitals;
   
    public ListSelectBean() {
     capitals = new ArrayList();
     capitals.add("India");
     capitals.add("Australia");
     capitals.add("USA");
    }
 
    public List getCapitals() {
        return capitals;
    }
 
    public void setCapitals(List capitals) {
        this.capitals = capitals;
    }
 
    public List getSelectedCapitals() {
        return selectedCapitals;
    }
 
    public void setSelectedCapitals(List selectedCapitals) {
        this.selectedCapitals = selectedCapitals;
    }
}
Table Example:
Client Side Code:
<h:form>
 <rich:panel>
  <h:panelGrid columns="2">
   <a4j:commandButton value="+" action="#{bean.add}" render="list" />
   <a4j:commandButton value="-" action="#{bean.remove}" render="list" />
   </h:panelGrid>
   <rich:dataTable id="list" value="#{bean.list}" var="item"
    width="60px">
    <rich:column>
     <h:outputText value="#{item}" />
    </rich:column>
   </rich:dataTable>
  </rich:panel>
 </h:form>
 <h:form>
  <rich:panel>
   <h:panelGrid columns="2">
    <a4j:commandButton value="+" action="#{bean.add}" render="list" />
    <a4j:commandButton value="-" action="#{bean.remove}" render="list" />
   </h:panelGrid>
   <rich:dataTable id="list" value="#{bean.list}" var="item"
    width="60px">
    <rich:column>
    <h:outputText value="#{item}" />
   </rich:column>
  </rich:dataTable>
 </rich:panel>
</h:form>
Backing Bean Code:
package com.trial;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.*;

@ManagedBean(name="testbean")
@RequestScoped


public class TestBean {
 private String name="tosha";

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

Setting Hibernate Tool to Generate Entities - Part 1

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.

Hibernate is a ORM database Tables Java Classes implies Rows in a Table Java Objects

Step1:
  • Download and install hibernate Plugin from here along with many handy tools for JBoss
Step2:
  • Create a Web Dynamic Project or you can go with Plain Java Project.
Step 3:
  • Project New Other Hibernate Configuration File
  • Select your project/JavaSource
  • Fill in details about your database
  • Make sure you select Checkbox Create a console configuration
  • Finish
You should get hibernate.cfg.xml
Hibernate Configuration File Wizard
Code (SQL Server):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="">
        <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="hibernate.connection.password">****</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://servername;databaseName=YourDatabase</property>
        <property name="hibernate.connection.username">bhauser</property>
        <property name="hibernate.default_catalog">Make sure You know your Catlog for SQL Server</property>
        <property name="hibernate.default_schema">SchemaName</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    </session-factory>
</hibernate-configuration>
Step 4:
  • Project Properties Project Facets Enable JPA Apply Ok
  • It will take some time to install JPA
  • In Persistant management class Select Radio Button "Discover annotated class automatically"

Step 5:
This step is going to be setting up hibernate console. From Step 3 you already have hibernate console for your project.
  • Change Perspective to Hibernate
  • Now near you project explorer you must see Hibernate Configuration Pane.
  • Right Click on your Project configuration file and Edit



All set to generate dynamic entities! :)