Tuesday, December 10, 2013

Broken Image handling in jsp/html

Instead of showing a blank image, the best thing is to hide the img element itself from html page. 
This can be done using following script:-

<script>
// Hide img tag
$("img").error(function(){ $(this).hide(); });
</script>

In case you want to show a blank image jpg, this can be achieved using following script:

<script>
// Replace missing image $('img').error(function(){ $(this).attr('src', 'missing.jpg'); });
</script>






Wednesday, December 4, 2013

Chrome's Developer Tool - Handy when debugging your web application for CSS issues.

Wanted to share the benefits of using Chrome's developer tool. I know most of you would be using it for lot of debugging and profiling. We can also use Chrome to see all our incorrectly defined CSS properties on our website. Just go to the following icon on your Chrome and select Developers Tools under Tools.


Once you open your developers tool console, it appears at the bottom of your browser.

Now you can view all your invalid CSS on a web page when you open that webpage on the browser and view "Resource" section of the developers Tool. This will list all invalid CSS properties defined on the page and you can go to your code and remove / Change them.


Monday, November 25, 2013

Struts 2 and Apache Tiles Integration

This is an extension of a series of blogs I have written on Struts 2. Feel free to go through them on this blog site to a better understanding of how to create a web application and use Struts 2.

In this blog, I will be introducing Apache Tiles integration with Struts 2.

Tiles is a templating system. It can be used to create a common look and feel for a web application. Tiles can also be used to create reusable view components.


Benefits of Tiles are:-

  • Screen definitions - Create a screen by assembling Tiles : header, footer, menu, body, etc.
  • Layouts - Define common page layouts and reuse them across your website.
  • Dynamic page building - Tiles can be gathered dynamically during page reload. It is possible to change any attribute: layout, list of Tiles in portal, list of menu items, etc.
  • Reuse of Tiles / Components - If well defined, a Tile can be reused across multiple applications.
  • Multi-channels - It is possible to load different Tiles according to a key.


Configuring Tiles:-

Configuring pom.xml

Following tiles artifacts need to be added.
I am using ${tiles.version} = 2.2.2


