Spring MVC form handling annotation

Download Report

Transcript Spring MVC form handling annotation

Spring MVC form handling
annotation example
Agenda
Spring Annotations
Spring MVC 3 (2.5)
Spring Annotations
Spring 2.x Data Access Annotations
Spring 2.x Aspects
Spring 2.5 Context Annotations
Spring 2.5 Stereotypes
Spring 2.5 Factory Annotations
JSR-250 javax.annotations
Spring 2.5 MVC Annotations
Spring 3.0 MVC Additions
Spring 3.0 Annotations
What's new in Spring 2.5?
 Annotation-driven dependency injection through the
@Autowired annotation and fine-grained auto-wiring
control with @Qualifier.
 Support for JSR-250 annotations, including @Resource
for dependency injection of a named resource, as well as
@PostConstruct and @PreDestroy for lifecycle
methods.
 Auto-detection of Spring components that are annotated
with @Component (or one of several stereotype
annotations).
 An all-new annotation-driven Spring MVC programming
model that greatly simplifies Spring web development.
 A new integration test framework that is based on JUnit 4
and annotations.
What's new in Spring 3.0?
 Full-scale REST support in Spring MVC
 New annotations for Spring MVC, including
@CookieValue and @RequestHeader.
 Support for declarative validation with JSR-303 (Bean
Validation) annotations.
 Support for the new JSR-330 dependency injection
specification.
 Annotation-oriented declaration of asynchronous and
scheduled methods.
 A new annotation-based configuration model that allows
for nearly XML-free Spring configuration.
 The Object-to-XML (OXM) mapping functionality from
the Spring Web Services project has been moved into
the core Spring Framework.
Spring 2.5 Context Annotations
 @Scope
Indicates the scope to use for annotated class instances
Default == “singleton”
Options:
 Singleton
 Prototype
Web Options:
 Request
 Session
 Global session
Spring 2.5 Stereotypes
 @Component **
 Indicates that a class is a component
 Class is a candidate for auto-detection
 Custom component extensions
 @Controller
 Specialized Component
 Typically used with RequestMapping annotation
 Discussed in section on web mvc
 @Repository
 2.0 stereotype… previously mentioned
 Now an extension of @Component
 @Service
 Intended to be a business service facade
Spring 2.5 Factory Annotations
 @Autowired
 Marks a constructor, field, setter or config method for injection.
 Fields are injected
 After construction
 Before config methods
 @Autowired(required=false)
 Config:
 AutowiredAnnotationBeanPostProcessor
 @Configurable
 Marks class as being eligible for Spring-driven configuration
 Used with AspectJ
 @Qualifier
 Qualifies a bean for autowiring
 May be customized
 @Required
 Marks a method as being injection required
Types of Injections
Constructor
Setter
Field
New Injection Type
configuration
method
with any
number of
arguments
Annotation Call Stack
@Configuration and @Bean
Annotating a class with the
@Configuration indicates that the class
can be used by the Spring IoC container
as a source of bean definitions.
@Bean is a method-level annotation and a
direct analog of the XML <bean/> element.
@Configuration and @Bean
Spring <beans/> XML :
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
Bean-annotated :
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
@Autowired
You can apply the @Autowired annotation
to "traditional" setter methods.
At run time you will be able to access all
methods of the class without worrying
about how you got the class. This is
known as Dependency Injection.
@Autowired
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
Spring MVC 3 (2.5)
Spring 2.5 MVC Annotations
 @Controller
Stereotype used to “Controller” of MVC
Scanned for RequestMappings
 @RequestMapping
Annotates a handler method for a request
 @RequestParam
Annotates that a method parameter should be bound to
a web request parameter
 @SessionAttributes
Marks session attributes that a handler uses
Spring 3 Annotations (New Only)











@Value
@RequestHeader
@CookieValue
@PathVariable
@Async
@Scheduled
@Model
@Bound
@ActionMapping
@EventMapping
@RenderMapping






