Controller annotati. con Spring MVC 2.5



Similar documents
Controlling Web Application Behavior

CONTROLLING WEB APPLICATION BEHAVIOR WITH

Web Service Development Using CXF. - Praveen Kumar Jayaram

Ch-03 Web Applications

Java Server Pages combined with servlets in action. Generals. Java Servlets

JSR 289: SIP Servlet 1.1 Provides Significant Benefits for SIP Application Developers

Please send your comments to:

Integration of Shibboleth and (Web) Applications

Web Container Components Servlet JSP Tag Libraries

Configuring Tomcat for a Web Application

Web Application Architecture (based J2EE 1.4 Tutorial)

Software Design Document Securing Web Service with Proxy

Creating Java EE Applications and Servlets with IntelliJ IDEA

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

How To Understand The Architecture Of Java 2Ee, J2Ee, And J2E (Java) In A Wordpress Blog Post

PowerTier Web Development Tools 4

Arjun V. Bala Page 20

Getting Started Guide. Version 1.8

Liferay Enterprise ecommerce. Adding ecommerce functionality to Liferay Reading Time: 10 minutes

CHAPTER 9: SERVLET AND JSP FILTERS

Servlet and JSP Filters

Spring 3.1 to 3.2 in a Nutshell. Sam Brannen Senior Software Consultant

Configure a SOAScheduler for a composite in SOA Suite 11g. By Robert Baumgartner, Senior Solution Architect ORACLE

Java Web Programming. Student Workbook

Handling the Client Request: Form Data

Creating A Walking Skeleton

Web Development in Java Live Demonstrations (Live demonstrations done using Eclipse for Java EE 4.3 and WildFly 8)

In this chapter the concept of Servlets, not the entire Servlet specification, is

Java Servlet 3.0. Rajiv Mordani Spec Lead

Development. with NetBeans 5.0. A Quick Start in Basic Web and Struts Applications. Geertjan Wielenga

Web Applications. For live Java training, please see training courses at

Piotr Nowicki's Homepage. Java EE 6 SCWCD Mock Exam. "Simplicity is the ultimate sophistication." Important!

Copyright 2008 The Pragmatic Programmers, LLC.

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

SSC - Web applications and development Introduction and Java Servlet (II)

SSO Plugin. Authentication service for HP, Kinetic, Jasper, SAP and CA products. J System Solutions. JSS SSO Plugin Authentication service

Principles and Techniques of DBMS 5 Servlet

Quick Setup Guide. HTML Signatures. A short guide on how to set up HTML Signatures on LetMC s. Last updated 22/11/2012

COMPUTACIÓN ORIENTADA A SERVICIOS (PRÁCTICA) Dr. Mauricio Arroqui EXA-UNICEN

Developing an EJB3 Application. on WebSphere 6.1. using RAD 7.5

Tutorial for Spring DAO with JDBC

Aspects of using Hibernate with CaptainCasa Enterprise Client

Intellicus Single Sign-on

Struts 2 - Practical examples

Building Web Services with Apache Axis2

Course Name: Course in JSP Course Code: P5

J2EE Web Development. Agenda. Application servers. What is J2EE? Main component types Application Scenarios J2EE APIs and Services.

Complete Java Web Development

By Nabil ADOUI, member of the 4D Technical Support team

PA165 - Lab session - Web Presentation Layer

Rapid application development with Apache Turbine and Maven Henning Schmiedehausen

Java Servlet Tutorial. Java Servlet Tutorial

Supporting Multi-tenancy Applications with Java EE

SPRING INTERVIEW QUESTIONS

Introduction to J2EE Web Technologies

Presentation of Enterprise Service Bus(ESB) and. Apache ServiceMix. Håkon Sagehaug

Java EE Web Development Course Program

Module Title: : Cloud Application Development

Web Applications. Originals of Slides and Source Code for Examples:

JAX-WS Developer's Guide

Building a Multi-Threaded Web Server

Portal Factory CMIS Connector Module documentation

An introduction to web programming with Java

Advanced OpenEdge REST/Mobile Security

Specialized Programme on Web Application Development using Open Source Tools

Customer Bank Account Management System Technical Specification Document

Developing WCM based WebSphere Portal application using IBM Rational Application Developer

Apache CXF Web Service Development

2. Follow the installation directions and install the server on ccc

Web Application Programmer's Guide

About ZPanel. About the framework. The purpose of this guide. Page 1. Author: Bobby Allen Version: 1.1

Java EE 6 New features in practice Part 3

SESM Tag Library. Configuring a Tag Library APPENDIX

Aspect Oriented Programming. with. Spring

WebSphere and Message Driven Beans

SSO Plugin. Authentication service for HP, Kinetic, Jasper, SAP and CA products. J System Solutions. Page 1 of 23.


Java Web Security Antipatterns

The Server.xml File. Containers APPENDIX A. The Server Container

Mobile Solutions for Data Collection. Sarah Croft and Laura Pierik

Managing Data on the World Wide-Web

Simplify Your Web App Development Using the Spring MVC Framework

What s new in Spring 3.1?

MiniBase. Custom View Tips & Tricks. Schoolwires Centricity 2.0

HDFS - Java API coreservlets.com and Dima May coreservlets.com and Dima May

Web Service Caching Using Command Cache

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Selected Topics in Java Web Application Development

Map Reduce Workflows

JWIG Yet Another Framework for Maintainable and Secure Web Applications

WRITING DATA TO A BINARY FILE

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

SDK Quick Start Guide

3 Techniques for Database Scalability with Hibernate. Geert Bevin - SpringOne 2009

Listeners se es. Filters and wrappers. Request dispatchers. Advanced Servlets and JSP

Java Web Programming with Eclipse

White Paper. JavaServer Faces, Graphical Components from Theory to Practice

Transcription:

Controller annotati con Spring MVC 2.5

Spring MVC

The Old Style

The annotated style @Controller @ RequestMapping( {"/my/*.html") public class MyController { @RequestMapping public void index() { // do something useful

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

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>

WEB-INF/javaday-servlet.xml <?xml version=" 1. 0 " encoding="utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2 001/XMLSchema-instance" 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>

Parancoe meta-framework www.parancoe.org Spring MVC + Hibernate/JPA + easy DAO +...plugins

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);

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>

More in the model @ModelAttribute( countries ) public List< Country > getcountries() { // producing and returning the list

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";

Preparing for a form @RequestMapping public void edit( @ RequestParam(value = "id", Model model) { required = false) Long id, 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);

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>

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";

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";

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";

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>

Changing the binding @InitBinder protected void initbinder(webdatabinder binder) { binder.registercustomeditor(byte [].class, new ByteArrayMultipartFileEditor());

Binary output @RequestMapping public void picture( @ RequestParam("id") Long id, throws IOException { OutputStream os) Contact contact = contactdao.get(id); os.write(contact.getpicture()); os.flush(); os.close(); < img src= ${ cp /contact/picture.html >

Questions??