Problem in the JSP Controller class

Asked

Viewed 54 times

0

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

Campos na URL da imagem

My JSP looks like this, when I click the edit button it sends an action to the Funciolistacontroller class.

 <thead>
                            <tr>
                                <th>ID</th>
                                <th>Nome</th>
                                <th>RG</th>
                                <th>CPF</th>
                                <th>Cargo</th>
                                <th>Setor</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach items="${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 Functioncontroller class is found 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 employee 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;
}

2 answers

1


Why do you use the update method Feature() before going to the edit screen of that employee’s data? This step should only be directed to the edit screen and from there you submit the data to update. And in your searchFunctionId(id) you are not returning the Employee but 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);
}
  • That buddy, I had forgotten to clone the right Ranche and some information was outdated. Thanks for the help! I will comment on the changes I have made :)

  • 1

    Ok! Then vote my answer as useful and put it as solved.

0

I was able to correct by making some changes.

First in the Functionclass, where it was giving null return in the search:

@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;
}

Next in the class Functionary():

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
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.