@ResourceMapping
@ExceptionHandler
@Mapping
@RequestBody
@ResponseBody
@ResponseStatus
Setting up Spring MVC
Spring MVC in our application is to place
the following <servlet> declaration in the
web.xml file:
Spring MVC configuration
Use the spring-context schema as shown
in the following XML snippet:
Spring MVC configuration (2)
 In a Spring MVC application, you may configure a
custom ConversionService instance explicity as an
attribute of the annotation-driven element of the MVC
namespace.
SimpleUrlHandlerMapping
In Spring configuration :
Handling Annotated Controllers
In Spring configuration :
Both are automatically registered by
DispatcherServlet
Spring MVC namespace
New in Spring 3.0:
Automatically discovering beans
 When you added to your Spring <context:annotationconfig> configuration, you told Spring that you want it to
honor a certain set of annotations in the beans that you
declare and to use those beans to guide bean wiring.
Annotating beans for auto-discovery
 By default, looks for classes that are annotated
<context:component-scan> with one of a handful of special
stereotype annotations:
@Component - A general-purpose stereotype
annotation indicating that the class is a Spring
component.
@Controller - Indicates that the class defines a Spring
MVC controller.
@Repository - Indicates that the class defines a data
repository.
@Service - Indicates that the class defines a service.
Any custom annotation that is itself annotated with
@Component
@Controller
This annotation support is available for
both Servlet MVC and Portlet MVC.
Typically used with @RequestMapping
annotation.
A simple controller
SimpleFormController vs @Controller
 In XML-based Spring MVC web application, you
create a form controller by extending the
SimpleFormController class. In annotationbased, the @Controller can also use to handle
form.
SimpleFormController vs @Controller
SimpleFormController
public class CustomerController extends SimpleFormController{
//...
}
Annotation
@Controller
@RequestMapping("/customer.htm")
public class CustomerController{
//...
}
Mapping requests with
@RequestMapping
The class-level annotation maps a specific
request path (or path pattern) onto a form
controller.
The method-level annotations narrowing
the primary mapping for a specific HTTP
method request method ("GET"/"POST")
or specific HTTP request parameters.
@RequestParam
Annotation which indicates that a method
parameter should be bound to a web
request parameter.
Mapping requests
 By path
 @RequestMapping(“path”)
 By HTTP method
 @RequestMapping(“path”, method=RequestMethod.GET)
 POST, PUT, DELETE, OPTIONS, and TRACE are are also
