Archive

Archive for May, 2010

Struts 1.3.5 Tiles – Basics

May 24, 2010 Leave a comment

Struts tiles provides us a templating system. The introduction below shows the basic reusability of the modules of a web page.
To start, we will build a basic struts project using a maven archetype. Read more of it here.
Add a new dependency to the pom file

 <dependency>
 <groupId>org.apache.struts</groupId>
 <artifactId>struts-tiles</artifactId>
 <version>1.3.5</version>
 </dependency>

In struts-config.xml of the project , add the following plugin

 <plug-in className="org.apache.struts.tiles.TilesPlugin" >
 <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
 <set-property property="definitions-parser-validate" value="true" />
 <set-property property="moduleAware" value="true" />
 </plug-in>

What we will now do is remove the default welcome.jsp page that was created by maven and reroute the request to a page (layout.jsp) that we create using tiles.
The components of the page will be the following : header.jsp, footer.jsp,menu.jsp , body.jsp and need to be located in the pages folder.
These will look as follows:

header.jsp

<div id="header">
 This is header
</div>

footer.jsp

<div id="footer">
 This is footer
</div>

body.jsp

<div id="body">
 This is body
</div>

menu.jsp

<div id="menu">
 This is menu
</div>

layout.jsp

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>

<html:html>
<head>

</head>

<body>
<table>
<tr valign='top'><td style="background-color:LightSlateGray "><tiles:insert attribute="menu"/></td>
<td><table>
<tr><td style="background-color:LightSkyBlue"><tiles:insert attribute="header"/></td></tr>
<tr><td style="background-color:LightSteelBlue "><tiles:insert attribute="body"/></td></tr>
<tr><td style="background-color:LightSkyBlue"><tiles:insert attribute="footer"/></td></tr>
</table>
</td>
</tr>
</table>
</body>
</html:html>

Uncomment the following from web.xml

<init-param>
<param-name>chainConfig</param-name>
<param-value>org/apache/struts/tiles/chain-config.xml</param-value>
</init-param>

Create a file tiles-defs.xml in the webapp folder of the project with the following content

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_3.dtd">
<tiles-definitions>
<definition name="Tiles.welcome" path="/pages/layout.jsp">
<put name="header" value="/pages/header.jsp" />
<put name="menu" value="/pages/menu.jsp" />
<put name="body" value="/pages/body.jsp" />
<put name="footer" value="/pages/footer.jsp" />
</definition>
</tiles-definitions>

So, now we have a tile definition by the name Tiles.welcome and the path /pages/layout.jsp. The layout.jsp has tiles header, menu, body and footer whose content will be in the files defined in the tiles-defs.xml file.

The last thing is to go to struts-config.xml file and change the forward path of the Welcome action…
Change ..

<action
path="/Welcome"
forward="/pages/Welcome.jsp"/>

to..

<action
path="/Welcome"
forward="Tiles.welcome"/>

Build and deploy the project. Go to the following url http://localhost:8080/test-struts-tiles (Note : The project I created was test-struts-tiles)
You should see the following

Note:
If you do not uncomment the lines in web.xml for chainConfig, you will get the following exception…
javax.servlet.ServletException: java.lang.IllegalArgumentException: Path Tiles.welcome does not start with a “/” character
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>

<html:html>
<head>

</head>

<body>
<table>
<tr valign='top'><td style="background-color:LightSlateGray "><tiles:insert attribute="menu"/></td>
<td><table>
<tr><td style="background-color:LightSkyBlue"><tiles:insert attribute="header"/></td></tr>
<tr><td style="background-color:LightSteelBlue "><tiles:insert attribute="body"/></td></tr>
<tr><td style="background-color:LightSkyBlue"><tiles:insert attribute="footer"/></td></tr>
</table>
</td>
</tr>
</table>
</body>
</html:html>

Web Usability: To click or not to click is the question… or … should there be a question at all?

May 16, 2010 Leave a comment

I have been building a web application at home and the challenge that I have faced is the UI design. I had assumed that I was logical enough to make the right choices until I ran into this book: Don’t Make Me Think by Steve Krug. It was an eye opener for me. Here are some things that I definitely need to change/add in my plan.
– Not have a separate button to take the user back to the home page. Make the site id do that instead. Have two different site ids for the application. The second site id has the text “home” in addition to the original site id. The second site id is for pages that are not home pages.
– Have a tagline on the web application in addition to a concise summary.
– Have tabs or menus to provide navigation to the 3rd and 4th level pages. (My goal — The tabs/menu should give a summary of what is available in the application)
– Name the pages. I am surprised I never thought of this.
– Have at least two visual cues. I ,like most developers, believed until now that subtle cues are more elegant. Not anymore.
– Have breadcrumbs.
There are many more changes that I need to make but I think the above are the most for the application I am working on.

Summary: If you haven’t read this book, go get a copy now.

Categories: Book, Usability Tags: , , ,

Basic Spring Code with Maven

May 10, 2010 1 comment

This blog shows how we can use a simple maven project and work with Spring to interact with a database.
First, we create a simple project using the maven command..

mvn archetype:generate

We select the maven-archetype-quickstart project (option 15)
Run the command

mvn eclipse:m2eclipse

to create the .classpath and .project files for eclipse.
We will create a file beans.xml in src/main/resources dir. Not creating the beans.xml file in the resources dir will give you the following exception:
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist

Here is my beans.xml file.

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="dataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
 <property name="url" value="jdbc:mysql://localhost:3306/database-name"></property>
 <property name="username" value="root"></property>
 <property name="password" value="root"></property>
 </bean>
 <bean id="template">
 <property name="dataSource" ref="dataSource"></property>
 </bean>
 <bean id="mySQLDao">
 <property name="template" ref="template"></property>
 </bean>
</beans>

Create a main class with the following code:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MySQLDao mySQLDao = (MySQLDao)context.getBean("mySQLDao");
System.out.println(mySQLDao.queryForDate());

In MySQLDao we have the following code:

 private JdbcTemplate template;

 public Date queryForDate(){
 String sql = "select now() from dual;";
 Date currentDate = (Date)template.queryForObject(sql, Date.class);
 return currentDate;
 }

 public void setTemplate(JdbcTemplate template) {
 this.template = template;
 }

In pom.xml add the following dependencies:

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>2.5.6</version>
 <scope>compile</scope>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.11</version>
 <scope>compile</scope>
 </dependency>



If the mysql-connector jar is not installed in the local repository , use the following command to install the jar.

mvn install:install-file -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.11 -Dfile=mysql-connector-java-5.1.11-bin.jar -DgeneratePom=true -Dpackaging=jar

Running the main class should now give the current timestamp from the database.
To run main from the command line use the following command

mvn exec:java -Dexec.mainClass="com.wordpress.codesilo.Main"