Boa tarde pessoal,
É o seguinte. Estou desenvolvendo um ecommerce como projeto da facul faz uns 2 meses e a entrega final (infelizmente) é para amanhã, mas estou com diversas dúvidas, diversos erros, quase me desesperando.... Mas como sempre encontro ajuda aqui, resolvi postar.
Em primeiro lugar antes que falem que deixei para ultima hora(como muitos podem pensar), pelo contrário, estou desenvolvendo há dois meses, mas parece que nunca termina...
exception
javax.servlet.ServletException: java.lang.NumberFormatException: For input string: ""
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:520)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:427)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
ACTION
public class ItensAction extends org.apache.struts.action.Action {
private final static String SUCCESS = "carrinho";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
int IdProduto = Integer.parseInt(request.getParameter("produtoId"));
HttpSession session = request.getSession(true);
List<ItemPedido> lista = (List<ItemPedido>) session.getAttribute(SUCCESS);
if(lista == null){
lista = new ArrayList<ItemPedido>();
}
boolean existe = false;
for(ItemPedido item : lista){
if(item.getId() == IdProduto){
item.setQuantidade(item.getQuantidade()+1);
item.setTotal(item.getValorUnitario().multiply(new BigDecimal(item.getQuantidade())));
//item.setTotal(item.getValorUnitario().multiply(item.getValorUnitario(), new MathContext(item.getQuantidade())));
existe = true;
}
}
if(existe == false){
ProdutoDao mProduto = new ProdutoDaoImpl();
Produto bProduto = mProduto.getProduto(IdProduto);
ItemPedido bean = new ItemPedido();
bean.setId(IdProduto);
bean.setQuantidade(1);
bean.getProduto().setNome(bProduto.getNome());
bean.setValorUnitario(bProduto.getValor());
bean.setTotal(bProduto.getValor().multiply(new BigDecimal(bean.getQuantidade())));
lista.add(bean);
}
session.setAttribute("carrinho",lista);
//calcula o valor da compra
BigDecimal total = new BigDecimal(0);
for(ItemPedido item: lista){
total = total.add(item.getTotal());
}
session.setAttribute("total",total);
return mapping.findForward(SUCCESS);
}
}
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.List,br.com.ecommerce.model.bean.Produto"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="h" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script>
function action(){
form = document.forms[0];
form.action = 'carrinho.do';
form.submit();
}
</script>
<title>Resultado da Busca de Produtos</title>
</head>
<body>
<form
><font face="cambria">
<table border="0" align ="center" cellspacing="0" cellpadding="0">
<tr>
<td colspan="4">
<img />
</td>
</tr>
<%!
List<Produto> lista;
%>
<%
lista = (List<Produto>)session.getAttribute("lista");
if(lista.isEmpty()) {
%>
<br><font color="FF0000">Nenhum produto encontrado!</font><br>
<%
} else {
%>
<table bgcolor=#E8E8E8" bordercolor="#009ACD" cellpadding="1" width="960px" border="4" align="center">
<tr>
<th>Cód. do Produto</th>
<th>Produto</th>
<th>Nome</th>
<th>Descricao</th>
<th>Valor Unitario</th>
<th>Valor Promocional</th>
<th>Quantidade</th>
</tr>
<%
for(Produto p: lista) {
%>
<tr>
<td align="center"><%=p.getId() %></td>
<td align="center"><img /></td>
<td align="center"><%=p.getNome() %></td>
<td align="center"><%=p.getDescricao() %></td>
<td align="center"><%=p.getValor() %></td>
<td align="center"><%=p.getValorPromocional() %></td>
<td align="center"><%=p.getQuantidade() %></td>
<td align="center">
<!-- Onde faço a chamada da action -->
<a ><%=p.getId()%> >
<img ></a></td>
</tr>
<%
}
%>
</table>
<%
}
%>
<br>
<hr>
<a >Realizar outra busca</a><br><br>
</font>
</form>
</body>
</html>
Não creio que seja problema no struts config, por esse motivo não postarei...
Ah, o framework que uso é struts 1. E me perdoem se algo está errado, sou iniciante em java =/
Desde já agradeço!