Thursday, June 23, 2011

mvc:view-controller

This tag is a shortcut for defining a ParameterizableViewController that immediately forwards to a view when invoked. Use it in static cases when there is no Java Controller logic to execute before the view generates the response.

An example of view-controller that forwards to a home page is shown below:
Source: http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html

Tuesday, June 21, 2011

Spring 3.1 M2: Spring MVC Enhancements

Programming Model Improvements

This is the list of programming model improvements introduced in the new @MVC support classes.
  1. Declared @PathVariable arguments are now automatically added to the model. For example this:

  2. is replaced by:


  3. Redirect strings support URI templates expanded with variables from the model (including declared @PathVariables). For example this:

  4. is replaced by:


  5. URI template variables are supported in data binding. For example this:

  6. is replaced by:


  7. Consumable and producible media types can be specified via @RequestMapping. For example this:

  8. is replaced by:

    Besides being shorter the above returns NOT_ACCEPTABLE (406) if the URL matches but the input media type doesn't.

  9. For producible media types this:

  10. is replaced by:

    The above returns a NOT_SUPPORTED_MEDIA_TYPE (415) if the URL matches but the acceptable media type doesn't.

Friday, June 17, 2011

Removing taglib from web.xml

JSP 1.2 required the <taglib> directive to be used in web.xml for every JSP Custom Tag Library used by the application. In JSP 2.0 this is no longer required. This guide shows how to get rid of your <taglib> directives and how JSP 2.0 finds TLD files.
The <taglib> directive looks like this. You will find one in web.xml for older versions of Struts, and any other software which uses tag libraries in JSP 1.2.
 
Since JSP 2.0, this is now optional.
The TLD is discovered automatically when the taglib is first referenced in a JSP file.
The actual JSP 1.2 TLD files themselves are upwardly compatible with JSP 2.0, so do not need to be modified.

Where does JSP look for the TLD files?

JSP 2.0 containers such as Tomcat 5.5 search for TLD files in the following places:
The following locations are searched:
  • <appName>/WEB-INF folder
  • All subfolders of <appName>/WEB-INF
  • Inside each JAR file in <appName>/WEB-INF/lib, in the META-INF folder of the JAR. This method is prefered for easy deployment of a JAR containing a taglib.  

How should I package my JAR file?

Place TLD files in a folder named META-INF, at the root level of the JAR file. eg:

Why is this better?

For library developers, distribution is now simpler as packaging is as simple as including the TLD file in the JAR file.
For end-users, deployment and re-use is simpler, as a tag library JAR file can simply be dropped into the WEB-INF/lib folder with no web.xml configuration required.

TLD Tips in JSP 2.0

  • The TLD file does not need to have the same name as the library. For example, your mytags library definition can be in a file called mytaglist.tld. However, for easier identification later, it is good practise to use the same name as you intend to use in the JSP files. For example, if you plan to reference your library as mytags, with <mytags:testtag> in JSP, call the file mytags.tld.
  • The <short-name> element in the TLD file does not need the same name as the library. Once again, for easier maintenance and to avoid potential clashes, it is good practise to use the same name. For example <short-name>mytags</short-name>.
  • The <uri> in the TLD file is the main reference for matching JSP declarations with the correct TLD. When automatically finding TLD files, the container matches the uri attribute in your JSP taglib declaration with the <uri> tag in the TLD file. Ensure this is always unique to avoid clashes. For example:


Upgrading your application

  • Many older applications still include the <taglib> directive in web.xml. If your application falls into this category, try removing it and check if everything still works. Don't forget to move your TLD file into WEB-INF, or preferably into your JAR file. Your web.xml will be simpler, and the Tag Library will be more modular (easier to remove from the app, replace, or reuse).
  • In older applications, the TLD file is sometimes in a separate folder, such as in the example above (/WEB-INF/jsp/mytaglib.tld). In this case, move the file into the JAR file if possible. TLD files will still work from subfolders of WEB-INF, but since the location of the TLD files is no longer recorded in web.xml, it can make them harder to find when debugging your system later - especially after a few months of not looking at it, or if someone else is trying to debug your code. It therefore makes sense to either include them in the JAR file which contains the classes for the tag, or at least place them in an easy to notice location such as a subfolder called WEB-INF/tld or WEB-INF/taglibs.

Reference: http://wiki.metawerx.net/wiki/Web.xml

Thursday, June 9, 2011

Using @PathVariable

Let's say that you are creating an user account-based web application in Spring 3.0. You would like to create a controller that would allow users to see each other's profile by using a URL such as
http://myapp/profile/username.

For example:
http://myapp/profile/Juan would show user Juan's profile.
http://myapp/profile/Gaby would show user Gaby's profile.


Spring 3.0's annotated controllers make this kind of mapping easy with the new @PathVariable mapping. As you can see, the RequestMapping for the showProfile method has something a bit unusual: {username}
This is a PathVariable, and it means that this method will serve any request of the format "/profile/someUserName"

We capture the actual username in the next line using the @PathVariable annotation and store it in the variable
final String username
Now we can use it however we want!
Viva Spring framework!!!

Wednesday, June 1, 2011

Log4J: Properties and XML

Personally I prefer the XML instead of the properties file, but...
On this case I was having some trouble with my App server (WebLogic) - it wasn't loading my log4.xml, so I had to come up with a workaround: by using a propeties file.

This is more a look and learn than look and follow this steps. Because by looking to my XML file you'll see the equivalent in properties file, maybe this will help you when you have to write one or another.