<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-compat</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${tiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>${tiles.version}</version>

</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>${tiles.version}</version>

</dependency>

      <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-tiles-plugin</artifactId>
<version>2.2.1</version>

</dependency>


Configuring web.xml to use Tiles:-

Once we have added all the Tiles dependencies in pom.xml, we need to add Tiles in our web.xml

Add following entries with you strtus2 entry. Your web.xml should look like this:-



<context-param>
   <param-name>
      org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
   </param-name>
   <param-value>
      /WEB-INF/tiles.xml
   </param-value>
   </context-param>

   <listener>
   <listener-class>
      org.apache.struts2.tiles.StrutsTilesListener
   </listener-class>
   </listener>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>

  </filter-mapping>




Creating Tiles template and different jsps that will be included as tiles.

My tiles template would look like the image below. It will have a header jsp that contains the corporation name and logo. Body jsp that contains the dynamic content and a footer jsp that contains copyright information.
baseLayout.jsp

This is the tiles jsp that will hold the other jsps and act like a template page.

You see how I am using divs and css to create a table. It is always advisable to use div tables rather than normal html tables.


<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> Struts2 Tiles sample</title>
 <style type="text/css">
    .divInner1{

        text-align:center;
        border: .1px solid;
        float:left;

    }
div.page {
width: 11in;
height: 8.5in;
padding: .5cm auto;
margin: .1cm auto;
border: 1px #D3D3D3 solid;
border-radius: 5px;
background: white;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
    </style>
 </head>

 <body>
 <div class="page">
<div style="display: table; ">
<div style="display: table-row; ">
<div class='divInner1' style="width: 11in; height: 1.5in;" >
<tiles:insertAttribute name="header" />
</div>
</div>
</div>
<div style="display: table;  float:left;">
<div style="display: table-row;">
<div class='divInner1' style="width: 11in; height: 4.5in;" >
<tiles:insertAttribute name="body" />
</div>
</div>
</div>
<div style="display: table; width: 7in; height: 6.5in; float:left;">
<div style="display: table-row;">
<div class='divInner1' style="width: 11in; height: .5in;">
<tiles:insertAttribute name="footer" />
</div>   
</div>
</div>
 </body>

</html>



header.jsp

Following is the code for header.jsp marked as "header" in tiles tag in baseLayout.jsp page above.

<div class="header">

<p>This is the header</p>

</div>

<style>

div.header

{

padding:2px 3px; 

background:white;

}

p {font-size:30px;font-family:Arial;font-weight:bolder;color:blue;}

</style>

footer.jsp

Following is the html code for footer.

<div class="footer">

<p>This is the footer</p>
</div>

<style>

div.footer

{

padding:2px 3px; 

background:white;

}

p {font-size:30px;font-family:Arial;font-weight:bolder;color:blue;}

</style>


body.jsp

This is the html code for body page.

<div class="body">
<p>This is the body area</p>
</div>
<style>
div.body
{
padding:2px 3px; 
background:white;
         }
p {font-size:30px;font-family:Arial;font-weight:bolder;color:blue;}
</style>


Configuring Tiles configuration file - tiles.xml

Now a tiles.xml file has to be created and kept along with web.xml in WEB-INF. This file tells about the configuration of tiles and corresponding jsp pages.

<?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="baseLayout" template="/myapp/baseLayout.jsp">
      <put-attribute name="title"  value="Tiles Page"/>
      <put-attribute name="header" value="/myapp/header.jsp"/>
      <put-attribute name="body" value="/myapp/body.jsp"/>
      <put-attribute name="footer"   value="/myapp/footer.jsp"/>
   </definition>

   <definition name="success" extends="baseLayout">
   </definition>
  
</tiles-definitions>


Configuring struts.xml

Once you have created the baseLayout page, other tiles pages and configured the tiles.xml, now you will proceed to configuring the struts.xml so that everything could work.



<package name="sampleapp" namespace="/" extends="struts-default,json-default">
<result-types>
                    <result-type name="tiles"  class="org.apache.struts2.views.tiles.TilesResult" />

              </result-types>
                  ....
                  ....
                  <action name="/sampleapp/*" method="execute"  class="com.myorg.action.SampleAppAction">
<result name="success" type="tiles">success</result>
                   ....
                   ....
                   </action>
           </package>
                 


As you see in the code above, we define tiles with class TilesResult and then in our action, we direct result that are "success" to type="tiles".

This completes the sample implementation of tiles. 

Tiles framework is much more flexible and provided with much more feature, which you can try out after this sample implementation works.






Friday, November 22, 2013

Struts 2 -Repopulating a page with updated data after form submit

This post is an extension of my previous post( Creating sample Struts 2 Web Application ) that talks about creating Struts 2 web application. In this post I will talk about a much smaller and simpler trick to reload the submitted page with some updated content. I have seen on lots of forums, this question being asked that how do we show the same submitted page with new content.

 For that you need following entry as your struts.xml entry:-
<action name="loginaction" method="execute"  class="com.myorg.LoginAction">
<result name="input" >/sampleapp/Login.jsp</result>
<result name="success">/sampleapp/Login.jsp</result>
</action>


Now the entry in your Login.jsp page is, 


    <body>
              <s:form action="loginaction" method="post" > 
              <label for="name"><p>Enter your Name</p></label>
              <input type="text" size="50" name="id"/>
             <s:submit value="Submit" />
            </s:form>
      <s:if test="%{id != null}">
         <br/><p>Hello 
         <s:property value="id"/></h3>
     </s:if>

</body>


Your action class looks like this:-


public String execute() throws Exception {

if(null != id)
this.id = "Dear "+id;

        return SUCCESS;

    }


Now when you submit your page, and you have entered a name, it will be go to the action, and then repopulated on the refreshed Login page with text "Hello Dear <yourname>".


Monday, September 23, 2013

Using Arquillian with JUnit and Glassfish for Integration Testing

We are all familiar with Junit as a unit and integration testing framework. This article talks about configuration of JUnit with Arquillian for Integration testing on Glassfish 4.
Arquillian (http://arquillian.org/) provides a component model for integration tests, which includes dependency injection and container life cycle management. Arquillian lets you ditch the mocks and write real tests. That's because Arquillian brings your test to the runtime, giving you access to container resources, meaningful feedback and insight about how the code really works.

Following article will present a test project, where we have a service endpoint resource class (RESTResource.java) which is a service endpoint for a RESTful webservice. The RESTful request comes to this endpoint class and it calls the business service (ServiceImpl.java). We using Java EE 7, CDI (context & dependency injection - JSR 299 specification) to inject ServiceImpl in RESTResource. Now we use also have a OraServiceDAO that does a resource lookup (using Java EE @Resource) of Oracle Data source. This OraServiceDAO is also injected into ServiceImpl using CDI. This kind of 
The problem most developers will have while writing integration tests would be, how to simulate CDI and JNDI resource look up as done in OraServiceDAO, while doing integration testing without an actual Glassfish 4 environment running. Arquillian come to our rescue and along with JUnit it provides an embedded Glassfish environment for running integration test cases.

First I would like to introduce our project structure. 

RESTResource is the RESTful web service endpoint that receives the GET request.


ServiceImpl is injected into it using CDI. using Arquillian and JUnit we would write an integration test case that would test this business service namely testService API.

ServiceImpl class

If you see, the Oracle DAO OraServiceDAO is injected into this business service class.


OraServiceDAO  class



This class looks up JNDI resource to get the Data source.

Now if you see the complexity of this simple example, framework like JUnit would not be sufficient alone to test CDI and resource look ups done in this example without external help. Arquillian comes to rescue..!!  

Using Arquillian and JUnit we can simulate the Glassfish environment and run test cases.

First I would introduce with the Maven environment setting defined in pom.xml for running Arquillian, JUnit and embedded Glassfish using Maven.

Following versions of dependencies are being used in pom.xml
<properties>
        <arquillian.version>1.0.0.Final</arquillian.version>
        <jersey.version>2.0</jersey.version>
        <junit.version>4.11</junit.version>
        <glassfish.embedded.all.version>4.0</glassfish.embedded.all.version>
        <arquillian.glassfish.embedded.version>1.0.0.CR4</arquillian.glassfish.embedded.version>
   </properties>

I’m using the arquillian-bom (Bill Of Material) who contains all the versions for the arquillian dependencies.


The Arquillian & Junit dependencies:-


Build profile setting:-

Now the resource files defined above and required for Arquillian to correctly setup the resources

arquillian.xml

<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://jboss.org/schema/arquillian
        http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    <container qualifier="glassfish-embedded" default="true">
        <configuration>
            <property name="resourcesXml">
              src/test/resources-glassfish-embedded/glassfish-resources.xml
             </property>
        </configuration>
    </container>

</arquillian>

glassfish-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC
    "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN"
    "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>

    <jdbc-resource pool-name="ArquillianEmbeddedOraclePool" jndi-name="jdbc/testresource/ORAlookup"/>
    <jdbc-connection-pool name="ArquillianEmbeddedOraclePool" res-type="javax.sql.DataSource"
        datasource-classname="oracle.jdbc.pool.OracleDataSource" is-isolation-level-guaranteed="false">

  <property name="URL" value="jdbc:oracle:thin:@oraserver.com:1521:ora1"/>
    <property name="User" value="username"/>
    <property name="Password" value="password"/>

    </jdbc-connection-pool>
         

</resources>


Now let us look at the Test class which call its all:-



Line 25 - you have to use @RunWith annotation to specify 
Line 35 - ShrinkWrap api, that assembles it all. Shrinkwrap provides a simple mechanism to assemble archives like JARs, WARs, and EARs with a friendly, fluent API.
Line 47 - Injecting ServiceImpl

This is a JUnit test class that uses Arquillian to create archive that runs on embedded Glassfish env.



The structure of the complete project looks like:-






Finally, you can run maven install or maven test using Eclipse or from command prompt cmd> mvn clean install. You will see in the output logs, that Service and oracle DAO is called.




This example uses Arquillian to startup Glassfish env and run integration testing using JUnit.


Wednesday, September 11, 2013

JAXB - using XmlAdapter class and @XmlJavaTypeAdapter annotation to write custom binding from java bean to xml

We can use JAXB to convert data between a xml and java bean. Using JAXB annotations like @XmlRootElement, @XmlAccessorType & @XmlElement we can achieve marshaling/unmarshaling of java bean to xml and vice versa.

Now sometime we come across problems where we need some custom conversion of an xml attribute to a java object.  When we are binding our own classes to XML, we are hit with a situation where our class representation doesn't match what we'd like to see in the XML.
In those cases, @XmlJavaTypeAdapter comes to our rescue. We can implement a custom java type adapter in the following way.

Identify the conversion from/ to

For example sake, we will be converting a java.lang.String to a custom GUID class (com.myorg.GUID)

Write custom adapter

public class StringToGUIDAdapter extends XmlAdapter<java.lang.String, GUID> {
@Override
public GUID unmarshal(String strGUID) throws Exception {
return new GUID(strGUID);
}
@Override
public String marshal(GUID guid) throws Exception {
if(null == guid)
return null;
else 
return guid.toString();
}
}

As you see, we have to extend XmlAdapter class and implement two of its methods - namely unmarshal & marshal.

Define the annotation in your java bean

Now you need to define your annotation @XmlJavaTypeAdapter on the java field like:-
  
  @XmlJavaTypeAdapter(type=String.class,value =StringToGUIDAdapter.class)
    private GUID                    entity;

And that is it. Your custom binding is ready to be used. 

@XmlJavaTypeAdapter annotation can be used with the following program elements:
  • a JavaBean property
  • field
  • parameter
  • package

This kind of custom binding is very useful when using JAX-RS and JSON.

Friday, August 30, 2013

Aspect Oriented Programming (AOP) using CDI for Java EE

As defined by wikipedia, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. AOP forms a basis for aspect-oriented software development. (Aspect oriented programming)
Using AOP we break down our program logic into distinct parts called concerns. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. 
Certain terms and terminology commonly used in AOP are:-
  • Aspect: a modularization of a concern that cuts across multiple classes.
  • Join point: a point during the execution of a program
  • Advice: action taken by an aspect at a particular join point.
AOP has been very common in Spring Framework and is also available in Context & Dependency Injection (CDI) for Java EE. 
The benefits of using AOP are:-
  1. modularization of system level coding i.e. logging, transaction, security 
  2. minimal coupling and duplication of code making it easier to add new functionalities.
  3. faster development as developers can focus mainly on the business logic rather than thinking of cross-cutting concerns such as security, transaction, logging etc. The business logic looks clear and does not have logging, security, transactional details.

Some example areas where AOP can be used:-

  1. Logging - We can write aspect around methods so that we can log before and after states of a method without actually putting logging statements in the actual method.
  2. Transaction management - in applications where transaction has to be managed manually, we can write aspects to begin and commit or rollback transactions depending on success or failure of the business method.
  3. Security - we can apply method level security around a method. So we do not have to implement security and role check in every business method. Aspect can be written that will be fired before a method is invoked and incase the user is not authenticated it will return an exception.
  4. Checking and using cached data before data access from database - we can write aspects around a data access method which will access the cache to see if the data is present. Incase the data is present it will return the data from the cache. Otherwise it will make data access call to the database and also add the returned data to the existing cache. Sample application below will use this as an example. 

Sample Application

Since we are developing using CDI for Java EE, I will present a sample implementation of AOP and cross-cutting using Java EE. This application uses Glassfish 4 as the application server and using Java EE.
 In this application I will show how we can write an Aspect around a web service method. This aspect will be fired before and after the web service method and can be used for security validation as well as checking cached data before making a DB call.
 We have a RestFulService service, which handles RESTful calls. Method retrieveRecordByResource handles the @Get RESTful call and retrieves all the parameters that are sent as part of the RESTful service. 
We will create an aspect around this call, which will
  • monitor the variables coming as input to the REST call 
  • response returned as part of the REST call
  • will modify variables before the call reaches retrieveRecordByResource method.
Code for Advice:-

In the code above, we create an advice class and method checkCache, with annotation @AroundInvoke. @AroundInvoke defines an interceptor method that interposes on business methods. 
Line 42, calls the proceed api of InvocationContext. Before this call we see that we have extracted the input parameters that came as input from the REST service call. It also returns returnParam which is the output after the call to our business method retrieveRecordByResource. 
Line 31 to 42, we see that we have extracted input parameters before the call to business method and we can manipulate these variables. We can do logging, security check, transaction management etc. before actually calling the business method. 
Line 44, we have control of the output parameters and we can log or manipulate these parameters. 

Line 17, defines an annotation @CacheResource. We need to define an interface CacheResource for the same.


In simple terms this interface and annotation @CacheResource, link our advice and the business class.
The interceptor is defined in WEB-INF/beans.xml:-

This configuration defines our interceptor. 

Conclusion:-
AOP is a very useful and powerful feature of OO programming. CDI brings AOP and dependency injection (DI). We can integrate AOP with caching using caching frameworks like JCache or EhCache.
CDI is the Java standard for dependency injection and interception (AOP). You can read futher about the same in JSR 299.