As I mentioned elsewhere, I have worked extensively with Java frameworks, starting with the specialized IBM San Francisco Framework to Oracle ADF. As I mentioned in my post, Frameworks make a developer’s work easier and also make the application development more streamlined and standardized.

More recently, I have worked on Apache Struts and Oracle’s ADF frameworks. Struts is a Web application Framework and Oracle ADF provides a lot of Visual components and make it easy to develop GUI application quickly. The Oracle Application Development Framework (Oracle ADF) extends the Struts framework to integrate with the Oracle ADF model layer.

First there was JSP and Servlets

If you have worked with Web applications in Java, you will know about JSPs and Servlets. Earlier, as Java evolved they were focusing on client side and we had applets. As the applets proved to be performance hogs, Server side Java programs evolved. These were called Servlets and were special Java programs that implemented HTTP protocol API in Java.

Servlets are programs to be written by programmers and cannot be done by Web designers. To make it easier for the Web designers to design the page, Sun developed the JSP or Java Server pages, a take on Microsoft’s ASP. JSP is a tag language that let you create HTML pages with specialized tags to include server side Java snippets/programs to get dynamic content.

Here is a simple JSP page to say Hello to you:

<hml>
<body>
<% String name = request.getParameter("name"); %>
Hello <%= name %>
</body>
</html>

JSPs however are just syntax sugar. Behind the scene they were compiled into Java Servlets which were compiled and run in a Java container. Here is part of the Servlet code generated for the above JSP:

public void _jspService.... {
try {
response.setContentType("text/html");
....

out.write("<hml>\r\n");
out.write("<body>\r\n");
String name = request.getParameter("name");
out.write("\r\n");
out.write("Hello ");
out.print( name );
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
} catch...

Though we are showing the
JSPs, Taglib

That was fine for a small web page like Hello World! However, the JSP pages were most abused with all the interspersed Java code between web page (HTML) design and thus it always needed programmers and designers to touch the same files, often leading to confusions and compile errors.

Soon, Java Standard Tag Library (JSTL) using Expression Language syntax (EL)evolved. It is essentially standard set of Tags in JSP, that designers can include in their HTML pages without worrying about how they are implemented. There are couple of jar files (JSTL, Standard) that need to be added to the server to make these standard JSTL tags work. Custom tags can be added as well, for which developers can develop Java code to translate them behind the scene. See this IBM Developerworks page for details on JSTL and this Oracle page about custom taglib.

Here is the modified version of the JSP:


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:set var="namex" value='${param["name"]}' />
<c:out value="Hello ${namex} - From JSTL"/>
</body>
</html>

And this eventually led way to Apache Jakarta Struts framework around 2000.

Struts

Here is the Struts version of above JSP file:

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<body>
<bean:write name="helloWorldForm" property="message" />
</body>
</html>

 

If you notice from the above example, Struts is essentially JSP, using custom tag libraries. You will also have to write some Java code behind this to make this customized tags to work. We have not included the Java code behind this here, but you can refer to this great blog for more info.

What is different in Struts is that, it implements Model-View-Controller (MVC) pattern for the JSP pages. Instead of letting the client program call JSPs directly, Struts has a backbone Controller, called Action Controller, that connects/loads various JSP pages (View in MVC). The controller uses Java beans (DAO = Data access objects) in Java to access data. These beans are the model in MVC.

The controller itself is a Servlet, essentially a Java program that connects the views and models together. Here is a simplified overview of the Struts Framework:

Courtesy: http://www.compchemcons.com

The above design was eventually termed as Struts-1 as this was followed by, you guessed it, Struts-2. Struts-1 depended heavily on the backbone Servlet and it exposed the Servlet and this made testing a little more challenging. Struts-2 however, like most of the modern Java frameworks, uses annotations and Dependency Injection to simplify the design. Now, you can use simple POJOs for the controllers. See here for a complete list of differences between Struts-1 and Struts-2.

Struts is configured and run using couple of XML files – web.xml and struts-config.xml. See here for more details how the different parts of a typical Struts MVC work together.

We will continue to dive into Struts in future posts.

Advertisements