繁体   English   中英

通过JSP / JSTL页面发送Ajax请求

[英]Sending ajax request through JSP/JSTL page

在我的项目中,有一个后续的JSP页面,在该页面中,我需要为复选框中的每一次单击将一个关联的日期/时间对发送到我的Seervlet:

<%@ 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的代码是:

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();
    }
}

有人可以找到与此代码有关的任何问题吗? 我真的被困在这里。

好的,我解决了这个问题。 由于某种原因,我不知道,JavaScript要求将字符“&”与参数名称放在一起(在两者之间有一个空格,未捕获参数)。 有人知道这种语言是否存在这种行为的原因吗?

现在我的问题是,如果/ else if / else放置在方法的末尾,则servlet仅从结构中的第三个grupo运行代码。 由于某些原因,我仍然看不到,尽管参数“ target”已正确捕获(我验证了),但两个逻辑语句仍返回false。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM