Senhores,
Estou na “luta” de preencher uma combo a partir da seleção de outra e estou seguindo um exemplo simples, mas infelizmente não está rolando ainda. Se eu chamo a Sevlet na mão ele vai, agora pelo método javascript não faz nada e nem dá erro de script. Abaixo segue o código do jsp, da servlet e wex.xml
JSP
<%@ page language="java"%>
<%@ page import="br.com.unitelco.dto.RangeIpDTO"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/tld/displaytag-11.tld" prefix="display"%>
<%
String action = (String) request.getAttribute("action");
RangeIpDTO dto = (RangeIpDTO) request.getAttribute("dto");
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<title>..:: Cadastro de Range de IP's ::..</title>
<html:base />
<link href="css/inscricao_css.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="js/global/default.js" type="text/javascript"></script>
<script language="JavaScript" src="js/local/rangeIp.js" type="text/javascript"></script>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script language="JavaScript">
var tXHR=0;
function XMLHTTPRequest() {
if (window.XMLHttpRequest) {
a=new XMLHttpRequest(); } //Objeto nativo (FF/Safari/Opera7.6+)
else {
try {
a=new ActiveXObject("Msxml2.XMLHTTP"); //activeX (IE5.5+/MSXML2+)
}
catch(e) {
try {
a=new ActiveXObject("Microsoft.XMLHTTP"); //activeX (IE5+/MSXML1)
}
catch(e) { /* O navegador não tem suporte */
a=false;
}
}
}
return a;
}
function atualizaCircuito() {
var idPj = document.rangeIpDTO.idPj.options[document.rangeIpDTO.idPj.selectedIndex].value;
tXHR=XMLHTTPRequest();
if (tXHR) {
document.rangeIpDTO.idCircuito.length = 0;
document.rangeIpDTO.idCircuito.options[0] = new Option("Aguarde....","");
tXHR.open("GET", "/Teste?idPj="+idPj, true);
tXHR.onreadystatechange=recebeResultado;
tXHR.send(null);
}
}
function recebeResultado() {
if (!tXHR) {
return false;
}
else {
if (tXHR.readyState == 4) {
if (tXHR.status == 200) {
document.getElementById("idCircuito").innerHTML = tXHR.responseText;
}
else {
alert('Erro! "'+ tXHR.statusText +'" (erro '+ tXHR.status +')');
}
}
}
}
</script>
<%if ("doClose".equals(action)) {%>
<script language="JavaScript" type="text/javascript">
parent.opener.parent.grid.location = '<%=request.getContextPath()%>/rangeIp.do?method=selectAll';
window.close();
</script>
<%}%>
</head>
<div id="pagina">
<!-- Início do formulário-->
<div id="formulario"> <img >
<h1>Cadastro de Range</h1>
<html:form action="/rangeIp.do?method=doConfirm" focus="idPj">
<input name="action" id="action" type="hidden" value="<%=action%>" >
<input name="idRangeIp" id="idRangeIp" type="hidden" value='<bean:write name="dto" property="idRangeIp"/>'>
<fieldset>
<label for="Cliente"><strong>*</strong>Cliente<br>
<html:select property="idPj" value='${dto.pj.idPj}'>
<html:option value="-1">Selecione uma opção</html:option>
<html:options collection="listPjs" property="idPj" labelProperty="nomeFantasia"/>
</html:select>
</label>
<br >
<label for="Circuito"><strong>*</strong>Circuito:<br>
<html:select property="idCircuito" value='${dto.circuito.idCircuito}' onchange="atualizaCircuito()">
<html:option value="-1">Selecione uma opção</html:option>
<html:options collection="listCircuitos" property="idCircuito" labelProperty="designacao"/>
</html:select>
</label>
<br >
<label for="Repetidora"><strong>*</strong>Repetidora:<br>
<html:select property="idPonto" value='${dto.repetidora.idPonto}'>
<html:option value="-1">Selecione uma opção</html:option>
<html:options collection="listRepetidoras" property="idPonto" labelProperty="descricao"/>
</html:select>
</label>
<br >
<label for="Rede"><strong>*</strong>Rede<br>
<input name="rede" id="rede" size="30" maxlength="15" type="text" value='<bean:write name="dto" property="rede"/>'/>
</label>
<br >
<label for="Tamanho"><strong>*</strong>Tamanho<br>
<html:select property="tamanho" value='${dto.tamanho}'>
<html:option value="/20">/20</html:option>
<html:option value="/21">/21</html:option>
<html:option value="/22">/22</html:option>
<html:option value="/23">/23</html:option>
<html:option value="/24">/24</html:option>
<html:option value="/25">/25</html:option>
<html:option value="/26">/26</html:option>
<html:option value="/27">/27</html:option>
<html:option value="/28">/28</html:option>
<html:option value="/29">/29</html:option>
<html:option value="/30">/30</html:option>
</html:select>
</label>
<br >
<label for="Data"><strong>*</strong>Data Ínicio<br>
<input name="dataInicio" id="dataInicio" size="15" maxlength="10" type="text" value='<bean:write name="dto" property="dataInicio"/>'/>
</label>
<br >
</fieldset>
<p>
<html:submit value="Gravar" styleClass="botao"/> |
<html:cancel value="Cancelar" styleClass="botao" onclick="window.close()" />
</p>
</html:form>
</div>
</div>
</html:html>
SERVLET
package br.com.unitelco.utils;
import br.com.unitelco.bdo.CircuitoBDO;
import br.com.unitelco.dto.CircuitoDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Teste extends HttpServlet {
private static final long serialVersionUID = 1L;
public Teste() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
StringBuffer retorno = new StringBuffer("");
PrintWriter out = response.getWriter();
out.println("<select name=\"idCircuito\">");
try {
ArrayList rows = (ArrayList) CircuitoBDO.selectAll(request);
for (CircuitoDTO circuito : (ArrayList<CircuitoDTO>) rows){
retorno.append("<option value=\"" + circuito.getIdCircuito() + "\">" + circuito.getDesignacao() + "</option>\n");
}
} catch (Exception e) {
request.setAttribute("erro", e.getMessage());
}
out.println(retorno);
out.println("</select>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
}
}
Web.xml
<servlet>
<servlet-name>Teste</servlet-name>
<servlet-class>br.com.unitelco.utils.Teste</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Teste</servlet-name>
<url-pattern>/Teste</url-pattern>
</servlet-mapping>