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:
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:
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 :)