Problem in the Controller JSP class

0

Hello, I need help, I have the problem on my system when I send an edit command. When I click the button to edit the table row I want, the screen turns white and does not show the options to edit. In the URL it shows that the fields are passing correctly, but the screen does not compile.

MyJSPislikethis,whenIclicktheeditbuttonitsendsanactiontotheclassOfficialListController.

<thead><tr><th>ID</th><th>Nome</th><th>RG</th><th>CPF</th><th>Cargo</th><th>Setor</th></tr></thead><tbody><c:forEachitems="${funcionarios}" var="funcionario">

                                <tr>


                                    <td class="center"><c:out value="${funcionario.id}" /></td>
                                    <td class="center"><c:out value="${funcionario.nome}" /></td>
                                    <td class="center"><c:out value="${funcionario.rg}" /></td>
                                    <td class="center"><c:out value="${funcionario.cpf}" /></td>
                                    <td class="center"><c:out value="${funcionario.cargo}" /></td>
                                    <td class="center"><c:out value="${funcionario.sigla_setor}" /></td>

                                    <td class="center"><a class="btn btn-info"
                                        href="FuncionarioListaController?action=editar&id=<c:out value="${funcionario.id}"/>">
                                            <i class="glyphicon glyphicon-edit icon-white"></i> Editar
                                    </a> <a class="btn btn-danger"
                                        href="FuncionarioListaController?action=delete&id=<c:out value="${funcionario.id}"/>">
                                            <i class="glyphicon glyphicon-trash icon-white"></i>
                                            Deletar
                                    </a></td>
                                </tr>
                                <tr>

                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>

The OfficialListController class looks like this:

