Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Aula 5 - Redirect com Escopo de Flash

olá!!

Não estou conseguindo realizar corretamente o redirect, já alterei diversas vezes o RequestMapping, porém, não consigo mais cadastrar (coisa que fazia antes) e nem redirecionar, poderiam me ajudar por favor?

de vez de produto é aluno, a pasta produtos renomeei para a pasta projeto

obrigada!!

AlunoController.java
package br.com.projeto.cuidandodaatencao.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import br.com.projeto.cuidandodaatencao.dao.AlunoDAO;
import br.com.projeto.cuidandodaatencao.model.Aluno;

@RequestMapping("projeto")
@Controller
public class AlunoController {

    @Autowired /* spring ejetar o DAO */
    private AlunoDAO alunoDAO; 


    @RequestMapping("/CadastroAluno")    
     public ModelAndView form(){
            ModelAndView modelAndView = new ModelAndView("projeto/CadastroAluno");

        return modelAndView;
    }

    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView gravar(Aluno aluno, RedirectAttributes redirectAttributes) {
        System.out.println(aluno);
        alunoDAO.gravar(aluno);

        redirectAttributes.addFlashAttribute("sucesso", "Aluno Cadastrado");

        return new ModelAndView("redirect:projeto");

        }

       @RequestMapping(method=RequestMethod.GET)
        public ModelAndView listar() {
           List<Aluno> alunos = alunoDAO.listar();
           ModelAndView modelAndView = new ModelAndView("projeto/ListaDeAlunos");
          modelAndView.addObject("alunos", alunos);

            return modelAndView;
        }


}
ListaDeAlunos.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cuidando da Atenção</title>
</head>
<body>
    <h1>Lista de Alunos</h1>
    <div>${sucesso }</div>
    <table>
        <tr>
            <td>Nome</td>
            <td>Idade</td>
            <td>Serie</td>
        </tr>

        <c:forEach items="${alunos}" var="aluno">
            <tr>
                <td>${aluno.nome}</td>
                <td>${aluno.idade}</td>
                <td>${aluno.serie}</td>
            </tr> 
        </c:forEach>
    </table>
</body>
</html>
3 respostas

Olá Lilian tudo bem,

Poderia enviar o arquivo CadastroAluno.jsp para que eu continuar a analise?

segue: Obrigada!!

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cuidando da Atenção</title>
</head>
<body>
    <form action="/projetocuidandodaatencao/projeto/CadastroAluno" method="POST">
       <div>
            <label>Matricula</label>
            <input type="text" name="matricula">
        </div>
        <div>
            <label>Nome do Aluno</label>
            <input type="text" name="nome">
        </div>

        <div>
            <label>senha</label>
            <input type="text" name="senha">
        </div>

         <div>
            <label>idade</label>
            <input type="text" name="idade">
        </div>

         <div>
            <label>serie atual</label>
            <input type="text" name="serie">
        </div>

         <div>
            <label>sexo</label>
            <input type="text" name="sexo">
        </div>


        <button type="submit">
            Cadastrar
        </button>
    </form>
</body>
</html>
solução!

Consegui ajuda em outro tópico.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software