jueves, 11 de noviembre de 2010

ACTIVAR SPRING MVC

incluir en el pom.xml la dependencia correspondiente spring-webmvc

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>

actualizar web.xml para que spring responda a las peticiones (request) de la aplicacion

En este caso le estamos indicando que escuche todas las peticiones del tipo http://maquina:puerto/contexto/springmvc/* y además le indicamos el fichero de configuración


<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/springmvc/*</url-pattern>
</servlet-mapping>


Si queremos que responda ante todas las peticiones, y no solo ante entradas /springmvc/* colocamos /*.


actualizar applicationContext.xml para indicarle a spring que utilzamos MVC

para ello tenemos que incluir la directiva xmlns:mvc en la cabecera con su schemaLocation


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">



la configuración será por anotaciones y todos los controladores se encuentran en el paquete es.cea.springmvc y además le indicamos que los archivos de vista son jsp y se encuentran en la carpeta raiz


<mvc:annotation-driven/>
<context:component-scan base-package="es.cea.springmvc"/>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--Directorio donde estan los .jsp por defecto en webapp. Si queremos decirle otra direccion(pero debe seguir en webapp): p.e. /WEB-INF/...-->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>


ya podemos escribir el controlador que utiliza Spring MVC


package es.cea.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ControladorSpring {

@RequestMapping(value="/helloWorldSpring", method=RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloWorldSpringMVC");
mav.addObject("message", "Hello World!");
return mav;
}

}


El cual escucha la peticion /helloWorldSpring carga el modelo identificado por la clave "message" y valor "Hello World" e indica que la vista se llama helloWorldSpringMVC (/helloWorldSpringMVC.jsp)


y la vista: helloWorldSpringMVC.jsp


<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@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>Hola SPRING MVC! </h1>
<h2>hola modelo: ${requestScope.message}</h2>

</body>

No hay comentarios:

Publicar un comentario