Computer users have adapted to the idiocies of early solutions. Back when software was developed solely for advanced users, programmers were in charge of defining the user interfaces of these pieces of software. The problem currently lies in the fact that this has not changed. This particular rant could go on forever so I am going to stick with the problems with the current saving solution.
Lets take a trip down memory lane to the last time you worked on a document. Whenever you made a change to this document, what did you have to do? Save. If you wanted to rename the document, what would you have to do? Save As. When you would like to save a copy of the document somewhere else, what would you have to do? Save As. The list can continue, but I shall make my point.
This scenario makes me laugh. I realize that the only reason I know this is because It has been ingrained into my head since the beginning of computer time. Is the save, save as method intuitive? I say "I know how to do it so it must be." Is it really though? Let's compare these current solutions to reality. In the computer world, there are usually 2 copies of a document. One in memory, and one on the hard disk. When you add the bibliography to that annoying assignment you didn't know required a bibliography, you are changing it in memory. Then, to put it on the hard disk, you save it. Back in the stone age, my buddy is writing the same paper by hand. But for some reason he doesn't have 2 copies, and when he writes something down it stays on the paper! Genius!
This article is called problems with software not solutions to software problems so unfortunately I am not posting a concrete solution to base the next project on. However, this doesn't mean I can't throw some ideas out there. The kibosh could be placed on the save and save as features. When one would like to back up their document somewhere, perhaps the backup button? When someone would like to file there document initially, perhaps the file button? When someone would like to rename this document to something else, perhaps the rename button will suffice. This of course requires the use of a nice undo system which allows you to undo file write-overs and things of that nature, but this is for later articles.
Remember, just because it is the standard, doesn't mean it is a good solution.
Note: This article assumes you have done some Java web development in the past, and you are capable of compiling and executing web applications with J2EE.
Overview
J2EE has done some amazing things in progressing the creation of custom tags. Now it is fairly easy to integrate any custom HTML tag within your application further separating the view from designers. This article will show you how to implement a custom HTML tag that calculates the square of a number by simply placing <math:square num="12" /> into your web application. Hopefully, you can feel the power with this simple feature.
Creating the class
Every custom tag extends SimpleTagSupport. This is a class implemented by the J2EE spec which allows a class to output information to a jsp. So, lets get right to coding. Create a class in your web application that extends javax.servlet.jsp.tagext.SimpleTagSupport. If you are compiling by hand, the class file must go in the package structure under /WEB-INF/classes/ i.e. /WEB-INF/classes/com/cyberkruz/test/NewMath.class
l
Every attribute in your custom tag must have a Java Bean mutator property to go with it. This is handled with reflection to send the parameter to your class.package com.cyberkruz.test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class NewMath extends SimpleTagSupport {
private Integer num;
/**
* Method overridden which is called
* by the jsp.
*/
public void doTag() throws JspException, IOException {
// Gets the jsp context and prints the
// number squared.
this.getJspContext().getOut().print(this.num * this.num);
}
/**
* Sets a number that our custom
* tag squares.
* @param num The number to which
* we want to square.
*/
public void setNum(Integer num) {
this.num = num;
}
}
Configuring the application to use the class
Now, all we have to do is let our application know that the new tag is there. To do so, we can set up a TLD file which looks much like the document descriptor for configuring servlets. So lets do it! Create a new file called MyTags.tld and place it in the /WEB-INF directory. Then type the following:
<?xml version="1.0" encoding="iso-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.2</tlib-version>
<uri>myTags</uri>
<tag>
<description>Custom tags</description>
<name>Square</name>
<tag-class>com.cyberkruz.test.NewMath</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>num</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
This file configures our web application to use our new custom tag. Multiple tags can be placed and mapped with this single file. The taglib directive just specifies the schemas for this particular file and version. The uri specifies what location these tags are being mapped to. Under the tag element, the description and name should be pretty straight forward. The tag-class element points this tag to the class that we wrote previously. the body-content is stating that we don't want inline scripting for this element. Now the attribute tag is fairly interesting. While optional, it maps the num variable to our setter in the class we specified. Then, it says we must have it there (required element) and it can be specified at run time (rtexprvalue).
Now all we have to do is use it!
Place our new tag in a jsp
Allowing our jsp to use the custom tag is the easiest part. It only requires a single declaration.
<%@ taglib prefix="math" uri="myTags" %>
The taglib keyword tells the jsp what we are trying to do. The prefix is what we want to put before the tags when we call them, and the uri points to the uri specified in the tld file. This is going to load every tag that is configured in that tld file that we made previously.
Now, let's put it in a jsp. Create a jsp file called SimpleTest.jsp and place it in your web application. Place the following in your jsp:
<%@page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib="" prefix="math" uri="myTags" %>
<html>
<head>
<title>SimpleTest</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
10 squared is: <math:Square num="10" />.
</body>
</html>
Run it on your web server and... YAY! This is a base introduction to what you can do with custom simple tags. More information about how to use these is below. Enjoy!
More information
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPTags7.html