Comece arrumando um eclipse kepler pra programar. Leia este artigo para ajeitar um.
A principal mudança do que vimos antes sobre spring para o que veremos agora é o estímulo cada vez mais forte da adoção do maven.
Façamos portanto um projeto maven de acordo com o que vemos no print de tela:
- groupid: sample
- Artifact Id: springwebmvc
Abra o arquivo pom.xml do projeto recém-criado e adicione o texto copiado:
Caso você não esteja familiarizado com o maven, leia isso aqui depois. O importante é que ele resolve as dependências do projeto por você.
Como o build é automático, logo o seu eclipse vai ficar com as bibliotecas maven assim:
Agora siga para a aba Servers. Vamos criar um tomcat:
A versão que usaremos é a 7.0:
O eclipse oferece a opção de baixar o tomcat na hora:
Aceite o contrato:
Agora crie uma pasta:
Ele irá dizer que deu erro, mas só enquanto dura o download. Aguarde:
Quando o erro sumir, avance e adicione o projeto ao tomcat:
E seu workspace deve ficar assim:
Agora retorne ao pom.xml e adicione a dependência do spring-webmvc:
Em seguida crie a pasta WEB-INF dentro de webapp em Deployed Resources:
Nesta pasta crie o applicationContext.xml:
Este xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="sample.springwebmvc" />
<context:annotation-config />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- set the path to jsps -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000000"/>
</bean>
</beans>
Adicione também o arquivo web.xml:Este xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springwebmvc</display-name>
<description>springwebmvc</description>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
Agora crie o controlador de exemplo:
Esta classe:
package sample.springwebmvc;
import java.sql.Timestamp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class Hello {
@RequestMapping("/hello")
public ModelAndView hello() {
return new ModelAndView("hello")//
.addObject("time", new Timestamp(System.currentTimeMillis()));
}
}
Adicione uma pasta chamada views dentro de WEB-INF, lá adicione um arquivo jsp chamado hello.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${time}</h1>
</body>
</html>
Não esqueça de adicionar também o suporte a upload de arquivo no pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>sample</groupId> <artifactId>springwebmvc</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3</version> </dependency> </dependencies> </project>
Feito. ao executar seu projeto o resultado deve ser similar a este:
Num momento posterior vamos detalhar um pouco mais o que podemos fazer com o spring-webmvc. Se você precisava sair do zero, parabéns.
Até a próxima.



















Nenhum comentário :
Postar um comentário