@WebServlet("/FuncionarioListaController")
public class FuncionarioListaController extends HttpServlet{
private static final long serialVersionUID = 1L;
public static final String FUNCIONARIO_EDITADO = "/FuncionarioEditado.jsp";
public static final String EDITAR = "/FuncionarioEditar.jsp";
public static final String TABELA = "/VerFuncionario.jsp";
private FuncionarioDAO funcionarioDAO;
private Funcionario funcionario;

/**
* @throws IllegalAccessException
* @throws InstantiationException
* @see HttpServlet#HttpServlet()
*/
public FuncionarioListaController() throws InstantiationException, 
IllegalAccessException {
super();
funcionarioDAO = new FuncionarioDAO();
funcionario = new Funcionario();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse 
response)
    throws ServletException, IOException {

String forward = "";
String action = request.getParameter("action");
try {
if (action.equalsIgnoreCase("delete")) {
    forward = TABELA;
    Integer id = Integer.parseInt(request.getParameter("id"));
    funcionarioDAO.deletarFuncionario(id);
    request.setAttribute("funcionarios", funcionarioDAO.todosFuncionarios());
} else if (action.equalsIgnoreCase("editar")) {
    forward = EDITAR;
    Integer id = Integer.parseInt(request.getParameter("id"));
    Funcionario funcionario = funcionarioDAO.buscarFuncionarioId(id);

    funcionarioDAO.atualizarFuncionario(funcionario);
    request.setAttribute("funcionario", funcionario);
}

else {
    forward = TABELA;
    request.setAttribute("funcionarios", funcionarioDAO.todosFuncionarios());
}
RequestDispatcher view = request.getRequestDispatcher(forward);
view.forward(request, response);
}catch (Exception e) {
    // TODO: handle exception
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
    throws ServletException, IOException {

funcionario.setNome(request.getParameter("nome"));
funcionario.setRg(request.getParameter("rg"));
funcionario.setCpf(request.getParameter("cpf"));
funcionario.setCargo(request.getParameter("cargo"));
funcionario.setSigla_setor(request.getParameter("sigla_setor"));

Integer idFuncionario = Integer.parseInt(request.getParameter("id"));

String id = Integer.toString(idFuncionario);
try {
if (id == null || id.isEmpty()) {
    funcionarioDAO.adicionarFuncionario(funcionario);
} else {
    funcionario.setId(Integer.parseInt(id));
    funcionarioDAO.atualizarFuncionario(funcionario);
}

RequestDispatcher view = request.getRequestDispatcher(FUNCIONARIO_EDITADO);
request.setAttribute("funcionarios", funcionarioDAO.todosFuncionarios());
view.forward(request, response);
}catch (Exception e) {
// TODO: handle exception
}
}

The DAO employee looks like this:

@Override
public void atualizarFuncionario(Funcionario funcionario) throws 
ClassNotFoundException, SQLException{
try(Connection conn = ConnectionSQL.conectar()) {
    String query = "UPDATE Funcionario "
            + "SET nome=?, rg=?, cpf=?, cargo=?, sigla_setor=? "
            + "WHERE id=?";

    PreparedStatement preparedStatement = conn.prepareStatement(query);
    preparedStatement.setString(1, funcionario.getNome());
    preparedStatement.setString(2, funcionario.getRg());
    preparedStatement.setString(3, funcionario.getCpf());
    preparedStatement.setString(4, funcionario.getCargo());
    preparedStatement.setString(5, funcionario.getSigla_setor());
    preparedStatement.setInt(6, funcionario.getId());
    preparedStatement.executeUpdate();
    preparedStatement.close();
} catch (SQLException e) {
    e.printStackTrace();
}

}

@Override
public List<Funcionario> todosFuncionarios() throws ClassNotFoundException, 
SQLException {
List<Funcionario> funcionarios = new ArrayList<>();
Connection conn = ConnectionSQL.conectar();
String query = "SELECT id, nome, rg, cpf, cargo, sigla_setor FROM 
Funcionario";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
    funcionarios.add(resultSetToFuncionario(resultSet));
}

return funcionarios;
}


 @Override
 public Funcionario buscarFuncionarioId(Integer id) throws 
 ClassNotFoundException, SQLException{
try (Connection conn = ConnectionSQL.conectar()){
    String query = "SELECT id, nome, rg, cpf, cargo, sigla_setor FROM Funcionario WHERE id = ?";
    PreparedStatement preparedStatement = conn.prepareStatement(query);
    preparedStatement.setInt(1, id);
    preparedStatement.execute();
}  catch (SQLException ex) {
    throw new RuntimeException(ex);
}
return null;
}
    
asked by anonymous 30.07.2018 / 18:40

2 answers

1

Why do you use the UpdateUpdate () method before going to the data editing screen for that employee? This step should only be directed to the editing screen and from it you submit the data to update. And in your search for FunctionalId (id) you are not returning the Employee and yes null. You need to create an employee with the data coming from the database and return it.

} else if (action.equalsIgnoreCase("editar")) {
   forward = EDITAR;
   Integer id = Integer.parseInt(request.getParameter("id"));
   Funcionario funcionario = funcionarioDAO.buscarFuncionarioId(id);
   /*AQUI!!!*/
   funcionarioDAO.atualizarFuncionario(funcionario);
   request.setAttribute("funcionario", funcionario);
}
    
31.07.2018 / 15:19
0

I was able to fix it by making some changes.

First in the class DAO, where I was giving null return in the lookupID:

@Override
public Funcionario buscarFuncionarioId(Funcionario funcionario) throws ClassNotFoundException, SQLException{
    Funcionario retorno = null;
    try (Connection conn = ConnectionSQL.conectar()){
        String query = "SELECT id, nome, rg, cpf, cargo, sigla_setor FROM Funcionario WHERE id = ?";
        PreparedStatement preparedStatement = conn.prepareStatement(query);

        preparedStatement.setInt(1, funcionario.getId());
        ResultSet resultado = preparedStatement.executeQuery();

        if (resultado.next()) {
            retorno = new Funcionario();
            retorno.setId(resultado.getInt("id"));
            retorno.setNome(resultado.getString("nome"));
            retorno.setRg(resultado.getString("rg"));
            retorno.setCpf(resultado.getString("cpf"));
            retorno.setCargo(resultado.getString("cargo"));
            retorno.setSigla_setor(resultado.getString("sigla_setor"));
        }   

    }catch (Exception e) {
        // TODO: handle exception
    }
    return retorno;
}

Then in the EmployerListController () class:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String forward = "";
    String action = request.getParameter("action");
    try {
    if (action.equalsIgnoreCase("delete")) {
        forward = TABELA;
        Integer id = Integer.parseInt(request.getParameter("id"));
        funcionarioDAO.deletarFuncionario(id);
        request.setAttribute("funcionarios", funcionarioDAO.todosFuncionarios());
    } else if (action.equalsIgnoreCase("editar")) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Funcionario f = new Funcionario();
        f.setId(id);
        Funcionario funcionario = funcionarioDAO.buscarFuncionarioId(f);
        forward = EDITAR;
        funcionarioDAO.atualizarFuncionario(funcionario);
        request.setAttribute("funcionario", funcionario);
    } else {
        forward = TABELA;
        request.setAttribute("funcionarios", funcionarioDAO.todosFuncionarios());
    }
    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);
    }catch (Exception e) {
        // TODO: handle exception
    }
}
    
31.07.2018 / 20:39