25/09/2013

[Java] Spring use DAO in JSP page

So you're happily coding your webapp with Spring, and have all your DAOs correctly defined as beans; you know they work because you are constantly injecting them around, but now you need to access one of them from a JSP.. how?

You may find this code fragment useful:

 <%@page import="com.groglogs.mydaos.MyDao"%>  
 <%@page import="org.springframework.web.context.WebApplicationContext"%>  
 <%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>  
   
 <%  
 MyDao myDao = null;  
   
 WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());  
 if (context != null) {  
   myDao = (MyDao) context.getBean("myDao");  
 }  
 %>  

You import your DAO, and request it directly from the JSP. Note that this example relies on the fact that somewhere you have correctly defined a bean named "myDao".

19/09/2013

[Eclipse] Comparison method violates its general contract error when updating

When installing updates in Eclipse, you may encounter the "Comparison method violates its general contract" error.

This may be related to a known bug in older Eclipse versions when using new JDKs (>6), I encountered it even using a JDK6 with latest Helios though. To fix it, try adding:

-Djava.util.Arrays.useLegacyMergeSort=true

in your eclipse.ini file, after the -vmargs flag, then restart Eclipse and run the update manager again.

[Eclipse] Slow update process

When installing updates via the Eclipse update manager, it sometimes takes forever to complete the update process, even for very small updates.

One cause may be Eclipse trying to contact all known sites when fetching updates. To avoid that, try adding:

-Declipse.p2.mirrors=false

in your eclipse.ini file, after the -vmargs flag then restart Eclipse. Alternatively, you can uncheck the "Contact all update sites" flag every time you run an update from the update wizard.

10/09/2013

[Windows] Manage printers from command line

On Windows, you can manage the printers connected to your computer from the command line either via Rundll32 printui.dll,PrintUIEntry on 32-bit systems or Cscript.exe and the Prnmngr.vbs script on 64-bit systems.

23/08/2013

[Android] Auto Answer app

Last year I bought an Android smartphone from LG, with Gingerbread 2.3.4 on it. Much to my surprise, it was not very smart, for example I couldn't set it to automatically answer incoming calls.

The good thing about having it run Android, was that I could fix this flaw by developing my own app, so I googled around and found an Auto Answer project on Google Code from Matt Hahnfeld, released under a GNU GPL v3 licence.

Since the author removed it from Google's Play Store, I catered his code to my needs and installed the app on my Optimus HUB. It works perfectly, I tested it on Ice Cream Sandwich 4.0.3 (LG Optimus L7) without issues, and it should work on Froyo 2.2 too.

At the time there weren't many free apps that did this but now you finally have some choices, note that some phones already come with this feature built-in.

You can find my code on GitHub, the licence remains the same GNU GPL v3. To successfully compile it you'll need an IDE such as Eclipse, and the proper Android SDK for your OS version. The source code I uploaded comes with the Android 4 lib, but you can easily switch it to another version.

22/08/2013

[Java] String to Date and vice versa

When working with dates in Java you'll often find yourself having to convert them from a String object to a Date object and vice versa. These function snippets will allow you to easily perform the conversion operations, no third-party libraries needed:

Imports

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


string2Date

 /** Converts given String in a Date object with specific format  
  * @param date - a String representing the date to convert. IT MUST BE in the given format  
  * @param dateFormat - a String specifying the format of the date e.g: dd/MM/yyyy  
  * @return a Date object, {@null} if the given string is {@null} or if there's been a conversion error  
  * */  
 public static Date string2Date(String date, String dateFormat){  
      Date d = null;  
      try{  
           if(date!=null && !date.equalsIgnoreCase("") && dateFormat!=null && !dateFormat.equalsIgnoreCase("")) d = new SimpleDateFormat(dateFormat, Locale.ITALIAN).parse(date);//or whatever Locale you need  
      }catch(Exception e){}//you could always decide to raise the exception rather than returning simply NULL  
      return d;  
 }  

date2String

 /** Converts given Date in a String object with specific format  
 * @param date - a Date to be represented as a string. IT MUST BE in the given format  
 * @param dateFormat - a String specifying the format of the date e.g: dd/MM/yyyy  
 * @return a String object, {@null} if the given date is {@null} or if there's been a conversion error  
 * */  
 public static String date2String(Date date, String dateFormat){  
      Format formatter = new SimpleDateFormat(dateFormat);  
      String s = null;  
      try{  
           if(date!=null && dateFormat!=null && !dateFormat.equalsIgnoreCase(""))s = formatter.format(date);  
      }catch(Exception e){}//you could always decide to raise the exception rather than returning simply NULL  
      return s;  
 }  

Of course, if you prefer, rather than returning NULL if something went wrong, you could always raise an Exception. You can find all accepted formats in the SimpleDateFormat JavaDoc.

28/07/2013

[Java 4] Download file from portlet

Scenario: Java 4, Spring 2, WebSphere 6, JSR-168. How the hell do I let the user download a file from a portlet?

The file is extracted from a database and does not exist on the server's filesystem yet; redirecting to a servlet to create and send the response which contains the file to download fails with a generic:

java.lang.IllegalStateException: Can't invoke sendRedirect() after certain methods have been called

More specifically, that method can not be invoked after any of the following methods of the ActionResponse interface has been called:
  •         setPortletMode
  •         setWindowState
  •         setRenderParameter
  •         setRenderParameters
  •         removePublicRenderParamter
And currently I don't know whether it's Spring or WebSphere calling one of those forbidden methods silently.