supported
 By presence of query parameter
 @RequestMapping(“path”, method=RequestMethod.GET,
params=”foo”)
 Negation also supported: params={ “foo”, “!bar” })
 By presence of request header
 @RequestMapping(“path”, header=”content-type=text/*”)
Obtaining request data
 A query parameter value
 @RequestParam(“name”)
 A path element value
 @PathVariable(“var”)
 A request header value
 @RequestHeader(“name”)
 A cookie value
 @CookieValue(“name”)
 The request body
 @RequestBody
 The request body and any request header
 HttpEntity<T>
formBackingObject() vs
RequestMethod.GET
 In SimpleFormController, you can initialize the
command object for binding in the
formBackingObject() method. In annotationbased, you can do the same by annotated the
method name with @RequestMapping(method
= RequestMethod.GET).
formBackingObject() vs
RequestMethod.GET (cont.)
 SimpleFormController
@Override
protected Object formBackingObject(HttpServletRequest request)
throws Exception {
Customer cust = new Customer();
//Make "Spring MVC" as default checked value
cust.setFavFramework(new String []{"Spring MVC"});
return cust;
}
formBackingObject() vs
RequestMethod.GET (cont.)
 Annotation
@RequestMapping(method = RequestMethod.GET)
public String initForm(ModelMap model){
Customer cust = new Customer();
//Make "Spring MVC" as default checked value
cust.setFavFramework(new String []{"Spring MVC"});
//command object
model.addAttribute("customer", cust);
//return form view
return "CustomerForm";
}
onSubmit() vs RequestMethod.POST
 In SimpleFormController, the form submission is
handle by the onSubmit() method. In
annotation-based, you can do the same by
annotated the method name with
@RequestMapping(method =
RequestMethod.POST).
onSubmit() vs RequestMethod.POST
SimpleFormController
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command,
BindException errors) throws Exception {
Customer customer = (Customer)command;
return new ModelAndView("CustomerSuccess");
}
onSubmit() vs RequestMethod.POST
Annotation
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("customer") Customer customer,
BindingResult result, SessionStatus status) {
//clear the command object from the session
status.setComplete();
//return form success view
return "CustomerSuccess";
}
referenceData() vs @ModelAttribute
In SimpleFormController, usually you put
the reference data in model via
referenceData() method, so that the form
view can access it. In annotation-based,
you can do the same by annotated the
method name with @ModelAttribute.
referenceData() vs @ModelAttribute
 SimpleFormController
@Override protected Map referenceData(HttpServletRequest request)
throws Exception {
Map referenceData = new HashMap();
//Data referencing for web framework checkboxes
List<String> webFrameworkList = new ArrayList<String>();
webFrameworkList.add("Spring MVC");
webFrameworkList.add("Struts 1");
webFrameworkList.add("Struts 2");
webFrameworkList.add("JSF");
webFrameworkList.add("Apache Wicket");
referenceData.put("webFrameworkList", webFrameworkList);
return referenceData;
}
 Spring’s form
<form:checkboxes items=“${webFrameworkList}" path="favFramework" />
referenceData() vs @ModelAttribute
 Annotation
@ModelAttribute("webFrameworkList")
public List<String> populateWebFrameworkList() {
//Data referencing for web framework checkboxes
List<String> webFrameworkList = new ArrayList<String>();
webFrameworkList.add("Spring MVC");
webFrameworkList.add("Struts 1");
webFrameworkList.add("Struts 2");
webFrameworkList.add("JSF");
webFrameworkList.add("Apache Wicket");
return webFrameworkList;
}
 Spring’s form
<form:checkboxes items=“${webFrameworkList}" path="favFramework" />
initBinder() vs @InitBinder
In SimpleFormController, you define the
binding or register the custom property
editor via initBinder() method. In
annotation-based, you can do the same by
annotated the method name with
@InitBinder.
initBinder() vs @InitBinder
 SimpleFormController
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormat, true));
}
 Annotation
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormat, true));
}
From Validation
 In SimpleFormController, you have to register
and map the validator class to the controller
class via XML bean configuration file, and the
validation checking and work flows will be
executed automatically.
 In annotation-based, you have to explicitly
execute the validator and define the validation
flow in the @Controller class manually.
From Validation
SimpleFormController
<bean class="com.mkyong.customer.controller.CustomerController">
<property name="formView" value="CustomerForm" />
<property name="successView" value="CustomerSuccess" />
<!-- Map a validator -->
<property name="validator">
<bean class="com.mkyong.customer.validator.CustomerValidator" />
</property>
</bean>
From Validation
 Annotation
@Controller
@RequestMapping("/customer.htm")
public class CustomerController{
CustomerValidator customerValidator;
@Autowired
public CustomerController(CustomerValidator customerValidator){
this.customerValidator = customerValidator;
}
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(
@ModelAttribute("customer") Customer customer,
BindingResult result, SessionStatus status) {
customerValidator.validate(customer, result);
if (result.hasErrors()) {
//if validator failed
return "CustomerForm";
} else {
status.setComplete();
//form success
return "CustomerSuccess";
}
Working with Session
The type-level @SessionAttributes
annotation declares session attributes
used by a specific handler.
More Crazy Stuff
Annotation Access to:
Headers
Cookies
Reference
Spring MVC form handling annotation
example http://www.mkyong.com/springmvc/spring-mvc-form-handling-annotationexample/