margus hanni, nortal as spring mvc 01.04.2013. viited varasematele materjalidele… 2012 – tÜ -...
TRANSCRIPT
![Page 1: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/1.jpg)
Margus Hanni, Nortal AS
Spring MVC
01.04.2013
![Page 2: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/2.jpg)
Viited varasematele materjalidele…
2012 – TÜ - Spring MVC – Roman Tekhov
![Page 3: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/3.jpg)
What is a Web Framework?
A web framework is a software framework designed to simplify your web development life.
Frameworks exist to save you from having to re-invent the wheel and help alleviate some of the overhead when you’re building a new site.
![Page 4: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/4.jpg)
What is a Web Framework?
Typically frameworks provide libraries for accessing a database, managing sessions and cookies, creating templates to display your HTML and in general, promote the reuse of code.
![Page 5: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/5.jpg)
Spring Framework
Open source application framework.
Inversion of Control container (IoC).
Lots of utility API-s.
![Page 6: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/6.jpg)
Should I bother?
Spring framework is still extremely popular in Java (web) applications.
If you’re going to build web applications in Java then chances are that you will meet Spring.
![Page 7: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/7.jpg)
Should I bother?
![Page 8: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/8.jpg)
IoC in Spring
@Servicepublic class UserService {
@Resourceprivate UserDao userDao;
public User getByUserName(String name) {User user = userDao.getByUserName(name);// additional actionsreturn user;
}}
![Page 9: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/9.jpg)
IoC in Spring
@Servicepublic class UserService {
@Resourceprivate UserDao userDao;
public User getByUserName(String name) {User user = userDao.getByUserName(name);// additional actionsreturn user;
}}
Declare bean, Spring will create it
![Page 10: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/10.jpg)
IoC in Spring
@Servicepublic class UserService {
@Resourceprivate UserDao userDao;
public User getByUserName(String name) {User user = userDao.getByUserName(name);// additional actionsreturn user;
}}
Declare dependencies, Spring will inject them
![Page 11: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/11.jpg)
IoC in Spring
Spring handles the infrastructure (bean creation, dependency lookup and injection)
Developer focus on application specific logic.
![Page 12: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/12.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
![Page 13: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/13.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
Universal abstraction
![Page 14: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/14.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
One possible implementation.
Spring will create and register it
![Page 15: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/15.jpg)
Dependency injection
public interface UserDao {User getByUserName(String name);
}
@Repositorypublic class JdbcUserDao implements UserDao {
public User getByUserName(String name) {// load user from DB
}}
@Resourceprivate UserDao userDao;
Spring can inject as an abstraction type
![Page 16: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/16.jpg)
XML based configuration
Bean can also be defined and injected in XML.
<bean id="userDao" class="example.JdbcUserDao" />
<bean id="userService" class="example.UserService"><property name="userDao" ref="userDao" />
</bean>
![Page 17: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/17.jpg)
Dependency injection
Your code depends on abstractions, Spring handles actual implementations.
You can switch implementations easily.
![Page 18: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/18.jpg)
What is MVC?
The Model View Controller (MVC) pattern is a way of organising an application (not necessarily a web application) so that different aspects of it are kept separate. This is a good thing because:
![Page 19: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/19.jpg)
What is MVC?
It is good software engineering practice to maintain separation of concerns.
An application might have more than one user interface
Different developers may be responsible for different aspects of the application.
![Page 20: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/20.jpg)
Spring MVC
Spring based web framework.
Implements the Model-View-Controller design pattern.
Very flexible (we’ll see how exactly).
![Page 21: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/21.jpg)
Spring MVC
IoC again – framework handles the infrastructure, you focus on application specific things.
![Page 22: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/22.jpg)
Should I bother?
![Page 23: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/23.jpg)
Should I bother?
![Page 24: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/24.jpg)
Architecture - DispatcherServlet
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
![Page 25: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/25.jpg)
Recall: Model-View-Controller
Model - The model represents enterprise data and the business rules that govern access to and updates of this data.
View - The view renders the contents of a model.
Controller - The controller translates interactions with the view into actions to be performed by the model.
![Page 26: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/26.jpg)
Recall: Model-View-Controller
http://www.oracle.com/technetwork/java/mvc-detailed-136062.html
![Page 27: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/27.jpg)
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}
![Page 28: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/28.jpg)
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}
Declare controller
![Page 29: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/29.jpg)
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}
Inject Spring resources
![Page 30: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/30.jpg)
Method for handling requests
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}
![Page 31: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/31.jpg)
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}
What requests to serve?
![Page 32: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/32.jpg)
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}
Prepare model data
![Page 33: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/33.jpg)
Controller
@Controllerpublic class HelloController {
@Resourceprivate UserService userService;
@RequestMapping("/hello")public String hello(Model model) {
User user = userService.getByUserName("Cartman");
model.addAttribute("user", user);
return "hello";}
}Logical view name
![Page 34: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/34.jpg)
Model
Set of attributes that Controller collects and passes to the View.
![Page 35: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/35.jpg)
Model
Spring’s Model object…
@RequestMapping(value="/hello")public String hello(Model model) {
User user = userService.getByUserName(“cartman");model.addAttribute("user", user);...
![Page 36: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/36.jpg)
Model
Spring’s Model object…
@RequestMapping(value="/hello")public String hello(Model model) {
User user = userService.getByUserName(“cartman");model.addAttribute("user", user);...
![Page 37: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/37.jpg)
Model
Or plain java.util.Map
@RequestMapping(value="/hello")public String hello(Map<String, Object> model) {
User user = userService.getByUserName("cartman");model.put("user", user);...
![Page 38: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/38.jpg)
Model
Or plain java.util.Map
@RequestMapping(value="/hello")public String hello(Map<String, Object> model) {
User user = userService.getByUserName("cartman");model.put("user", user);...
![Page 39: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/39.jpg)
View
Any representation of output, invoked after the Controller, uses data from the Model to render itself.
![Page 40: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/40.jpg)
View technologies
Usually Java Server Pages that generate HTML.
Out-of-the-box there are also PDF, XML, JSON, Excel and other views.
You can create your own.
![Page 41: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/41.jpg)
View
Controller is totally decoupled from actual view technology.
@RequestMapping("/hello")public String hello() {...return "hello";
}
Just a logical view name to be invoked
![Page 42: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/42.jpg)
JSP view
<bean class="org.springframework...InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" />
</bean>
"hello” /WEB-INF/jsp/hello.jsp
![Page 43: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/43.jpg)
JSP view (hello.jsp)
Model attributes are accessible as EL (Expression Language) variables in JSP.
JSP: <p>Hello, ${user.fullName}!</p>
HTML: <p>Hello, Eric Cartman!</p>
![Page 44: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/44.jpg)
JSON view
JSON view transforms the whole model to JSON format.
<bean class="org.springframework...ContentNegotiatingViewResolver">
<property name="defaultViews"> <list>
<bean class="org.springframework...MappingJacksonJsonView" />
</list> </property></bean>
![Page 45: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/45.jpg)
JSON view
Outputs the Model as JSON document.
{"user":{"fullName":"Eric Cartman"}}
![Page 46: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/46.jpg)
Request mapping
@RequestMapping("/hello")
@RequestMapping(value="/hello", method=RequestMethod.GET)
@RequestMapping(value="/hello", params= {"param1", "param2"})
@RequestMapping(value="/hello", consumes="application/json", produces="application/json")
![Page 47: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/47.jpg)
Path variables
@RequestMapping(value="/hello/{username}")
public String hello(@PathVariable String username, Model model) {
...
http://[SERVER]/hello/cartman
![Page 48: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/48.jpg)
Path variables
@RequestMapping(value="/hello/{username}")public String hello(
@PathVariable String username,
Model model) {...
http://[SERVER]/hello/cartman
![Page 49: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/49.jpg)
Request parameters
@RequestMapping(value="/hello")public String hello( @RequestParam("username") String username, Model model) {
...
http://[SERVER]/hello?username=cartman
![Page 50: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/50.jpg)
Type conversion
HttpServletRequest parameters, headers, paths etc are all Strings.
Spring MVC allows you to convert to and from Strings automatically.
![Page 51: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/51.jpg)
Built-in conversion
There are some standard built-in converters.
@RequestMapping("/foo")public String foo(
@RequestParam("param1") int intParam,
@RequestParam("param2") long longParam) {...
![Page 52: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/52.jpg)
Type conversion
You can also define your own PropertyEditors
PropertyEditorSupport implements PropertyEditor
![Page 53: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/53.jpg)
Custom types
public class DateRange {
private Date start;private Date end;
public DateRange(Date start, Date end) {this.start = start;this.end = end;
}
public int getDayDifference() {// calculate
}}
![Page 54: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/54.jpg)
Custom type editor
public class DateRangeEditor extends PropertyEditorSupport {
private DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
public void setAsText(String text) throws IllegalArgumentException {String[] parts = text.split("-");
Date start = dateFormat.parse(parts[0]);Date end = dateFormat.parse(parts[1]);
setValue(new DateRange(start, end)); }
public String getAsText() {DateRange dateRange = (DateRange) getValue();return dateFormat.format(dateRange.getStart()) + "-" +
dateFormat.format(dateRange.getEnd());}
}
String to custom type
Custom type to String
![Page 55: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/55.jpg)
Register and use
@Controllerpublic class MyController {
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(DateRange.class, new DateRangeEditor()); }
@RequestMapping(value="/dateRange") public String dateRange(@RequestParam("range") DateRange range) {
... }}
![Page 56: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/56.jpg)
Method parameters
It all about IoC and Convention Over Configuration.
Your code simply declares what it needs from the environment, Spring makes it happen.
The order is not important, the type is.
![Page 57: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/57.jpg)
Method parameters
Model/Map – model
@RequestParam/@PathVariable annotated
HttpServletRequest, HttpServletResponse, HttpSession – it is all based on Servlet API!
![Page 58: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/58.jpg)
Method parameters
java.io.Writer / java.io.OutputStream – if you want to generate response directly in controller
…
![Page 59: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/59.jpg)
Method parameters
For example
@RequestMapping(value="/hello")public String hello(Model model, Writer writer, HttpServletRequest request, HttpSession session) {
...
![Page 60: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/60.jpg)
Return values
Same principle – you return what Spring awaits from you.
![Page 61: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/61.jpg)
Return value examples
String – logical view name.
voidIf you write the response in controller
If you use default/content negotiating views (like JSON earlier)
…
![Page 62: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/62.jpg)
Non-intrusive
Very important feature of a framework is non-intrusiveness.
Spring MVC normally lets you do stuff according to MVC pattern.
But it doesn’t prevent you from violating MVC if you really want to.
![Page 63: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/63.jpg)
No view examples
@RequestMapping(value="/noView")@ResponseBodypublic String noView() {
return "Too simple for a view";}
@RequestMapping(value="/noView")public void noView(Writer writer) {
writer.write("Too simple for a view");}
![Page 64: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/64.jpg)
Form
First you need to give Spring a new command object
@RequestMapping(value="/addUser")public String add(Model model) {
model.addAttribute("user", new User());return "add";
}
![Page 65: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/65.jpg)
Command object
public class User {
private String fullName;private int age;
// getters/setters...
![Page 66: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/66.jpg)
Form JSP
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form commandName="user"><form:input path="fullName" />...<form:input path="age" />...<input type="submit" value="Add" />
</form:form>
![Page 67: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/67.jpg)
Form
Spring will bind the data to our command object
@RequestMapping(value="/addUser", method=RequestMethod.POST)
public String save(User user) {// save user
return "home";}
![Page 68: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/68.jpg)
Validation
JSR-303 defines constraint annotations:
public class User {
@NotNull@Size(max=20)private String fullName;
@Min(10)private int age;
...
![Page 69: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/69.jpg)
Validation
@RequestMapping(value="/addUser", method=RequestMethod.POST)public String save(@Valid User user, BindingResult result,
Model model) {
if (result.hasErrors()) {model.addAttribute("user", user);return "add";
}
// save userreturn "home";
}
![Page 70: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/70.jpg)
Validation
@RequestMapping(value="/addUser", method=RequestMethod.POST)public String save(@Valid User user, BindingResult result,
Model model) {
if (result.hasErrors()) {model.addAttribute("user", user);return "add";
}
// save userreturn "home";
}
Check for validation errors.
Render the same view in case of errors
![Page 71: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/71.jpg)
Validation
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form commandName="user"><form:input path="fullName" /><form:errors path="fullName" />...<form:input path="age" /><form:errors path="age" />...<input type="submit" value="Add" />
</form:form>
Show validation errors
![Page 72: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/72.jpg)
Validation
It is of course possible to define your own constraints and validators.
![Page 73: Margus Hanni, Nortal AS Spring MVC 01.04.2013. Viited varasematele materjalidele… 2012 – TÜ - Spring MVC – Roman Tekhov](https://reader035.vdocument.in/reader035/viewer/2022062515/56649d015503460f949d47b4/html5/thumbnails/73.jpg)
Sources of wisdomSpring has great documentation: http://www.springsource.org/spring-framework#documentation
Java BluePrints - Model-View-Controllerhttp://www.oracle.com/technetwork/java/mvc-detailed-136062.html
Model-View-Controllerhttp://www.oracle.com/technetwork/java/mvc-140477.html
Inversion of controlhttp://en.wikipedia.org/wiki/Inversion_of_control