Projeto com Spring MVC 4.1

Projeto feito em JSP e Spring MVC4.1 gravando, listando e excluindo.

As libs do Projeto são : JSF 2.0 , Primefaces 4.0 , Oracle necessárias são:

  • aopalliance-1.0.jar
  • commons-logging-1.1.3.jar
  • jcl-over-slf4j-1.6.1.jar
  • jstl-api-1.2.jar
  • jstl-impl-1.2.jar
  • log4j-1.2.16.jar
  • mysql-connector-java-5.1.15.jar
  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.1.jar
  • spring-aop-4.1.0.RELEASE.jar
  • spring-beans-4.1.0.RELEASE.jar
  • spring-context-4.1.0.RELEASE.jar
  • spring-core-4.1.0.RELEASE.jar
  • spring-expression-4.1.0.RELEASE.jar
  • spring-web-4.1.0.RELEASE.jar
  • spring-webmvc-4.1.0.RELEASE.jar

Pessoa.java
package entity;

import java.io.Serializable;

public class Pessoa implements Serializable{

	private static final long serialVersionUID = 1L;	
	
	private Integer idPessoa;
	private String nome;
	private String email;
	
	public Pessoa() {
	
	}

	public Pessoa(Integer idPessoa, String nome, String email) 	{
		super();
		this.idPessoa = idPessoa;
		this.nome = nome;
		this.email = email;
	}

	@Override
	public String toString() {
		return "Pessoa [idPessoa=" + idPessoa + ", nome=" + nome + ", email=" + email + "]";
	}

	public Integer getIdPessoa() {
		return idPessoa;
	}
	public void setIdPessoa(Integer idPessoa) {
		this.idPessoa = idPessoa;
	}
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}   
				
script.sql
drop database if exists spring;

create database  spring;

use spring;

	create table pessoa(
		idPessoa int primary key auto_increment ,
		nome varchar (50) not null,
		email varchar (50) not null unique
		);
	
	insert into pessoa values (null,'luis','luis@gmail.com');
	insert into pessoa values (null,'marcos','marcos@gmail.com');
	insert into pessoa values (null,'lu','lu@gmail.com');
	insert into pessoa values (null,'caio','caio@gmail.com');
	
	commit; 

	select * from pessoa;
							
PessoaController.java
package controller;

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

import entity.Pessoa;
import persistence.PessoaDao;

@Controller
public class PessoaController {
	
@RequestMapping(value="/",method=RequestMethod.GET)
		public ModelAndView iniciar(){
		return new ModelAndView("index");
		}
	
@RequestMapping(name="pessoa_cadastrar", value="/cadastrar.html", method=RequestMethod.POST)
	public ModelAndView cadastrar(Pessoa p){	   
		ModelAndView mv = new ModelAndView("index"); //arquivo
		try{
			new  PessoaDao().create(p);
			mv.addObject("msg","Dados Gravados Pessoa !!!");
		}catch(Exception ex){
			ex.printStackTrace();
			mv.addObject("msg","Error :" + ex.getMessage());
		} 
			return mv;
	}
	

@RequestMapping(name="pessoa_listar",value="/listar.html",
			method=RequestMethod.GET	) 
	public ModelAndView listar(){
		ModelAndView mv = new ModelAndView("listar"); //arquivo
		try{
			mv.addObject("lst",  new PessoaDao().findAll());
		}catch(Exception ex){
		ex.printStackTrace();
		} 
			return mv;
	}


@RequestMapping(value="/excluir/{id}", method=RequestMethod.GET)
	public ModelAndView exluir(@PathVariable Integer id){
		ModelAndView mv = new ModelAndView("listar"); //arquivo
		try{
			new PessoaDao().delete(id); 
//apagando a pessoa pelo codigo
			mv.addObject("lst",  new PessoaDao().findAll());
//carrega lista
			mv.addObject("msg", "Registro Excluido");
		}catch(Exception ex){
	ex.printStackTrace();
		} 
			return mv;
	}
} 
					
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
			http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>Servlet 3.0 Web Application</display-name>

	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/application-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file></welcome-file>
	</welcome-file-list>

</web-app>  
application-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation=" http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/data/jpa
	http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
	>
	
	<context:component-scan base-package="controller"></context:component-scan>
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/pages/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	
</beans>    
							
index.jsp
<!Doctype html>
<html>

<head>

<link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

</head>

<body>
<div class="container-fluid">

	<div class="well">
		<h1>Projeto Spring MVC</h1>
		<a href="listar.html">Listar Pessoas</a>
	</div>
	
	<div class="col-md-6 col-md-offset-3">
		<div class="panel panel-default">
			<div class="panel-heading">
				<div class="panel-title">
					Cadastrar Pessoa Sring MVC
				</div>
			</div>
			
			<div class="panel-body">
				<form  action="cadastrar.html" method="post" role="form">
						<label for="nome">Nome</label>
						<input 	type="text" name="nome" id="nome" value="" 
								required="required" size="35" class="form-control"
								placeholder="Digite o Nome" title="Digite o nome"/>
						
						<label for="email">Email</label>
						<input 	type="email" name="email" id="email" value=""
								required="required" size="35"  class="form-control"
								placeholder="Digite o email" title="Digite o email"/>
						<br/>      
						<button type="submit" class="btn btn-info btn-xs">Gravar os Dados</button>
				</form>			
			</div>
			
			<div class="panel-footer">
				${msg}			
			</div>
		</div>
	</div>
</div>
</body>
</html>   
					

Rodando index

Cadastrando

listar.jsp

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet"	href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet"	href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<script type="text/javascript"	src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type="text/javascript"	src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript"	src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

</head>
<body>
	<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
	<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

	<div class="container-fluid">

		<div class="well">
			<h1>Projeto Spring MVC</h1>
			<a href="/Aula16_springmvcp1/">Voltar para Principal</a>
		</div>

		<div class="col-md-6 col-md-offset-3">
			<div class="panel panel-default">
				<div class="panel-heading">
					<div class="panel-title">
						Listar Pessoas
					</div>
				</div>
				
				<div class="panel-body">
					<c:if test="${fn:length(lst) >0 }">
						<table class="table table-hover table-striped">
							<thead>
								<tr>
									<th>IdPessoa</th>
									<th>Nome</th>
									<th>Email</th>
									<th>Excluir</th>
								</tr>
							</thead>
							<c:forEach items="${lst}" var="linha">
								<tr>
									<td>${linha.idPessoa}</td>
									<td>${linha.nome}</td>
									<td>${linha.email}</td>
									<td class="text-center">
										<a href="/Aula16_springmvcp1/excluir/${linha.idPessoa}" title="Deseja Excluir?">
											<i class="fa fa-trash"></i>
										</a>
									</td>
								</tr>
							</c:forEach>
						</table>
					</c:if>				
				</div>
				
				<div class="panel-footer">
					${msg}
				</div>
			</div>
		</div> 
	</div>
</body>

</html>
					

Rodando a lista..

Excluindo Registro..