class HelloControllerTests extends TestCase { public void testHandleRequestView() throws Exception{ HelloController controller = new HelloController(); ModelAndView modelAndView = controller.handleRequest(null, null); assertEquals("WEB-INF/jsp/hello.jsp", modelAndView.getViewName()); assertNotNull(modelAndView.getModel()); String nowValue = (String) modelAndView.getModel().get("now"); assertNotNull(nowValue); }}Agora vamos executar o teste Ant 'tests'.$ ant testsBuildfile: build.xmlbuild:buildtests: [javac] Compiling 1 source file to /home/trisberg/workspace/springapp/war/WEB-INF/classestests: [junit] Running springapp.web.HelloControllerTests [junit] Testsuite: springapp.web.HelloControllerTests [junit] Oct 31, 2007 1:27:10 PM springapp.web.HelloController handleRequest [junit] INFO: Returning hello view [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.046 sec [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.046 sec [junit] [junit] ------------- Standard Error ----------------- [junit] Oct 31, 2007 1:27:10 PM springapp.web.HelloController handleRequest [junit] INFO: Returning hello view [junit] ------------- ---------------- --------------- [junit] Testcase: testHandleRequestView(springapp.web.HelloControllerTests): FAILED [junit] expected:<[WEB-INF/jsp/]hello.jsp> but was:<[]hello.jsp> [junit] junit.framework.ComparisonFailure: expected:<[WEB-INF/jsp/]hello.jsp> but was:<[]hello.jsp> [junit] at springapp.web.HelloControllerTests.testHandleRequestView(HelloControllerTests.java:14) [junit] [junit] [junit] Test springapp.web.HelloControllerTests FAILEDBUILD FAILED/home/trisberg/workspace/springapp/build.xml:101: tests.failed=true *********************************************************** *********************************************************** **** One or more tests failed! Check the output ... **** *********************************************************** ***********************************************************Total time: 2 secondsAgora vamos atualizar HelloController configurando o recurso da view referente a nova localizacao 'WEB-INF/jsp/hello.jsp' bem como definir o part key/value para a data/hora corrente no modelo com a chave de identificacao: "now" e o valor: 'now'.'springapp/src/springapp/web/HelloController.java':package springapp.web;import org.springframework.web.servlet.mvc.Controller;import org.springframework.web.servlet.ModelAndView;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.io.IOException;import java.util.Date;public class HelloController implements Controller { protected final Log logger = LogFactory.getLog(getClass()); public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String now = (new Date()).toString(); logger.info("Returning hello view with " + now); return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now); }}Execute o ant 'tests' para verificar se esta tudo ok.Lembre-se que o Controller ja foi configurado no arquivo 'springapp-servlet.xml', entao estamos prontos para experimentar os nossos aprimoramentos depois de contruimos e implantarmos esse novo codigo. Quando digitar http://localhost:8080/springapp/ no browser, ele deve subir o arquivo de boas-vindas 'index.jsp', que deve redirecionar para 'hello.htm' e e tratado pelo DispatcherServlet, que envia o pedido para o controller que adiciona na pagina data/hora e, em seguida, torna o modelo disponivel para a view 'hello.jsp'.Aplicacao atualizada2.3. Dissociar a view do controllerNeste momento, o controller especifica o caminho completo da view, o que cria uma dependencia desnecessaria entre o controller e a view. O ideal e mapear a view usando um nome logico, o que nos permite mudar a view sem ter que alterar o controller. Voce pode configurar este mapeamento em um arquivo de propriedades se quiser usar ResourceBundleViewResolver e aSimpleUrlHandlerMapping class. Para um mapeamento basico da view para uma localizacao, basta definir um prefixo e um sufixo em InternalResourceViewResolver. Esta segunda abordagem e a que vamos implementar agora, para isso modifique o 'springapp-servlet.xml' declarando a entrada 'viewResolver'. Ao escolher JstlView, podemos utilizar JSTL em combinacao com pacotes de recursos de mensagens, nos fornecendo tambem apoio a internacionalizacao.'springapp/war/WEB-INF/springapp-servlet.xml':<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- the application context definition for the springapp DispatcherServlet --> <bean name="/hello.htm" class="springapp.web.HelloController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>Atualize o nome da view na classe de teste HelloControllerTests para 'hello' e execute novamente para verificar a falha.'springapp/test/springapp/web/HelloControllerTests.java':package springapp.web;import org.springframework.web.servlet.ModelAndView;import springapp.web.HelloController;import junit.framework.TestCase;public class HelloControllerTests extends TestCase { public void testHandleRequestView() throws Exception{ HelloController controller = new HelloController(); ModelAndView