Created
July 19, 2012 21:09
-
-
Save juliano/3146833 to your computer and use it in GitHub Desktop.
SimpleDev - Action para combos dependentes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | |
<html> | |
<head> | |
<title>SimpleDev - Combos Dependentes com Struts 2</title> | |
</head> | |
<body> | |
<h3>SimpleDev - Combos Dependentes com Struts 2 - Estado e Cidade</h3> | |
<select name="estados" id="estados"> | |
<option>Selecione o estado</option> | |
<c:forEach items="${estados}" var="est"> | |
<option value="${est}">${est}</option> | |
</c:forEach> | |
</select> | |
<br> | |
<select name="cidades" id="cidades"> | |
<option>Selecione o estado</option> | |
</select> | |
<script language="JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> | |
<script language="JavaScript" src="/js/populaCidades.js" type="text/javascript"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.simpledev.struts2.action; | |
import java.util.List; | |
import org.apache.struts2.convention.annotation.Action; | |
import org.apache.struts2.convention.annotation.Namespace; | |
import org.apache.struts2.convention.annotation.ParentPackage; | |
import org.apache.struts2.convention.annotation.Result; | |
import br.com.simpledev.struts2.dao.LocalidadeDao; | |
import br.com.simpledev.struts2.dao.jdbc.JdbcLocalidadeDao; | |
@ParentPackage("json-default") | |
@Namespace(value = "/localidade") | |
public class LocalidadeAction { | |
private String estado; | |
private List<String> estados; | |
private List<String> cidades; | |
private LocalidadeDao dao; | |
public LocalidadeAction() { | |
this.dao = new JdbcLocalidadeDao(); | |
} | |
@Action(value = "estados", results = @Result(name = "ok", location = "/localidades.jsp")) | |
public String listaEstados() { | |
estados = dao.listaEstados(); | |
return "ok"; | |
} | |
@Action(value = "cidades", results = @Result(name = "ok", type = "json", params = { "excludeProperties", "estados, estado" })) | |
public String listaCidades() { | |
cidades = dao.listaCidades(estado); | |
return "ok"; | |
} | |
public String getEstado() { | |
return estado; | |
} | |
public void setEstado(String estado) { | |
this.estado = estado; | |
} | |
public List<String> getEstados() { | |
return estados; | |
} | |
public List<String> getCidades() { | |
return cidades; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#estados').change(function() { | |
var estado = $('#estados').val(); | |
$.getJSON('cidades', {'estado' : estado}, function(json) { | |
var options = ''; | |
$(json.cidades).each(function(index, cidade) { | |
options += '<option value="' + cidade + '">' + cidade + '</option>'; | |
}) | |
$("#cidades").html(options); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment