1 / 73

Spring MVC

Spring MVC. Margus Hanni, Nortal AS. 01.04.2013. Viited varasematele materjalidele …. 2012 – TÜ - Spring MVC – Roman Tekhov. What is a Web Framework ?. A web framework is a software framework designed to simplify your web development life .

khuong
Download Presentation

Spring MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spring MVC Margus Hanni, Nortal AS 01.04.2013

  2. Viited varasematele materjalidele… • 2012 – TÜ - Spring MVC – Roman Tekhov

  3. 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.

  4. 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.

  5. Spring Framework • Open source application framework. • Inversion of Control container (IoC). • Lots of utility API-s.

  6. 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.

  7. Should I bother?

  8. IoC in Spring @Service publicclassUserService { @Resource privateUserDaouserDao; publicUsergetByUserName(Stringname) { Useruser = userDao.getByUserName(name); // additional actions returnuser; } }

  9. IoC in Spring Declare bean, Spring will create it @Service publicclassUserService { @Resource privateUserDaouserDao; publicUsergetByUserName(Stringname) { Useruser = userDao.getByUserName(name); // additional actions returnuser; } }

  10. IoC in Spring @Service publicclassUserService { @Resource privateUserDaouserDao; publicUsergetByUserName(Stringname) { Useruser = userDao.getByUserName(name); // additional actions returnuser; } } Declare dependencies, Spring will inject them

  11. IoC in Spring • Spring handles the infrastructure (bean creation, dependency lookup and injection) • Developerfocus on application specific logic.

  12. Dependency injection publicinterfaceUserDao { UsergetByUserName(Stringname); } @Repository publicclassJdbcUserDaoimplementsUserDao { publicUsergetByUserName(Stringname) { // load userfrom DB } } @Resource privateUserDaouserDao;

  13. Dependency injection publicinterfaceUserDao { UsergetByUserName(Stringname); } @Repository publicclassJdbcUserDaoimplementsUserDao { publicUsergetByUserName(Stringname) { // load userfrom DB } } @Resource privateUserDaouserDao; Universal abstraction

  14. Dependency injection publicinterfaceUserDao { UsergetByUserName(Stringname); } @Repository publicclassJdbcUserDaoimplementsUserDao { publicUsergetByUserName(Stringname) { // load userfrom DB } } @Resource privateUserDaouserDao; One possible implementation. Spring will create and register it

  15. Dependency injection publicinterfaceUserDao { UsergetByUserName(Stringname); } @Repository publicclassJdbcUserDaoimplementsUserDao { publicUsergetByUserName(Stringname) { // load userfrom DB } } @Resource privateUserDaouserDao; Spring can inject as an abstraction type

  16. 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>

  17. Dependency injection • Your code depends on abstractions, Spring handles actualimplementations. • You can switch implementations easily.

  18. Whatis MVC? • The Model View Controller (MVC) pattern is a way of organisingan application (not necessarily a web application) so that different aspects of it are kept separate. This is a good thing because:

  19. Whatis MVC? • It is good software engineering practice to maintain separation of concerns. • An application might have more than one user interface • Different developersmay be responsible for different aspects of the application.

  20. Spring MVC • Spring based web framework. • Implements the Model-View-Controller design pattern. • Very flexible (we’ll see how exactly).

  21. Spring MVC • IoC again – framework handles the infrastructure, you focus on application specific things.

  22. Should I bother?

  23. Should I bother?

  24. Architecture - DispatcherServlet http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

  25. 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.

  26. Recall: Model-View-Controller http://www.oracle.com/technetwork/java/mvc-detailed-136062.html

  27. Controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } }

  28. Controller Declare controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } }

  29. Controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } } Inject Spring resources

  30. Controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } } Method for handling requests

  31. Controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } } What requests to serve?

  32. Controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } } Prepare model data

  33. Controller @Controller publicclassHelloController { @Resource privateUserServiceuserService; @RequestMapping("/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName("Cartman"); model.addAttribute("user", user); return"hello"; } } Logical view name

  34. Model • Set of attributes that Controller collects and passes to the View.

  35. Model • Spring’s Model object… @RequestMapping(value="/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName(“cartman"); model.addAttribute("user", user); ...

  36. Model • Spring’s Model object… @RequestMapping(value="/hello") public String hello(Modelmodel) { Useruser = userService.getByUserName(“cartman"); model.addAttribute("user", user); ...

  37. Model • Or plain java.util.Map @RequestMapping(value="/hello") public String hello(Map<String, Object> model) { Useruser = userService.getByUserName("cartman"); model.put("user", user); ...

  38. Model • Or plain java.util.Map @RequestMapping(value="/hello") public String hello(Map<String, Object> model) { Useruser = userService.getByUserName("cartman"); model.put("user", user); ...

  39. View • Any representation of output, invoked after the Controller, uses data from the Model to render itself.

  40. 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.

  41. View • Controller is totally decoupled from actual view technology. @RequestMapping("/hello") public String hello() { ... return"hello"; } Just a logical view name to be invoked

  42. JSP view <beanclass="org.springframework...InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> "hello”/WEB-INF/jsp/hello.jsp

  43. 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>

  44. JSON view • JSON view transforms the whole model to JSON format. <beanclass="org.springframework...ContentNegotiatingViewResolver"> <propertyname="defaultViews"> <list> <beanclass="org.springframework...MappingJacksonJsonView" /> </list> </property> </bean>

  45. JSON view • Outputs the Model as JSON document. {"user":{"fullName":"Eric Cartman"}}

  46. 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")

  47. Path variables @RequestMapping(value="/hello/{username}") public String hello( @PathVariable String username, Modelmodel) { ... http://[SERVER]/hello/cartman

  48. Path variables @RequestMapping(value="/hello/{username}") public String hello( @PathVariable String username, Modelmodel) { ... http://[SERVER]/hello/cartman

  49. Request parameters @RequestMapping(value="/hello") public String hello( @RequestParam("username") String username, Modelmodel) { ... http://[SERVER]/hello?username=cartman

  50. Type conversion • HttpServletRequest parameters, headers, paths etc are all Strings. • Spring MVC allows you to convert to and from Strings automatically.

More Related