While searching for a Java based spell checker that would work nicely with our new website we found it hard to find something that matched our needs so we decided to create our own, well sort of. Instead of reinventing the wheel we used the Jazzy Spell Check API along with JavaScript and CSS used in a PHP based spell checker that can be found at http://www.broken-notebook.com.
JSP Spell Checker How-To
- Download and install the Jazzy API and add the Jazzy-core.jar to your class path (normally you would just need to add this file into the WEB-INF/lib directory).
- Download and install Ajax-Spell — you will be using the javascript and css files from this tool and just replacing the server side components.
- Create and compile the java/jsp code seen below (simple cut and paste should do). You will also need to compile the StringUtil.java file.
- Make minor modifications to spell_checker.js
- Add/Remove dictionary files in SpellCheckerUtil.java
- Add HTML for spell checker to your site!
Spell Checker Java Utility
The first piece of code you will need is the SpellCheckerUtil.java (see below). This code is a generic wrapper on top of the Jazzy API and is used by the SpellChecker.jsp page to provide the actual spell checking used by the JavaScript. You will also need to compile the StringUtil.java file.
Spell Checker JSP
Download file SpellChecker.jsp and place it in your web application.
Modifications to spell_checker.js
spell_checker.js is the JavaScript code provided by http://www.broken-notebook.com. I did need to make a few very small changes for the JS to work with the data sent back from the JSP:
- Add the trim string function into the javascript (source below)
- Add
new_string = trimString(new_string);
after the line
new_string = new_string.replace(/~~~/gi, "\n");
in switchText_cb.
Source code for trimString:
function trimString(sInString) {
 sInString = sInString.replace( /^\s+/g, "" );// strip leading
 return sInString.replace( /\s+$/g, "" );// strip trailing
}
Adding/Removing dictionaries
To add or remove dictionaries for your spell checker simply update the forceLoadDictionaries() method in the SpellCheckUtil class so that it adds (or removes) entries in the returned array. The dictionaries used by the sample code can be downloaded here and should be placed in the WEB-INF/classes directory.
HTML for your site
Well you gotten through the hard parts now to add your JSP SpellChecker to your site. All you need to do is include the appropriate JS and CSS files, which might look like this:
<link rel="stylesheet" type="text/css" href="spell_checker/css/spell_checker.css">
<script src="spell_checker/cpaint/cpaint2.inc.compressed.js"
type="text/javascript"></script>
<script src="spell_checker/js/spell_checker.js" type="text/javascript"></script>
Then you create a text area that looks like this:
<textarea title="spellcheck_icons" accesskey="SpellChecker.jsp"
id="spell_checker1" name="comment1" style="width: 400px; height: 150px;" />
</textarea>
Note the title and accesskey attributes are key to making this work.
JSP Spell Checker Demo
See a simple Demo of the JSP Spell Checker.
#1 by Rushikesh on November 30, 2009 - 1:04 pm
Quote
Some of the files are missing for Ajax spell checker functionality.
Can you please gv me all the files to implement this functionality.