Tuesday, April 29, 2008

Cancel Button for CancellableFormController

input type="submit" name="_cancel" value="Cancel"

where by default "_cancel" is the cancelParamKey. You can use setCancelParamKey() to override it.

Redirect in Spring MVC

return new ModelAndView(new RedirectView(redirectURL, true), model);

Thursday, April 24, 2008

Form Validation Error Message

Use SpringFramework, SimpleFormController, and CustomDateEditor.

When the date input doesn't match the specified format, it will report an error message. The message is very much in Java developer's language, not user oriented.

To replace the message with your own, just put a line like that in your messages.properties,

typeMismatch=Invalid or incomplete input.

Spring Framework - Date Field in a SimpleFormController

  1. Override the initBinder method.
  2. Create a SimpleDateFormat, e.g. MM/dd/yyyy HH:mm:ss where M is month, d is day, y is year, H is hour(0-23), m is minute, and s is second.
  3. Set the date format lenient false, which means inputs do not have to match the format exactly
  4. Create a CustomDateEditor(dateFormat, true), where true means empty allowed.
  5. Register the editor to Date.class.

Thursday, April 10, 2008

Postgresql - Generate Primary Key before Insert

First define the primary key column as serial, it is a integer, but a sequence table will be created automatically to match it. For example, if you define an April_ID as type serial in table April, a sequence table called April_April_ID_SEQ will be created automatically when creating the April table.

Second in your program, use select nextval('schema.april_april_id_seq') to obtain the next value for primary key. You don't need to plus 1 since Postgresql does it for you. If using Spring JDBC template, just call getJdbcTemplate().queryForInt(the sql).

Third, use the value just obtained as parameter in the insert sql.

Done.

Monday, April 07, 2008

Velocity Layout in Spring

Spring 2.5.2/Velocity 1.5

In web.xml, define org.springframework.web.servlet.DispatcherServlet as spring. Mapping *.xhtml to spring. I leave html for real html files.

In spring-servlet.xml, define

viewResolver class=org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver

  • viewClass = org.springframework.web.servlet.view.velocity.VelocityLayoutView
  • layoutUrl = layouts/mainlayout.vm
  • cache = true
  • suffix = .vm
  • exposeRequestAttributes = true
  • exposeSessionAttributes = true
  • exposeSpringMacroHelpers = true
  • dateToolAttribute = dateTool
  • numberToolAttribute = numberTool

velocityConfigurer class = org.springframework.web.servlet.view.velocity.VelocityConfigurer

  • resourceLoaderPath = WEB-INF/views
  • velocityProperties directive.foreach.counter.name = loopCounter
  • directive.foreach.counter.initial.value=0

Create a layout file, mainlayout.vm under WEB-INF/views/layouts. Put html, body, stylesheet stuff in. Note the stylesheet file shall sit outside WEB-INF. e.g. stylesheet href = "includes/main.css" means the main.css file sits at [web-root]/includes forlder.

Put $screen_content whereever you want.

In spring controller, return the ModelAndView name "dashboard", create a dashboard.vm file under WEB-INF/views.

In the dashboard.vm file, put any content in.