2014-02-21 78 views
0

我有我的項目中遵循JSP頁面,在這裏我需要的,在一個複選框,相關的一日期/時間每次點擊發送到我的seervlet:通過JSP/JSTL頁面發送Ajax請求

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<!-- <script src="jquery-1.11.0.min.js"></script> 
<script> 
$(".checkbox").change(function() { 
    if(this.checked) { 
     alert("unmarked"); 
    } else{ 
     alert("marked"); 
    } 
}); 
</script> --> 

<script type="text/javascript" src="<c:url value='/js/ajax.js'/>"></script> 
<script> 
function handleClick(cb, idevento, data, hora) { 
    if(cb.checked) { 
     alert("marked "+data+" "+hora+" for "+idevento); 
     sendAjaxRequest("/hora_livre/CadastraHoraLivre?target=cadastra & data="+data+" & hora="+hora+" &evento="+idevento, "showdetails"); 
    } else{ 
     alert("unmarked "+data+" "+hora+" for "+idevento); 
     sendAjaxRequest("/hora_livre/CadastraHoraLivre?target=remove & data="+data+" & hora="+hora+" &evento="+idevento, "showdetails"); 
    } 
} 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Cadastra Horario Livre</title> 
</head> 
<body> 

<p align="center"> 
<span class="usuario">${nome}</span> | <strong> Hora Livre</strong> | <a href="/hora_livre/ProcessaSaida"> Sair</a> 
</p> 

<p align="center"> 

<form method="get" action="/hora_livre/CadastraHoraLivre"> 

<table border = 2> 

<tr> 
    <th> </th> 
    <c:forEach var="item" items="${list2}"> 
     <th> <c:out value="${item}"/> </th> 
    </c:forEach> 
</tr> 

<c:forEach var="item2" items="${list}"> 
<tr> 
    <td> <c:out value="${item2}"/> </td> 
    <c:forEach var="item" items="${list2}"> 
     <td> <input type="checkbox" id="checkbox" onclick="handleClick(this, '${id}', '${item2}', '${item}')"> </td> 
    </c:forEach> 
</tr> 
</c:forEach> 

</table> 

<input type="submit" value="ok"> 

</form> 

<div id="showdetails"> </div> 

</p> 

</body> 
</html> 

我的問題是:servlet正確捕獲'target'和'evento'的值,但'data'和'hora'的值爲null。

從Servlet中的doGet mehod的代碼是:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    String target = request.getParameter("target"); 
    System.out.println("[CadastraHoraLivre.doGet] target = " + target); 

    String data = request.getParameter("data"); 
    System.out.println("[CadastraHoraLivre.doGet] data = " + data); 

    String hora = request.getParameter("hora"); 
    System.out.println("[CadastraHoraLivre.doGet] hora = " + hora); 

    String idevento = request.getParameter("evento"); 
    System.out.println("[CadastraHoraLivre.doGet] idevento = " + idevento); 
    int id_evento = Integer.valueOf(idevento).intValue(); 

    Cookie[] cookies = request.getCookies(); 
    String idsessao = new String(); 
    if (cookies != null) { 
    for (Cookie cookie : cookies) { 
     if (cookie.getName().equals("sessao")) { 
      idsessao = cookie.getValue(); 
     } 
     } 
    } 

    System.out.println("[CadastraHoraLivre.doGet] idsessao = " + idsessao); 
    int id_sessao = Integer.valueOf(idsessao).intValue(); 

    data.Usuario usuario = null; 
    try { 
     usuario = new data.Usuario(id_sessao); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    int id_usuario = usuario.getUsuario().getId(); 
    System.out.println("[CadastraHoraLivre.doGet] idusuario = " + String.valueOf(id_usuario)); 

    data.HoraLivre hora_livre = null; 
    try { 
     hora_livre = new data.HoraLivre(id_evento, id_usuario, data, hora); 
     if(target.equals("cadastra")) { 
      if(hora_livre.CadastraHoralLivre()) 
      { 
       //request.setAttribute("msg", "Horario cadastrada com sucesso"); 
       //request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); 
       PrintWriter out = response.getWriter(); 
       out.write("Horario cadastrado com sucesso"); 
      } 
      else 
      { 
       //request.setAttribute("msg", "Erro ao cadastrar o Horario"); 
       //request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); 
       PrintWriter out = response.getWriter(); 
       out.write("Erro ao cadastrar o Horario"); 
      } 
     } 
     else if(target.equals("remove")) { 
      if(hora_livre.RemoveHoraLivre()) 
      { 
       //request.setAttribute("msg", "Horario removido com sucesso"); 
       //request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); 
       PrintWriter out = response.getWriter(); 
       out.write("Horario removido com sucesso"); 
      } 
      else 
      { 
       //request.setAttribute("msg", "Erro ao remover o Horario"); 
       //request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); 
       PrintWriter out = response.getWriter(); 
       out.write("Erro ao remover o Horario"); 
      } 
     } 
     else { 
      PrintWriter out = response.getWriter(); 
      out.write("Opção invalida!"); 
     } 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

有人能找到這個代碼的任何問題嗎?我真的被困在這裏。

回答

0

好的,我解決了這個問題。出於某種原因我不知道,JavaScript要求將字符'&'與參數名稱放在一起(兩者之間有一個空格,參數未被捕獲)。任何人都知道如果存在語言的原因有這種行爲?

我現在的問題是,servlet只運行結構中if/else if/else放置在結構中的第三個grupo的代碼。出於某種原因,我仍然無法看到,儘管參數'target'被正確捕獲(我證實了這一點),但兩個邏輯句子仍然返回false。