..
Heus aquí un exemple d'una aplicació web, podem veure amb més precisió com implementar un controlador i un jsp, que li permeten afegir un llibre d'una col.lecció de llibres.
En primer lloc, en el nostre web.xml configurar el WebApplicationContext i mapatge de la DispatcherServlet:
<- PRIMAVERA -> <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> primavera-MVC </ servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </ servlet-class> <load-on-startup> 1 </ load-on-startup> </ Servlet> <servlet-mapping> <servlet-name> primavera-MVC </ servlet-name> <url-pattern> / action / * </ url-pattern> </ Servlet-mapping>
Sense que això vam crear la nostra classe Book:
public class Llibre {
@ Size (min = 1, el missatge = "El nom del camp no pot estar buit")
@ Patró (regex = "[A-Za-z0-9 _]+", message =" El nom del camp només pot contenir lletres ")
private String nom;
@ NotNull
@ Size (min = 1, el missatge = "El camp d'autor no pot estar buit")
@ Patró (regex = "[A-Za-z']+", message =" El camp d'autor només pot contenir lletres ")
autor de cadenes privades;
DateTimeFormat @ (patró = "dd / MM / yyyy")
@ NotNull (message = "El camp de data de llançament no pot estar buit")
Data dataDiUscita privat;
/ / Getters i setters .................
//..........
}
Com vostè pot veure el nostre llibre té tres atributs:
@ Controller
{Public BookController classe
Llista privada Booklist <Book>;
pública BookController () {
Llista de llibres <Book> = new ArrayList ();
}
/ / Mostra la creació de la pàgina
@ RequestMapping (value = "/ createBook" method = RequestMethod.GET)
public String benvinguda (model tipus) {
model.addAttribute ("llibre", nou llibre ());
return "/ llibre / createBook";
}
/ / Comprovar si hi ha errors de validació i si no, el llibre se suma a la llista
@ RequestMapping (value = "/ create", method = RequestMethod.POST)
public String crear (llibre Book @ vàlids resultat, BindingResult) {
if (result.hasErrors ()) {
return "/ llibre / createBook";
}
bookList.add (llibre);
return "redirect: GetView";
}
/ / Mostra la llista de llibres
@ RequestMapping (value = "/ GetView")
Veure public String (model de models) {
model.addAttribute ("Llista de llibres", llista de llibres);
return "/ llibre / vista";
}
}
ara veiem els dos jsp:
1) WEB-INF/jsp/book/createBook.jsp
<% @ Page language = "java" contentType = "text / html; charset = UTF-8" pageEncoding = "UTF-8"%>
<% @ Taglib prefix = "forma" 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> Afegir un llibre </ title>
</ Head>
<body>
<form:form commandName="book" method="post" action="create">
<fieldset>
<legend> Afegir un llibre </ legend>
<p>
<form:label for="nome" path="nome"> Nom </ form: label> <br/>
<form:input path="nome" /> <form:errors path="nome" />
</ P>
<p>
<form:label for="autore" Autor path="autore"> </ form: label> <br/>
<form:input path="autore" /> <form:errors path="autore" />
</ P>
<p>
<form:label for="dataDiUscita" Moment de la creació path="dataDiUscita"> </ 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> Llista de llibres </ title>
</ Head>
<body>
<p>
href = "createBook"> <a Introduïu un altre llibre </ a>
</ P>
<fieldset>
Llibres <legend> s'insereix </ legend>
<c:forEach items="${bookList}" var="book">
Nom: <c:out value="${book.nome}" /> <br/>
Autor: <c:out value="${book.autore}" /> <br/>
Any: <c:out value="${book.dataDiUscita}" /> <br/>
<hr/>
</ C: foreach>
</ Fieldset>
</ Body>
</ HTML>
Finalment podem veure el contingut de WEB-INF/spring-mvc-servlet.xml:
<- Activar Anotacions -> <mvc:annotation-driven /> <- CONTROL -> <bean class="it.mrwebmaster.mvc.BookController" scope="session"/> <- VISTA AL RESOLDRE -> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> propietat> name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" /> propietat> name = "prefix" value = "/ WEB-INF/jsp /" /> propietat> name = "suffix" value = ". jsp" /> </ Bean>
| |
Linux (del curs)
Guia completa de codi obert del sistema. A partir de 49 €. |
| |
PHP (Curs)
Cicle complet per a la creació de llocs web dinàmics. A partir de 49 €. |
| |
Ruby i Ruby on Rails (Curs)
Crear aplicacions de programari i la web amb Ruby i RoR. A partir de 39 €. |