annotated controllers with spring mvc 2.5
DESCRIPTION
How to write controller using Java 5 annotations with Spring MVC 2.5.TRANSCRIPT
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Controller annotaticon Spring MVC 2.5
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Spring MVC
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
The Old Style
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
The annotated style
Controller@RequestMapping( "/my/ .html" )@ { * }
public class MyController {
RequestMapping@ public void index() {
// do something useful }
}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Configuration
No, thanks!(at least while I’m writing the code)
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
WEB-INF/web.xml
web-app< > display-name JavaDay 2 9 Demo Web Application /display-name< > 00 < >
!--< context-param< > param-name contextConfigLocation /param-name< > < > param-value< >/WEB-INF/applicationContext.xml /param-value< > /context-param< > -->
listener< > listener-class< >org.springframework.web.context.ContextLoaderListener /listener-class< > /listener< >
servlet< > servlet-name< >javaday /servlet-name< > servlet-class< >org.springframework.web.servlet.DispatcherServlet /servlet-class< > load-on-startup /load-on-startup< >1< > /servlet< >
servlet-mapping< > servlet-name javaday /servlet-name< > < > url-pattern .html /url-pattern< >* < > /servlet-mapping< >
welcome-file-list< > welcome-file redirect.jsp /welcome-file< > < > /welcome-file-list< >/web-app< >
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
WEB-INF/javaday-servlet.xml
?xml version=" . " encoding="UTF-8"?< 1 0 >beans xmlns="http://www.springframework.org/schema/beans"<
xmlns:xsi="http://www.w3.org/2 /XMLSchema-instance"001 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
context:component-scan base-package="< it.jugpadova.javaday.controller"/>
!--<bean<
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">property name="webBindingInitializer"< >
bean class="it.jugpadova.javaday.JavadayBindingInitializer"/< >/property< >
/bean< > -->
bean id="viewResolver"< class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />/beans< >
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Parancoe meta-framework
www.parancoe.org
Spring MVC + Hibernate/JPA + easy DAO + ...plugins
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Using the model
Controller@RequestMapping("/contact/ .html")@ *
public class ContactController {
Resource@ private ContactDao contactDao;
RequestMapping@ public void list(Model model) { List Contact contacts = contactDao.findAll();< > model.addAttribute("contacts", contacts); }
}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
WEB-INF/jsp/contact/list.jsp
table< > c:forEach var="contact" items="< contacts${ }"> tr< > td contact.name /td< >${ }< > td contact.email /td< >${ }< > td< > a href="edit.html?id= contact.id " Edit /a< ${ } > < > a href="delete.html?id= contact.id " Delete /a< ${ } > < > /td< > /tr< > /c:forEach< >/table< >c:if test="< empty contacts${ }" No contacts in the DB /c:if> < >a href="edit.html" New /a< > < >
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
More in the model
ModelAttribute(“countries”)@public List Country getCountries() < > {
// producing and returning the list
}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Getting parameters
RequestMapping@public String delete( RequestParam("id") @ Long id) {
Contact contact = contactDao.get(id); if (contact == null) { throw new RuntimeException("Contact not found"); } contactDao.delete(contact);
return "redirect:list.html";}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Preparing for a form
RequestMapping@public void edit( RequestParam(value = "id", @ required = false) Long id, Model model) {
Contact contact = null; if (id != null) { contact = contactDao.get(id); if (contact == null) { throw new RuntimeException("Contact not found"); } else } { contact = new Contact(); } model.addAttribute("contact", contact);
}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
WEB-INF/jsp/contact/edit.jsp
form:form commandName="< contact" method="POST" action=" cp /contact/save.html"${ } > table< > tr< > td Name: /td< > < > td< > form:input path="name"/< > /td< > /tr< > tr< > td E-mail: /td< > < > td< > form:input path="email"/< > /td< > /tr< > tr< > td /td< > < > td input type="submit" value="Submit"/ /td< >< >< > /tr< > /table< > form:errors path=" " cssClass="errorBox"/< * >/form:form< >
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Submit the form
Controller@RequestMapping( "/contact/ .html" )@ { * }SessionAttributes( "contact" )@ { }
public class ContactController {
// ... RequestMapping@ public String save( ModelAttribute("contact") Contact contact@ , SessionStatus status) {
contactDao.store(contact); status.setComplete(); return "redirect:list.html";
}}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Validation
Resource@private Validator validator;
RequestMapping@public String save( ModelAttribute("contact") Contact contact,@ BindingResult result, SessionStatus status) {
validator.validate(contact, result); if (result.hasErrors()) { return "contact/edit"; }
contactDao.store(contact); status.setComplete(); return "redirect:list.html";}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Parancoe validation
RequestMapping@Validation(view="contact/edit", continueOnErrors=false)@
public String save( ModelAttribute("contact") Contact contact,@ BindingResult result, SessionStatus status) {
contactDao.store(contact); status.setComplete(); return "redirect:list.html";}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
File upload
form:form commandName="contact" method="POST"< action=" cp /contact/save.html"${ } enctype="multipart/form-data"> table< >
!-- ... --< >
tr< > td Picture: /td< > < > td< > input type="file" name="picture" id="picture"/< > /td< > /tr< >
!-- ... --< >
/form:form< >
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Changing the binding
InitBinder@protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(byte .class,[] new ByteArrayMultipartFileEditor());
}
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Binary output
RequestMapping@public void picture( RequestParam("id") Long id, @ OutputStream os) throws IOException {
Contact contact = contactDao.get(id);
os.write(contact.getPicture()); os.flush(); os.close();
}
img src=” cp /contact/picture.html”< ${ } >
Lucio Benfante - [email protected] – JUG PadovaJavaday Roma III Edizione – 24 gennaio 2009
Questions?
?