Saturday, 1 March 2014

JSP + Apache Tiles Example

Hello all,
This sample application will show you how to use apache tiles using JSP in your maven web application project. so lets get started.
Follow these steps to make your project:

Application Type: Maven Web Application
Apache Tiles: 2.2.2 version
Web Server: Tomcat 7.0.50

Download Source Code: Click here

web.xml Configuration Code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_2_5.xsd"
    version="2.5">

    <context-param>
<param-name>
            org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG
        </param-name>
<param-value>
            /WEB-INF/myTilesConfigFile.xml
        </param-value>
    </context-param>
    <listener>
    <listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>jsp/home.jsp</welcome-file>
    </welcome-file-list>

</web-app>

myTilesConfigFile.xml Configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="homePage" template="/jsp/template.jsp">
        <put-attribute name="header" value="/jsp/defaultHeader.jsp" />
        <put-attribute name="menu" value="/jsp/defaultMenu.jsp" />
        <put-attribute name="body" value="/jsp/home_body.jsp" />
        <put-attribute name="footer" value="/jsp/defaultFooter.jsp" />
    </definition>

</tiles-definitions>



Template.jsp

<%-- 
    Document   : defaultFooter
    Created on : Mar 1, 2014, 1:38:55 PM
    Author     : Umair
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
    <body style="width:100%;height:100%">
        <table border="1" cellspacing="0" cellpadding="0" style="width:100%;height:100%">
            <tr>
                <td colspan="2">
                    <tiles:insertAttribute name="header" /></td>
            </tr>
            <tr>
                <td>
                    <tiles:insertAttribute name="menu" /></td>
                <td>
                    <tiles:insertAttribute name="body" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <tiles:insertAttribute name="footer" /></td>
            </tr>
        </table>
    </body>

</html>

Thursday, 16 January 2014

Set/Get Variables in URL JSF Example

Hello guys today i shall be covering that how to set and retrieve parameter in jsf application. first of all i must tell you about application tools:

Maven Project
JSF
Primefaces
Apache Tomcat

You can also configure this project in your IDE by download it. Click Here to Download
Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_2_5.xsd"
version="2.5">
<display-name>JSFApp</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- Just here so the JSF implementation can initialize, *not* used at runtime -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Just here so the JSF implementation can initialize -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

</web-app>

-------------------------------------------------

POM.xml

Dependencies:
<dependencies>
<dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.0.2-b10</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>4.0</version>
        </dependency>

  </dependencies>

--------------------------------------------------

Index.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<p:button outcome="target" value="Navigate">
<f:param name="id" value="20" />
</p:button>
</h:body>

</html>

---------------------------------------------------

Target.xhtml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<h:body>
<h:outputText value="#{param['id']}" />
</h:body>

</html>