Tuesday, March 22. 2011
When using Spring annotation based controllers, @RequestMapping must be applied to a method as well as the class or it will not work. For example:
@Controller
@RequestMapping("/blah.html")
public MyController {
@ModelAttribute("session")
public InventorySession getSession() {
return session;
}
}
Will result in No mapping found for HTTP request with URI [/[contextpath]/blah.html] errors. Request methods (or sub-pages) must be mapped to controller methods, i.e.:
. . .
@RequestMapping(method={RequestMethod.GET})
public String getForm() {
return "formView.jsp";
}
. . .
This mistake is particularly easy to make when migrating older code to the newer annotation based model, where mappings on legacy code may make it (erroneously) appear such linking is done in the xml file already.
Friday, March 4. 2011
Spring, my favorite Java application framework, doesn't provide an easy way to bind Spring-managed session beans to JSP pages. I have had success doing this by binding the bean to the request at creation time. When doing it this way, be sure to call the session bean somewhere in your dispatch servlet configuration or it will never be initialized when individual requests are created.
Note that for most applications, session beans are better left as plain objects managed independently of Spring for less overhead and to keep them as simple as possible. Each of these is going to take up space for each user. However, I have found some cases where accessing Spring objects from within a session bean is desirable; if you have as well or curiosity prevails, details are below.
Continue reading "Spring Session Bind to JSP"
Monday, October 25. 2010
I was recently asked how to do custom date parsing in Perl, specifically for a date in the format mmddyyyy. I work with dates frequently in Java, so I am familiar with using SimpleDateFormat objects to parse dates in this manner. The Perl equivalent is Date::Manip::Date.
Continue reading "Perl Custom Date Parsing"
Wednesday, March 3. 2010
Upgrading an old Java Swing application using Substance LAF to 5.3, the old method of instantiating org.jvnet.substance.SubstanceLookAndFeel fails as this class is now abstract. This is documented in the release notes; however instantiating a concrete skin using:
UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());
Causes the following error for most of them on OS X:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.apple.laf.AquaRootPaneUI cannot be cast to org.jvnet.substance.SubstanceRootPaneUI
Use the third recommended option instead:
SubstanceLookAndFeel.setSkin(new SubstanceBusinessBlueSteelSkin());
This usage resolves the issue for every skin I've tested.
Tuesday, November 17. 2009
When working with automatic id's in Grails, sequence generation uses a less than useful default sequence name of hibernate_sequence. Use the following syntax as part of the GORM mapping to set the sequence name to something sane:
static mapping = {
id generator:sequence,params:[name:'mytable_id_seq']
}
Wednesday, November 11. 2009
Working on a Spring application using Hibernate recently I encountered the dreaded org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions error. Fixes for this vary widely; it can be caused by problems ranging from bad session handling in your framework configuration, bad cascade settings, incorrect persistence settings, to forgetting initialization of an array stored in a @ManyToOne database relation.
Make sure to check thoroughly for the latter before wasting hours changing the transaction handling, trying various cascade settings, or mucking with persistence annotations.
Wednesday, October 28. 2009
When using the FCKeditor plugin for Grails, if the data is loaded into the view this way:
<fckeditor:editor name="detail">
${fieldValue(bean:itemInstance,field:'detail')}
</fckeditor:editor>
The raw html being retrieved from the data source will not be parsed as expected. This will display as html source in the editor, and if saved will result in the html tags being placed into the data source as escaped text. For correct handling, add .decodeHTML() to the JSTL retrieval:
<fckeditor:editor name="detail">
${fieldValue(bean:itemInstance,field:'detail').decodeHTML()}
</fckeditor:editor>
Tuesday, June 9. 2009
Every programmer I know has played Dungeons and Dragons at some point. The C++ Lands Map is a beautiful merging of the two worlds. There are some amusing comments on the map at boing boing too.
I would love to have a poster print of this on some good quality parchment-like stock. Perhaps the author will do a commercial printing of it in the future.
Thursday, February 5. 2009
When building Spring controllers, it is often useful to access a DAO instance which has been set up in applicationContext.xml. To do so, add a variable in the controller for the DAO as well as a setter for it. Once this is done, wire it up by reference in dispatcher-servlet.xml.
Continue reading "Injecting DAO Objects Into Spring Controllers "
Often when working with Spring Web MVC mappings I want all calls to a certain extension handled by a generic resolver, i.e. mapping "*.html" to corresponding "*.jsp" entries in WEB-INF. This is easily done using UrlFilenameViewController which automatically passes the parsed filename to the view resolver.
Continue reading "Spring Generic URL Handling"
Monday, October 20. 2008
A Java Swing user interface I am working on which makes extensive use of a custom JTableModel implementation was failing. In certain instances when my data structure changes, it purges empty cells. If the cursor was left in these cells while this was taking place, the table would become unresponsive, often requiring an application restart to fix.
Watch for issues with setValueAt, which in some cases can be called with invalid values. In particular if the cursor is editing a field, the table is not refreshed until the edit focus leaves the widget even if you try to force it with validate() calls. In my instance the ideal fix was to add checks and do nothing instead of throwing exceptions.
With getValueAt, some table widget types choked on my default of null, which I was returning for invalid cells. Using an empty string instead of null for these invalid cells fixed this problem.
Wednesday, September 17. 2008
Firefox 3 introduces a new behavior when your doctype is XHTML Strict; certain tags that do not allow minimization as per the spec no longer close anyway. For example, if you use a <script src="blah.js" /> tag in your heading, Firefox 3 will take the whole rest of the page as a script, which will result in a blank page. This is good as it enforces standards compliance, however jspx minimizes tags, so you may have <script src="blah.js"></script> on your page, but the actual rendered page will minimize it.
There are many ways to fix this issue by playing tricks on jspx, I recommend:
<script src="blah.js"><!-- //prevent jspx minimization --></script>
This reminds me when I'm working on the code why that comment is there. Hopefully this will be fixed in the jspx spec at some point.
Thursday, June 5. 2008
A project I am working on requires binary image files and form data to be submitted simultaneously. This is rather easy to do with Spring, following the directions available in the latest documentation to handle file uploads in a form. It does not mention there that you can mix form fields as well as file upload fields when doing this, but it works as expected.
Continue reading "Spring Binary and Text Forms"
Wednesday, February 27. 2008
Here is the easiest process I have found for setting up Ruby and PostgreSQL on OS X Leopard at this time.
I recommend PostgreSQL for Mac instead of fink as it is more actively maintained and easier to install. Note that when downloading, use the alternate download not the Sourceforge links, as the Sourceforge download is not current and has issues. This installer places the PostgreSQL CLI commands in /Library/PostgreSQL8/bin/ and will auto-start PostgreSQL on system startup. It includes some graphical tools, but I recommend ignoring them and and installing pgAdmin III if you want an administration GUI (I usually just use the CLI tools).
Ruby has a couple PostgreSQL gems, I recommend ruby-pg which appears to be the only one actively maintained. The easiest way to get it installed is to download the latest .gem, then run the following command to install it in the directory where you saved it (replace the version number as necessary):
PATH=$PATH:/Library/PostgreSQL/bin/ gem install pg-0.7.9.2008.02.05.gem
Don't waste your time trying to find documentation for this gem online, point your browser at the documentation on your local machine, which for this version was at:
file:///Library/Ruby/Gems/1.8/doc/pg-0.7.9.2008.02.05/rdoc/index.html
Thursday, December 20. 2007
When storing preferences for applications on Java, there is a built in Preferences API. If you need full control of preference storage or need to change it from applications external to Java or manually, one of the options I discussed last year may be better (if you need an XML file in a specific location for example), but if you just need simple cross-platform preference storage, the built in option is ideal.
Usage Summary
import java.util.prefs.Preferences;
Preferences prefs = Preferences.userNodeForPackage( getClass() );
prefs.get("keyName", "default value");
prefs.put("keyName", "new value");
The JavaDoc for it is quite confusing, so try this tutorial on java.util.prefs.Preferences instead.
|