si, ne sono convinto... ma si tratta di un esercizio (tratta di Servlet, JSP, JSTL ... niente framework avanzati come struts...) e in più l'intenzione di sottolineare il perchè di certi "controsensi"...
tornando al discorso..
ho questo esempio di validazione che è fatto sulla stessa pagina.. con le tecnologie che conosco (quelle sopra):
e come si vede... tutto va fatto on-Page... che "dovrebbe" essere un controsenso nel rigore di MVC...
Il fatto è che con queste tecnologie non si può fare altrimenti?!?! (senza perdere i parametri, gia inputati dall utente). E ho in mente solo di passare i parametri da controllare da una pagina ad un altra.. ma mi pare stupido.
Codice PHP:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:if test="${param.submitted}">
<c:if test="${empty param.name}" var="noName"/>
<c:if test="${empty param.email}" var="noEmail"/>
<c:if test="${empty param.age}" var="noAge"/>
<c:catch var="ageError">
<fmt:parseNumber var="parsedAge" value="${param.age}"/>
<c:if test="${parsedAge < 13}" var="youngAge"/>
</c:catch>
<c:if test="${not empty ageError}" var="badAge"/>
<c:if test="${not (noName or noEmail or noAge or badAge or youngAge)}">
<c:set value="${param.name}" var="name" scope="request"/>
<c:set value="${param.email}" var="email" scope="request"/>
<c:set value="${param.age}" var="age" scope="request"/>
<jsp:forward page="pass.jsp"/>
</c:if>
</c:if>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>JSP Page</title>
</head>
<body>
<h1>Error Handling</h1>
<form method="post" action="index.jsp" name="input">
Grazie per non si sa cosa.</p>
<input type="hidden" name="submitted" value="true"/>
Il tuo nome: <input type="text" name="name" value="<c:out value='${param.name}'/>"/>
<c:if test="${noName}">
[size="1"]<font color="red">Devi scrivere un nome</font>[/size]
</c:if>
</p>
La tua mail: <input type="text" name="email" value="<c:out value='${param.email}'/>"/>
<c:if test="${noEmail}">
[size="1"]<font color="red">Devi scrivere una mail</font>[/size]
</c:if>
</p>
La tua Età: <input type="text" name="age" size="3" value="<c:out value='${param.age}'/>"/>
<c:choose>
<c:when test="${noAge}">
[size="1"]<font color="red">Devi scrivere l'età</font>[/size]
</c:when>
<c:when test="${badAge}">
[size="1"]<font color="red">Età non corretta</font>[/size]
</c:when>
<c:when test="${youngAge}">
[size="1"]<font color="red">Sei troppo giovane</font>[/size]
</c:when>
</c:choose>
</p>
<input type="submit" value="Invia"/>
</form>
</body>
</html>