SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Controller annotati
  con Spring MVC 2.5




        Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
                           Javaday Roma III Edizione – 24 gennaio 2009
Spring MVC




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




Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
                   Javaday Roma III Edizione – 24 gennaio 2009
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Questions?




  ?
Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova
                   Javaday Roma III Edizione – 24 gennaio 2009

Mais conteúdo relacionado

Semelhante a Annotated controllers with Spring MVC 2.5

Alfresco Forms Service Deep Dive
Alfresco Forms Service Deep DiveAlfresco Forms Service Deep Dive
Alfresco Forms Service Deep DiveAlfresco Software
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)Carles Farré
 
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity PresentationRails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentationrailsconf
 
Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010
Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010
Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010Sergey Ilinsky
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet Sagar Nakul
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax componentsIgnacio Coloma
 
JSUG - Spring by Christoph Pickl
JSUG - Spring by Christoph PicklJSUG - Spring by Christoph Pickl
JSUG - Spring by Christoph PicklChristoph Pickl
 
Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On RailsWen-Tien Chang
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSCarol McDonald
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Matt Raible
 
Struts2
Struts2Struts2
Struts2yuvalb
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...Kirill Chebunin
 
Liferay Training Struts Portlet
Liferay Training Struts PortletLiferay Training Struts Portlet
Liferay Training Struts PortletSaikrishna Basetti
 
Facebook Development with Zend Framework
Facebook Development with Zend FrameworkFacebook Development with Zend Framework
Facebook Development with Zend FrameworkBrett Harris
 
Django - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazosDjango - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazosIgor Sobreira
 
Building real-time collaborative apps with Ajax.org Platform
Building real-time collaborative apps with Ajax.org PlatformBuilding real-time collaborative apps with Ajax.org Platform
Building real-time collaborative apps with Ajax.org PlatformJaveline B.V.
 
More Secrets of JavaScript Libraries
More Secrets of JavaScript LibrariesMore Secrets of JavaScript Libraries
More Secrets of JavaScript Librariesjeresig
 
Introduction To ASP.NET MVC
Introduction To ASP.NET MVCIntroduction To ASP.NET MVC
Introduction To ASP.NET MVCAlan Dean
 
JavaScript and DOM Pattern Implementation
JavaScript and DOM Pattern ImplementationJavaScript and DOM Pattern Implementation
JavaScript and DOM Pattern Implementationdavejohnson
 

Semelhante a Annotated controllers with Spring MVC 2.5 (20)

Alfresco Forms Service Deep Dive
Alfresco Forms Service Deep DiveAlfresco Forms Service Deep Dive
Alfresco Forms Service Deep Dive
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
 
Rails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity PresentationRails 3 And The Real Secret To High Productivity Presentation
Rails 3 And The Real Secret To High Productivity Presentation
 
Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010
Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010
Building Complex GUI Apps The Right Way. With Ample SDK - SWDC2010
 
Struts,Jsp,Servlet
Struts,Jsp,ServletStruts,Jsp,Servlet
Struts,Jsp,Servlet
 
Strutsjspservlet
Strutsjspservlet Strutsjspservlet
Strutsjspservlet
 
Developing and testing ajax components
Developing and testing ajax componentsDeveloping and testing ajax components
Developing and testing ajax components
 
JSUG - Spring by Christoph Pickl
JSUG - Spring by Christoph PicklJSUG - Spring by Christoph Pickl
JSUG - Spring by Christoph Pickl
 
Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On Rails
 
Interoperable Web Services with JAX-WS
Interoperable Web Services with JAX-WSInteroperable Web Services with JAX-WS
Interoperable Web Services with JAX-WS
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2
 
Struts2
Struts2Struts2
Struts2
 
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
ZFConf 2010: Zend Framework & MVC, Model Implementation (Part 2, Dependency I...
 
Liferay Training Struts Portlet
Liferay Training Struts PortletLiferay Training Struts Portlet
Liferay Training Struts Portlet
 
Facebook Development with Zend Framework
Facebook Development with Zend FrameworkFacebook Development with Zend Framework
Facebook Development with Zend Framework
 
Django - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazosDjango - Framework web para perfeccionistas com prazos
Django - Framework web para perfeccionistas com prazos
 
Building real-time collaborative apps with Ajax.org Platform
Building real-time collaborative apps with Ajax.org PlatformBuilding real-time collaborative apps with Ajax.org Platform
Building real-time collaborative apps with Ajax.org Platform
 
More Secrets of JavaScript Libraries
More Secrets of JavaScript LibrariesMore Secrets of JavaScript Libraries
More Secrets of JavaScript Libraries
 
Introduction To ASP.NET MVC
Introduction To ASP.NET MVCIntroduction To ASP.NET MVC
Introduction To ASP.NET MVC
 
JavaScript and DOM Pattern Implementation
JavaScript and DOM Pattern ImplementationJavaScript and DOM Pattern Implementation
JavaScript and DOM Pattern Implementation
 

Último

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 

Último (20)

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 

Annotated controllers with Spring MVC 2.5

  • 1. Controller annotati con Spring MVC 2.5 Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  • 2. Spring MVC Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  • 3. The Old Style Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Questions? ? Lucio Benfante - lucio.benfante@jugpadova.it – JUG Padova Javaday Roma III Edizione – 24 gennaio 2009