MVC on the WEB - Amazon Development Center …romania.amazon.com/academy/pdf/day2_2.pdf · Model...
-
Upload
hoanghuong -
Category
Documents
-
view
223 -
download
0
Transcript of MVC on the WEB - Amazon Development Center …romania.amazon.com/academy/pdf/day2_2.pdf · Model...
MVC on the WEB
~$> whoami
Ioan Marius Curelariu
SDE at Website Analytics
Amazon Development Center Romania
The Server Side Story
The Server Side Story
Elastic Beanstalk
The Spring Framework
• First described in 2002, in “Expert One-on-One J2EE Design and Development” book, by Rod Johnson.
Spring Container
Spring Container
Spring Container
Spring Modules
Core
AOP
Data Access Objects
Object Relational Mapping
Context
Web
MVC
Model View Controller
Controller
View
Model User
Uses
Manipulates
Updates Sees
Spring MVC Architecture
Controller
Handler Mapping
View Resolver
View
Dispatcher Servlet
1. Request
Model and View
Application Logic
Application Data
2
3
4
5 6
TodoMVC Data Model
Todo Todos
TodoSpringMVC Data Model
Todo Todos
User Users
TodoSpringMVC Data Model
TodosManager Todos
User UsersManager
Spring MVC Architecture
Controller
Handler Mapping
View Resolver
View
Dispatcher Servlet
1. Request
Model and View
Application Logic
Application Data
2
3
4
5 6
DispatcherServlet
In /WEB-INF/web.xml <servlet> <servlet-name>appServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> appServlet </servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
ContextLoaderListener
In /WEB-INF/web.xml <context-param> <param-name> contextConfigLocation </param-name> <param-value> /WEB-INF/spring/root-context.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
Essential Configuration Declare the controllers and other components (appServlet-context.xml)
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:todospringmvc.properties</value> </property> </bean> <context:component-scan base-package=“com.amazon.todo.web”/>
Essential Configuration Resolve JSP Views
<bean name=“viewResolver” class=“org.springframework.web.servlet.view.InternalResourceView Resolver”> <property name=“prefix” value=“/WEB-INF/views”/> <property name=“suffix” value=“.jsp”/> </bean>
Essential Configuration Handle annotation-driven request mappings <mvc:annotation-driven/>
Spring MVC Architecture
Controller
Handler Mapping
View Resolver
View
Dispatcher Servlet
1. Request
Model and View
Application Logic
Application Data
2
3
4
5 6
SpringMVC Controller
• Configured using java annotations
– @Controller
– @RequestMapping
• Simple JavaBeans
• Multiple handler mappings
@Controller public class HomeController { … @ModelAttribute(value = "userName") public String getUserName(Principal user) { return user.getName(); } @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(new Date()); model.addAttribute("serverTime", formattedDate ); return "home"; } }
@Controller @RequestMapping(“/register”)
public class RegistrationController {
@RequestMapping(method = RequestMethod.POST)
public String registerUser(TodoUser todoUser) {
return “redirect:/”;
}
@RequestMapping(method = RequestMethod.POST)
public String register(TodoUser todoUser) {
return “register”;
}
@RequestMapping(value = “/validateUsername")
public @ResponseBody String suggestUsername(@RequestParam String username) {
return username;
}
}
@Controller @RequestMapping(value = "/todos", produces = "application/json") public class TodosController { … @RequestMapping( value = "/{todoItemId}", method = RequestMethod.PUT, consumes = "application/json" ) @ResponseBody public TodoItem alterTodo(@PathVariable String todoItemId, @RequestBody TodoItem todoItem) { … } }
Spring MVC Parameters & Return types • Parameters can be
– HttpServletRequest / HttpServletResponse / HttpSession
– WebRequest
– InputStream / OutputStream
– @RequestParam / @PathVariable / @RequestBody / @CookieValue
• Return types can be
– ModelAndView / Model / Map
– View Object
– String (logical view name)
– Void
SpringMVC Views
• Integrates with many existing technologies
• Out of the box support for JSP & JSTL
Spring MVC Views
JSON
Negotiating Content
Request
ContentNegociatingViewResolver
Other Spring MVC Features
• Exception Handling
• Localization Support
• Theme Handling
• Multipart (file upload) support
• Asynchronous Request Processing
• Testability
What we have done …
Hosting ...
First Beanstalk Application
Basic Application Informations
Application Environment Details
Application Configuration Details
Amazon RDS Instance Configuration
Configuration Summary
Application Console
Deploying a New Version
Thank you!...
http://amazonacademytodos.elasticbeanstalk.com