我的项目中有以下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的mehod 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();
}
}
有人可以发现此代码有任何问题吗?我真的被困在这里。
答案 0 :(得分:0)
好的,我解决了这个问题。出于某种原因,我不知道,JavaScript需要角色'&amp;'与参数名称放在一起(两者之间有空格,未捕获参数)。任何人都知道该语言是否存在这种行为的原因?
我现在的问题是servlet只运行结构中第三个grupo的代码,如果/ else if / else放在方法的末尾。由于某种原因,我仍然看不到,尽管正确捕获了“目标”参数(我验证了这一点),但两个逻辑句子都返回false。