..


Sponsored Links

Web - A complete example

Here is an example of a web application, we see more precisely how to implement a controller and jsp which allow you to add a book to a collection of books.

First of all, in our web.xml configure the WebApplicationContext and mapping the DispatcherServlet:






 <! - SPRING ->







 <context-param>



  



 <param-name> contextConfigLocation </ param-name>



  



 <param-value> classpath *: applicationContext.xml </ param-value>







 </ Context-param>









 <listener>



  



 <listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>







 </ Listener>









 <! - MVC ->







 <servlet>



  



 <servlet-name> spring-mvc </ servlet-name>



  



 <servlet-class> org.springframework.web.servlet.DispatcherServlet </ servlet-class>



  



 <load-on-startup> 1 </ load-on-startup>







 </ Servlet>









 <servlet-mapping>



  



 <servlet-name> spring-mvc </ servlet-name>



  



 <url-pattern> / action / * </ url-pattern>







 </ Servlet-mapping>



Without this we create our Book class:






 public class Book {





  



 @ Size (min = 1, message = "The field name can not be empty")



  



 @ Pattern (regex = "[A-Za-z0-9 _]+", message =" The field name can contain only letters ")



  



 private String name;



	

  



 @ NotNull



  



 @ Size (min = 1, message = "The author field can not be empty")



  



 @ Pattern (regex = "[A-Za-z']+", message =" The author field can contain only letters ")



  



 private String author;





  



 DateTimeFormat @ (pattern = "dd / MM / yyyy")



  



 @ NotNull (message = "The Release Date field can not be empty")



  



 private Date dataDiUscita;



	

  



 / / Getters and setters .................



  



 //..........







 }



As you can see our book has three attributes:
  • name of the book
  • author
  • release date
But we see that, in addition to defining the attributes, there are also annotations.
These annotations are part of JSR-303 , and if either of these APIs implemtazione is present in the classpath, Spring is able to validate our model.
To enable the validation you only need to add annotations to the attributes of our model and add the @ Valid next to the input of parameters by applying our controller:





 @ Controller







 {public class BookController





  



 private List <Book> Booklist;



	

  



 public BookController () {



    



 Booklist <Book> = new ArrayList ();



  



 }



	

  



 / / Show the page creation

 

  



 @ RequestMapping (value = "/ createBook" method = RequestMethod.GET)



  



 public String welcome (Model model) {



    



 model.addAttribute ("book", new Book ());



    



 return "/ book / createBook";



  



 }



	

  



 / / Check if there are validation errors and if not, the book adds to the list



  



 @ RequestMapping (value = "/ create", method = RequestMethod.POST)



  



 public String create (Book book @ Valid, BindingResult result) {



    



 if (result.hasErrors ()) {



      



 return "/ book / createBook";



    



 }



    



 bookList.add (book);



    



 return "redirect: GetView";



  



 }



	

  



 / / Show the list of books



  



 @ RequestMapping (value = "/ GetView")



  



 public String view (Model model) {



    



 model.addAttribute ("Booklist", Booklist);



    



 return "/ book / view";



  



 }







 }



we now see the two jsp:

1) WEB-INF/jsp/book/createBook.jsp






 <% @ Page language = "java" contentType = "text / html; charset = UTF-8" pageEncoding = "UTF-8"%>







 <% @ Taglib prefix = "form" uri = "http://www.springframework.org/tags/form"%>







 <! DOCTYPE html PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd">







 <html>







 <head>







 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">







 <title> Add a book </ title>







 </ Head>







 <body>







 <form:form commandName="book" method="post" action="create">



  



 <fieldset>



    



 <legend> Add a book </ legend>



    



 <p>



      



 <form:label for="nome" path="nome"> Name </ form: label> <br/>



      



 <form:input path="nome" /> <form:errors path="nome" />

			

    



 </ P>



    



 <p>

	

      



 <form:label for="autore" path="autore"> Author </ form: label> <br/>



      



 <form:input path="autore" /> <form:errors path="autore" />



    



 </ P>



    



 <p>



      



 <form:label for="dataDiUscita" path="dataDiUscita"> Creation Time </ form: label> <br/>



      



 <form:input path="dataDiUscita" /> <form:errors path="dataDiUscita" />



    



 </ P>



    



 <p>

	

      



 <input type="submit" value="Crea Libro" />



    



 </ P>



  



 </ Fieldset>







 </ Form: form>







 </ Body>







 </ Html>



2) WEB-INF/jsp/book/view.jsp





 <% @ Page language = "java" contentType = "text / html; charset = UTF-8" pageEncoding = "UTF-8"%>







 <% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c"%>









 <! DOCTYPE html PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd">







 <html>







 <head>







 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">







 <title> List of books </ title>







 </ Head>







 <body>







 <p>



  



 <a href="createBook"> Enter another book </ a>







 </ P>







 <fieldset>







 Books <legend> inserted </ legend>







 <c:forEach items="${bookList}" var="book">



  



 Name: <c:out value="${book.nome}" /> <br/>



  



 Author: <c:out value="${book.autore}" /> <br/>



  



 Year: <c:out value="${book.dataDiUscita}" /> <br/>



  



 <hr/>







 </ C: forEach>







 </ Fieldset>









 </ Body>







 </ Html>



Finally we see the contents of WEB-INF/spring-mvc-servlet.xml:






 <! - Enable Annotations ->







 <mvc:annotation-driven />









 <! - CONTROLLER ->







 <bean class="it.mrwebmaster.mvc.BookController" scope="session"/>









 <! - VIEW RESOLVER ->







 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">



  



 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />



  



 <property name="prefix" value="/WEB-INF/jsp/" />



  



 <property name="suffix" value=".jsp" />







 </ Bean>



Spring Java Guide
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links