Monday, September 21, 2009

Spring framework MVC with annotations

In that article I will try to describe how to use SpringFramework MVC with annotations and the Tiles2 as view rendering.

The controller class is annotated with @Controller annotation.

@Controller

public class FooContorller {

public void action() {}

}

The controller can be mapped to one URL or to many URL

@Controller

@RequestMapping(“/index.html”);

public class FooSingleMappedController {

public void action() {}

}

or

@Controller

public class FooMultiMappedController {

@RequestMapping(“/index-1.html”)

public void fooAction1() {}

@RequestMapping(“/index-2.html”)

public void fooAction2() {}

}

also mapping can be specified for what type of request it will be done, in next example is done for GET or POST request:

@Controller

@RequestMapping(value=”/index-get.html”, method=RequestMethod.GET)

public class FooSingleMappedController {

public void action() {}

}

or

@Controller

public class FooMultiMappedController {

@RequestMapping(value=“index-get.html”, method=RequestMethod.GET)

public void doGet() {}

@RequestMapping(value=”index-post.html”, method=RequestMethod.POST)

public void doPost() {}

}

You can see that all our mapping URLs are finished with .html, this happen becouse in our web.xml will be defined that all URLs with .html at the end to be handled by SpringFramework MVC dispatcher. That defenition will look as:

fooprojectdispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

1

fooprojectdispatcher

*.html

Also you must add some configuration to spring xml configuration file:

The next step is to get access to HttpServletRequest and HttpServletResponse objects. The code will look like:

@Controller

@RequestMapping("/index.html")

public class FooController {

public void action(

HttpServletRequest request,

HttpServletResponse response) {}

}

The Springframework will inject request and response object to method. It seems that having request object we can get parameters from URL. It's true but there is another, more cool, way. Look at the code:

@Controller

@RequestMapping("/index.html")

public class FooController {

public void action(

@RequestParam("param1") String param1,

@RequestParam("param2") Integer param2) {}

}

Cool not? the Springframework convert automatically the request param to required type. But what happen if parameter is not send by request, than application throw exception :(, but exists solution! Let say that the param2 is not required, it can be, then we will have:

@Controller

@RequestMapping("/index.html")

public class FooController {

public void action(

@RequestParam("param1") String param1,

@RequestParam(value="param2", required=false) Integer param2) {}

}

Next question is how we send data from controller to view? The answer is ModelMap object. Look at next code:

@Controller

@RequestMapping("/index.html")

public class FooController {

public void action(ModelMap model) {

model.addAttribute("helloString", "Hello World");

}

}

Now in jsp page we have access to this parameter through EL expression:

<b>${helloString}</b>

Hmmm, but how we show which jsp file must be rendered? Yep, we linked the application with Tiles2 view render engine. To do that we must add some configuration to spring xml configuration file, and those are:

/WEB-INF/tiles-cfg/defs.xml

To be able to do multipart request add also:

A little about multipart requests! The multipart object is gotten from request as in next example:

@Controller

@RequestMapping("upload.html")

public class UploadController {

public void action(@RequestParam("file") MultipartFile f) {}

}

All our controller methods was returning void, thats wrong, they must return String, the name of .jsp file which must be rendered.

@Controller

@RequestMapping("/index.html") {

public String action(ModelMap model) {

model.addAttribute("helloString", "Hello World!!!");

reutrn "index"

}

}

If something happen in controller and the action must be redirected then we return

return "redirect:error.html";

that code will redirect to error.html page and the controller mapped to that URL will process request. Also if we want to redirect full specified URL then we return:

return "redirect:http://google.com";

What we will do if some output allready go to response? For example we change some session vars, then we can do forward and that is done with:

return "forward:error.html";

Thats all!

P.S. How to configure SpringFramework and Tiles2 welcome to http://google.com :)

Thursday, September 10, 2009

fu**ing with java pl localization :(

That was the post on twitter 16 min. ago, without stars :). What happen?

I'm working on a Java web application which must have PL localization. The current task was to add the PL localization to application, so with fmt tag lib from jstl I start to implement it. When all static text was replaced with fmt tags I found that some chars are not rendered correctly. Today all morning I was searching why it happening, I was thinking that problem was JSTL, but not at finally I found that the problem is JVM it trying to convert sources and resource files from any encoding to unicode and somewhere something is not working as I expected :(. Ok here is the solution if you have resource files in ru, pl, etc.

In java exist utility native2ascii translate your files with that utility like:

# native2ascii dict_pl.properties dict_pl.properties and all will be ok.

Ok, I leave to take a launch, now I can do it quite :)