1.前台页面
<script type="text/javascript" src="<%=basePath %>script/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#district").change(function(){
var p_id=$(this).val();
var strHTML="";
if(p_id==0){
strHTML="<option value=\"0\">-请选择市--</option>"
$("#street").html(strHTML);
return;
}
$.ajax({
type:"post",
url:"ajaxServer!ajaxServer",
data:{pid:p_id},
dataType:"json",
success: function(data){
var len=data.length;
for(var i=0;i<len;i++){
strHTML+="<option value=\""+data[i].id+"\">"+data[i].name+"</option>";
}
$("#street").html(strHTML);
},
error:function(){
alert("解析数据错误");
}
});
});
});
</script>
2.后台action
public String ajaxServer(){
Integer p_id=pid==null?null:Integer.parseInt(pid.toString());
District district=new District();
district.setId(p_id);
Set<Street> listStreet=districtService.queryStreet(district);
//ajax解决对象里面包含对象
JsonConfig config=new JsonConfig();
HttpServletResponse response=ServletActionContext.getResponse();
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if(name.equals("district")||name.equals("house")){
return true;
}else {
return false;
}
}
});
JSONArray jsonArray=JSONArray.fromObject(listStreet, config);
String jsonStr=jsonArray.toString();
System.out.println(jsonStr);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().write(jsonStr);
response.getWriter().close();//关闭输出流
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
3.struts配置文件跟普通配置一样
<action name="ajaxServer" class="houseAction" method="ajaxServer">
</action>
<script type="text/javascript" src="<%=basePath %>script/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#district").change(function(){
var p_id=$(this).val();
var strHTML="";
if(p_id==0){
strHTML="<option value=\"0\">-请选择市--</option>"
$("#street").html(strHTML);
return;
}
$.ajax({
type:"post",
url:"ajaxServer!ajaxServer",
data:{pid:p_id},
dataType:"json",
success: function(data){
var len=data.length;
for(var i=0;i<len;i++){
strHTML+="<option value=\""+data[i].id+"\">"+data[i].name+"</option>";
}
$("#street").html(strHTML);
},
error:function(){
alert("解析数据错误");
}
});
});
});
</script>
2.后台action
public String ajaxServer(){
Integer p_id=pid==null?null:Integer.parseInt(pid.toString());
District district=new District();
district.setId(p_id);
Set<Street> listStreet=districtService.queryStreet(district);
//ajax解决对象里面包含对象
JsonConfig config=new JsonConfig();
HttpServletResponse response=ServletActionContext.getResponse();
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if(name.equals("district")||name.equals("house")){
return true;
}else {
return false;
}
}
});
JSONArray jsonArray=JSONArray.fromObject(listStreet, config);
String jsonStr=jsonArray.toString();
System.out.println(jsonStr);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().write(jsonStr);
response.getWriter().close();//关闭输出流
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
3.struts配置文件跟普通配置一样
<action name="ajaxServer" class="houseAction" method="ajaxServer">
</action>