Skip to content

Instantly share code, notes, and snippets.

@juliano
Created August 1, 2012 18:14
Show Gist options
  • Save juliano/3229414 to your computer and use it in GitHub Desktop.
Save juliano/3229414 to your computer and use it in GitHub Desktop.
Simplificando - Spring MVC
apply plugin: 'eclipse'
apply plugin: 'jetty'
apply plugin: 'maven'
sourceCompatibility = 1.5
targetCompatibility = 1.5
version = '0.1-SNAPSHOT'
group = 'br.com.simpledev'
repositories {
mavenCentral()
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
configurations {
provided
}
sourceSets {
main { compileClasspath += configurations.provided }
test { runtimeClasspath += configurations.provided }
}
eclipse.classpath.plusConfigurations += configurations.provided
dependencies {
compile 'commons-logging:commons-logging:1.1.1'
compile 'javax.servlet:jstl:1.2'
compile 'org.springframework:spring-asm:3.1.1.RELEASE'
compile 'org.springframework:spring-beans:3.1.1.RELEASE'
compile 'org.springframework:spring-context:3.1.1.RELEASE'
compile 'org.springframework:spring-core:3.1.1.RELEASE'
compile 'org.springframework:spring-jdbc:3.1.1.RELEASE'
compile 'org.springframework:spring-test:3.1.1.RELEASE'
compile 'org.springframework:spring-web:3.1.1.RELEASE'
compile 'org.springframework:spring-webmvc:3.1.1.RELEASE'
provided 'javax.servlet:servlet-api:2.5'
testCompile 'org.mockito:mockito-core:1.9.0'
testCompile 'junit:junit:4.10'
}
[jettyRunWar, jettyRun]*.contextPath = '/'
package br.com.simpledev.spring.controller;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import br.com.simpledev.spring.dao.CarroDao;
import br.com.simpledev.spring.model.Carro;
@Controller
@RequestMapping("/carro")
public class CarroController {
private final CarroDao dao;
@Autowired
public CarroController(final CarroDao dao) {
this.dao = dao;
}
@RequestMapping(value = "/lista", method = GET)
public List<Carro> lista() {
return dao.lista();
}
@RequestMapping(value = "/novo", method = GET)
public ModelAndView novo() {
return new ModelAndView("carro/novo", "carro", new Carro());
}
@RequestMapping(value = "/novo", method = POST)
public String novo(final Carro carro) {
dao.adiciona(carro);
return "redirect:lista";
}
@RequestMapping(value = "/editar/{id}", method = GET)
public ModelAndView editar(@PathVariable Long id) {
Carro carro = dao.busca(id);
return new ModelAndView("carro/editar", "carro", carro);
}
@RequestMapping(value = "/editar", method = PUT)
public String editar(final Carro carro) {
dao.atualiza(carro);
return "redirect:lista";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="br.com.simpledev.spring" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>SimpledDev - Spring MVC</title>
</head>
<body>
<h3>Editar Carro</h3>
<form:form action="../editar" method="put" commandName="carro">
<form:hidden path="id" />
<table>
<tr>
<td>Nome: <form:input path="nome" /></td>
<td>Ano: <form:input path="ano" /></td>
<td><input type="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>SimpledDev - Spring MVC</title>
</head>
<body>
<h3>Lista de carros</h3>
<table>
<tr>
<td>Id</td>
<td>Nome</td>
<td>Ano de fabricação</td>
</tr>
<c:forEach items="${carroList}" var="carro">
<tr>
<td>${carro.id}</td>
<td>${carro.nome}</td>
<td>${carro.ano}</td>
<td><a href="editar/${carro.id}">Editar</a></td>
</tr>
</c:forEach>
</table>
<br>
<h5>
<a href="novo">Novo</a>
</h5>
</body>
</html>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>SimpledDev - Spring MVC</title>
</head>
<body>
<h3>Novo Carro</h3>
<form:form action="novo" method="post" commandName="carro">
<table>
<tr>
<td>Nome: <form:input path="nome" /></td>
<td>Ano: <form:input path="ano" /></td>
<td><input type="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>context</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<servlet-name>context</servlet-name>
</filter-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment