java4all@1986 java. Powered by Blogger.

Tiles Simple Program?

>> Tuesday, June 21, 2011


Simple Struts program Using Tiles

Welcome to Struts Tiles Component.Tiles is a templating system.It can be used to create a common look and feel for a webapplication.This can be used to  reusable view components.

The main features of Tiles:                                                               

    Inheritance mechanism on definitions
    Common layouts with overriding mechanism
    Dynamic page reload based on parameter layout
    We can load different tiles according to LocaleInternationalization(i18).
    It is possible to load different Tiles according to a key.

The additional elements to develop Tiles application

    layout.jsp
    tiles-defs.xml


These two elements we have to add our struts application.
step1. Develop your jsp pages to create layout to your application.

here i am using header,body,footer this is layout.If you perform some action in body part the page will be replaced with bodyone.jsp page.It means body.jsp is going to overriden by bodyone.jsp based on struts configurations.

step2. Create a layout by using the three pages.

step3. If you are using html,tiles tld files in your jsp pages dont forget to configure in web.xml as taglib.Configure the tld files in WEB-INF folder.
<jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
 </jsp-config>

step4. Develop web.xml with all tags.(best to copy and paste in you application).

step5. Develop tiles-defs.xml with definitions.

Here the mappings can be done to perticular attribute name given in jsps and the path of perticular jsp pages.The definition name and parent definition name which is extending to antoher definition.
<definition name="layout" path="/layout.jsp"> 
         <put name="header" value="/header.jsp">
         <put name="body" value="/body.jsp">
         <put name="footer" value="/footer.jsp">   
 </put></put></put></definition>
 
 <definition extends="layout" name="bodypart"> 
         <put name="body" value="/bodyone.jsp">   
 </put></definition>
step6. Configure tiles plugin in struts-config.xml with action path and parameter to forward in the layout.
<plug-in classname="org.apache.struts.tiles.TilesPlugin">
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml">      
        <set-property property="moduleAware" value="true">
    </set-property></set-property></plug-in>
Based on the path given in addressbar the action will be done in struts-config.xml.
<action-mappings>
        <action parameter="layout" path="/tiles" type="org.apache.struts.actions.ForwardAction">
        
        <action parameter="bodypart" path="/bodypart" type="org.apache.struts.actions.ForwardAction">
    </action></action></action-mappings>
Start your Server(Tomcat) and run your application by giving this url in addressbar
http://localhost:8080/TilesDemo/tiles.do
The Given programs for this application:
                  1).header.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <center>Header to my Site</center>
</body>
</html>
2.)body.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 
<center><h3>Body to my Site
</br></h3><html:link action="/bodypart">Click</html:link> </center>
</body>
</html>
3.)footer.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <center>Footer to my Site
 
  <a href="http://javabynataraj.blogspot.com">http://javabynataraj.blogspot.com</a></center>
</body>
</html>
4.)bodyone.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <center><h3><font color="red">Body One to my Site</font></h3></center>
</body>
</html>
6.)layout.jsp
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
 
<table border=2 width="50%">
<tr>
 <td bgcolor="pink"><tiles:insert attribute="header" /></td>
</tr>
 
<tr>
<td><tiles:insert attribute="body" /></td>
</tr>
 
<tr>
 <td bgcolor="pink"><tiles:insert attribute="footer" /></td>
</tr>
 
</table>
7.)tiles-defs.xml
<?xml version="1.0" encoding="UTF-8" ?>
 
 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
        
<tiles-definitions>
 <definition name="layout" path="/layout.jsp">
         <put name="header" value="/header.jsp" />
         <put name="body"   value="/body.jsp" />
         <put name="footer" value="/footer.jsp" />  
 </definition>
  
 <definition name="bodypart" extends="layout">
         <put name="body"   value="/bodyone.jsp" />  
 </definition>
</tiles-definitions>
8.)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
  
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <session-config>
  <session-timeout>30</session-timeout>
 </session-config>
 
 <welcome-file-list>
  <welcome-file></welcome-file>
 </welcome-file-list>
 
 <jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>
<tiles-definitions>
 <definition name="layout" path="/layout.jsp">
         <put name="header" value="/header.jsp" />
         <put name="body"   value="/body.jsp" />
         <put name="footer" value="/footer.jsp"/>
</defination>
9.)struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
 
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
 
<struts-config>
 
    <form-beans></form-beans>
     
    <global-exceptions></global-exceptions>
 
    <global-forwards></global-forwards>
 
    <action-mappings>
        <action path="/tiles" parameter="layout" type="org.apache.struts.actions.ForwardAction"/>
         
        <action path="/bodypart" parameter="bodypart" type="org.apache.struts.actions.ForwardAction"/>
    </action-mappings>
     
    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
     
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />     
        <set-property property="moduleAware" value="true" />
    </plug-in>
     
</struts-config>
After running the application the output looks like below screen
  By clicking the hyperlink the next screen looks like below

                      

Read more...

Exception Handling In java?

Exception Handling:   Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program.

In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes.

   
import java.io.*;

public class exceptionHandle{
  public static void main(String[] args) throws Exception{
  try{
  int a,b;
  BufferedReader in = 
  new BufferedReader(new InputStreamReader(System.in));
  a = Integer.parseInt(in.readLine());
  b = Integer.parseInt(in.readLine());
  }
  catch(NumberFormatException ex){
  System.out.println(ex.getMessage() 
  + " is not a numeric value.");
  System.exit(0);
  }
  }
}
The output will as follow
1).If we enter are trying to write a value other than integer it throws error
I.e Entering a as value it throws,For input string: "a" is not a numeric value.

Read more...

What is meant by Project Architecture?

Project Architecture :
                         
     Project architecture means nothing but ,the when the client sends the request to the server and server generates the response.How the functionality works is nothing but project Flow.

Here in above image we are using 3-Tier architecture.

Project Architecture means like take the below example.

There are like 2-Tier,3-Tier and so on n-Tier architecture.

So generally for any project we are using 3-Tier Architecture 1.Client(Browser),
2.Server (Tomcat) 3.Back end Database(Oracle).

So explain like Requests will be taken from the Client and
it will be process in Tomcat server the server will contact
the database and process the request.The request will be get
back to the service method and inturn it will send to the
client nothing but browser.

so in this way you have to tell about your project.
 

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP