Controller annotati
  con Spring MVC 2.5




        Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
           ...
Spring MVC




Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
                   Javaday Roma III Edizione – 24...
The Old Style




Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
                   Javaday Roma III Edizione –...
The annotated style



@Controller
@RequestMapping({quot;/my/*.htmlquot;})
public class MyController {

    @RequestMappin...
Configuration




No, thanks!
  (at least while I’m writing the code)




         Lucio Benfante - lucio.benfante@jugpado...
WEB-INF/web.xml
<web-app>
   <display-name>JavaDay 2009 Demo Web Application</display-name>

   <!--
   <context-param>
  ...
WEB-INF/javaday-servlet.xml

<?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
<beans xmlns=quot;http://www.springfra...
Parancoe meta-framework




www.parancoe.org
  Spring MVC + Hibernate/JPA + easy DAO + ...plugins




                 Luc...
Using the model


@Controller
@RequestMapping(quot;/contact/*.htmlquot;)
public class ContactController {

    @Resource
 ...
WEB-INF/jsp/contact/list.jsp


<table>
 <c:forEach var=quot;contactquot; items=quot;${contacts}quot;>
   <tr>
     <td>${c...
More in the model




@ModelAttribute(“countries”)
public List<Country> getCountries() {

    // producing and returning t...
Getting parameters



@RequestMapping
public String delete(@RequestParam(quot;idquot;) Long id) {

    Contact contact = c...
Preparing for a form

@RequestMapping
public void edit(
   @RequestParam(value = quot;idquot;, required = false) Long id,
...
WEB-INF/jsp/contact/edit.jsp

<form:form commandName=quot;contactquot; method=quot;POSTquot;
      action=quot;${cp}/conta...
Submit the form

@Controller
@RequestMapping({quot;/contact/*.htmlquot;})
@SessionAttributes({quot;contactquot;})
public c...
Validation

@Resource
private Validator validator;


@RequestMapping
public String save(@ModelAttribute(quot;contactquot;)...
Parancoe validation



@RequestMapping
@Validation(view=quot;contact/editquot;, continueOnErrors=false)
public String save...
File upload

<form:form commandName=quot;contactquot; method=quot;POSTquot;
      action=quot;${cp}/contact/save.htmlquot;...
Changing the binding




@InitBinder
protected void initBinder(WebDataBinder binder) {

    binder.registerCustomEditor(by...
Binary output



@RequestMapping
public void picture(@RequestParam(quot;idquot;) Long id, OutputStream os)
    throws IOEx...
Questions?




  ?
Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
                   Javaday Roma III Edizione ...
Upcoming SlideShare
Loading in...5
×

Annotated controllers with Spring MVC 2.5

2,931

Published on

How to write controller using Java 5 annotations with Spring MVC 2.5.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,931
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
185
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Annotated controllers with Spring MVC 2.5

  1. 1. Controller annotati con Spring MVC 2.5 Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  2. 2. Spring MVC Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  3. 3. The Old Style Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  4. 4. The annotated style @Controller @RequestMapping({quot;/my/*.htmlquot;}) public class MyController { @RequestMapping public void index() { // do something useful } } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  5. 5. Configuration No, thanks! (at least while I’m writing the code) Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  6. 6. WEB-INF/web.xml <web-app> <display-name>JavaDay 2009 Demo Web Application</display-name> <!-- <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>1</load-on-startup> </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 - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  7. 7. WEB-INF/javaday-servlet.xml <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <beans xmlns=quot;http://www.springframework.org/schema/beansquot; xmlns:xsi=quot;http://www.w3.org/2001/XMLSchema-instancequot; xmlns:context=quot;http://www.springframework.org/schema/contextquot; xmlns:p=quot;http://www.springframework.org/schema/pquot; xsi:schemaLocation=quot;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.xsdquot;> <context:component-scan base-package=quot;it.jugpadova.javaday.controllerquot;/> <!-- <bean class=quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterquot;> <property name=quot;webBindingInitializerquot;> <bean class=quot;it.jugpadova.javaday.JavadayBindingInitializerquot;/> </property> </bean> --> <bean id=quot;viewResolverquot; class=quot;org.springframework.web.servlet.view.InternalResourceViewResolverquot; p:prefix=quot;/WEB-INF/jsp/quot; p:suffix=quot;.jspquot; /> </beans> Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  8. 8. Parancoe meta-framework www.parancoe.org Spring MVC + Hibernate/JPA + easy DAO + ...plugins Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  9. 9. Using the model @Controller @RequestMapping(quot;/contact/*.htmlquot;) public class ContactController { @Resource private ContactDao contactDao; @RequestMapping public void list(Model model) { List<Contact> contacts = contactDao.findAll(); model.addAttribute(quot;contactsquot;, contacts); } } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  10. 10. WEB-INF/jsp/contact/list.jsp <table> <c:forEach var=quot;contactquot; items=quot;${contacts}quot;> <tr> <td>${contact.name}</td> <td>${contact.email}</td> <td> <a href=quot;edit.html?id=${contact.id}quot;>Edit</a> <a href=quot;delete.html?id=${contact.id}quot;>Delete</a> </td> </tr> </c:forEach> </table> <c:if test=quot;${empty contacts}quot;>No contacts in the DB</c:if> <a href=quot;edit.htmlquot;>New</a> Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  11. 11. More in the model @ModelAttribute(“countries”) public List<Country> getCountries() { // producing and returning the list } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  12. 12. Getting parameters @RequestMapping public String delete(@RequestParam(quot;idquot;) Long id) { Contact contact = contactDao.get(id); if (contact == null) { throw new RuntimeException(quot;Contact not foundquot;); } contactDao.delete(contact); return quot;redirect:list.htmlquot;; } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  13. 13. Preparing for a form @RequestMapping public void edit( @RequestParam(value = quot;idquot;, required = false) Long id, Model model) { Contact contact = null; if (id != null) { contact = contactDao.get(id); if (contact == null) { throw new RuntimeException(quot;Contact not foundquot;); } } else { contact = new Contact(); } model.addAttribute(quot;contactquot;, contact); } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  14. 14. WEB-INF/jsp/contact/edit.jsp <form:form commandName=quot;contactquot; method=quot;POSTquot; action=quot;${cp}/contact/save.htmlquot;> <table> <tr> <td>Name:</td> <td><form:input path=quot;namequot;/></td> </tr> <tr> <td>E-mail:</td> <td><form:input path=quot;emailquot;/></td> </tr> <tr> <td>&nbsp;</td> <td><input type=quot;submitquot; value=quot;Submitquot;/></td> </tr> </table> <form:errors path=quot;*quot; cssClass=quot;errorBoxquot;/> </form:form> Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  15. 15. Submit the form @Controller @RequestMapping({quot;/contact/*.htmlquot;}) @SessionAttributes({quot;contactquot;}) public class ContactController { // ... @RequestMapping public String save( @ModelAttribute(quot;contactquot;) Contact contact, SessionStatus status) { contactDao.store(contact); status.setComplete(); return quot;redirect:list.htmlquot;; } } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  16. 16. Validation @Resource private Validator validator; @RequestMapping public String save(@ModelAttribute(quot;contactquot;) Contact contact, BindingResult result, SessionStatus status) { validator.validate(contact, result); if (result.hasErrors()) { return quot;contact/editquot;; } contactDao.store(contact); status.setComplete(); return quot;redirect:list.htmlquot;; } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  17. 17. Parancoe validation @RequestMapping @Validation(view=quot;contact/editquot;, continueOnErrors=false) public String save(@ModelAttribute(quot;contactquot;) Contact contact, BindingResult result, SessionStatus status) { contactDao.store(contact); status.setComplete(); return quot;redirect:list.htmlquot;; } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  18. 18. File upload <form:form commandName=quot;contactquot; method=quot;POSTquot; action=quot;${cp}/contact/save.htmlquot; enctype=quot;multipart/form-dataquot;> <table> <!-- ... --> <tr> <td>Picture:</td> <td><input type=quot;filequot; name=quot;picturequot; id=quot;picturequot;/></td> </tr> <!-- ... --> </form:form> Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  19. 19. Changing the binding @InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); } Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  20. 20. Binary output @RequestMapping public void picture(@RequestParam(quot;idquot;) 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 - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  21. 21. Questions? ? Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×