I have a JSP that on the same screen I make a query record. When I open the screen I basically have the fields for the registration to below a grid with the listing of the bank, so far so good, working. As soon as I open the screen, the bank data is brought in.
My problem is when I need to query a record. I have DAO running, but shortly after running the DAO that brings the record I want, due to the fact I do not know @RequestMapping's configuration, the page loads all the data again.
Page jsp:
<!DOCTYPE html>
<html lang="br">
<head>
<%@ include file="/WEB-INF/views/tags/head.jsp"%>
</head>
<body>
<%@ include file="/WEB-INF/views/tags/header.jsp"%>
<div id="body">
<div id="content">
<div class="container-fluid" id="dashboard">
<div class="row-fluid">
<div class="span9">
<div class="page-header">
<h1>Cadastro de Fundos</h1>
</div>
<form class="form-horizontal form-container" method="post" action="${pageContext.request.contextPath}/FundoServlet">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
<fieldset>
<div class="control-group" style="vertical-align: middle;">
<label for="fundoid" class="control-label">Código</label>
<div class="controls">
<input type="text" id="fundoid" name="fundoid" class="input-xlarge" value="${Fundos.id}" maxlength="3" size="3">
<input type="hidden" id="action" name="action" />
</div>
</div>
<div class="control-group" style="vertical-align: middle;">
<label for="fundoDescr" class="control-label">Descrição</label>
<div class="controls">
<input name="fundoDescr" autofocus="autofocus" value="${Fundos.fundoDescr}" />
<input type="hidden" id="action" name="action" />
</div>
</div>
<div class="control-group" style="vertical-align: middle;">
<label for="enviar" class="control-label">Ação</label>
<div class="controls">
<select name="acao" required>
<option selected value="Incluir">Incluir</option>
<option value="Alterar">Alterar</option>
<option value="Excluir">Excluir</option>
<option value="Consultar">Consultar</option>
</select>
<input type="submit" id="enviar" name="enviar" value="Enviar">
<input type="reset" id="limpar" name="limpar" value="Limpar">
</div>
</div>
</fieldset>
</form>
<table id="dataTable" class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 90px">Código<i class="sort"></i></th>
<th style="width: 90px">Descrição<i class="sort"></i></th>
<th></th>
</tr>
</thead>
<tbody>
<c:forEach var="fundos" items="${fundos}">
<tr>
<td><c:out value="${fundos.id}" /></td>
<td><c:out value="${fundos.fundoDescr}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<%@ include file="/WEB-INF/views/tags/footer.jsp"%>
</body>
</html>
FundController class:
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
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.bind.annotation.RequestMethod;
@Controller
public class FundosController {
@Autowired
private FundosServices fundosServices;
@RequestMapping(path = "/monitor/fundos")
public String listar(HttpServletRequest request) {
final List<Fundos> fundos = fundosServices.findAll();
request.getSession().setAttribute("fundos", fundos);
return "/monitor/fundos";
}
@RequestMapping(value="/monitor/fundos/{id}", method = RequestMethod.GET)
public String findById(HttpServletRequest request, @PathVariable("id") long id) throws SQLException {
final Fundos fundos = fundosServices.findById(id);
request.getSession().setAttribute("fundos", fundos);
return "/monitor/fundos/{id}";
}
}
FundosServices Class:
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class FundosServices {
@Autowired
FundosDAO fundosDao;
public List<Fundos> findAll() {
return this.fundosDao.listar();
}
public Fundos findById(long id) {
Fundos retorno = null;
try {
retorno = this.fundosDao.findById(id);
} catch (SQLException e) {
e.printStackTrace();
}
return retorno;
}
}
FundoServlet class:
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/FundoServlet")
public class FundoServlet extends HttpServlet {
private static final long serialVersionUID = 7159377112218914143L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String acao = request.getParameter("acao");
String destino = "/monitor/fundos";
Fundos fundo = new Fundos();
FundosDAO fundosDao = new FundosDAO();
try {
if (acao.equalsIgnoreCase("Excluir")) {
fundo.setId(Integer.parseInt(request.getParameter("fundoid")));
fundosDao.deleteFundo(fundo);
} else if (acao.equalsIgnoreCase("Incluir")) {
fundo.setFundoDescr(request.getParameter("fundoDescr"));
fundosDao.inserir(fundo);
} else if (acao.equalsIgnoreCase("Consultar")) {
fundo = fundosDao.findById(Integer.parseInt(request.getParameter("fundoid")));
destino = "/monitor/fundos/15";
}
} catch (Exception e) {
e.printStackTrace();
}
RequestDispatcher rd = request.getRequestDispatcher(destino);
rd.forward(request, response);
}
}
ADO Fund Class:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
@Repository
public class FundosDAO extends GenericDAO<Integer, Fundos> {
@PersistenceContext
private EntityManager manager;
public List<Fundos> listar() {
final List<Fundos> retorno = manager.createQuery("select fundos from Fundos fundos", Fundos.class).getResultList();
return retorno;
}
public Fundos findById(long id) throws SQLException {
Connection conn = ConnectionFactory.getConnection();
PreparedStatement pstm = conn.prepareStatement("SELECT FUNDO_DESCR FROM TTCMT_FUNDOS WHERE FUNDO_ID = ?");
pstm.setFloat(1, id);
ResultSet rs = pstm.executeQuery();
Fundos fundos = new Fundos();
while (rs.next()) {
fundos.setId(id);
fundos.setFundoDescr(rs.getString("FUNDO_DESCR"));
}
rs.close();
return fundos;
}
public void inserir(Fundos fundo) {
try {
Connection conexao = getConexao();
PreparedStatement pstm = conexao.prepareStatement("INSERT INTO TTCMT_FUNDOS (FUNDO_DESCR) values (?)");
pstm.setString(1, fundo.getFundoDescr());
pstm.execute();
pstm.close();
conexao.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteFundo(Fundos fundo) throws SQLException {
Connection conexao = getConexao();
String sql = "DELETE FROM TTCMT_FUNDOS WHERE FUNDO_ID = ?";
PreparedStatement pstm = conexao.prepareStatement(sql);
pstm.setLong(1, fundo.getId());
pstm.execute();
pstm.close();
}
}