前台如下图如果我在sector中做了选择,则onchange="loadDivisions(),然后使用ajax通信,告诉action当前sector的选项,而action则根据sector的选项值,返回相应的division值。
我在js里的回调函数,写了测试语句;alert("here");alert("Receive:"+data)
但是并没有执行alert,请问是怎么回事啊?是拦截器的问题么?
在action里,我也简单写了测试
this.getResponse().getWriter().print("Action say");
谢谢啦~loadDivisions()内容如下:
function loadDivisions() {
var selectedValue;
var options = document.getElementById("sectorId").options;
for ( var i = 0; i < options.length; i++) {
if (options[i].selected == true)
selectedValue = options[i].value;
}

if (selectedValue=="100") {
document.getElementById("companyId").options.length = 0;
}else{
var params = {
"parentSectorId" : selectedValue
};
var url = "getSubDivision";
document.getElementById("divisionId").options.length = 0;
alert("selectedValue:"+selectedValue);
$.getJSON(url, params, function callback(data) {

alert("here");
alert("Receive:"+data);
option = document.createElement('option');
option.text= "";
option.value = 100;
document.getElementById("divisionId").add(option, 0);

for ( var i = 0; i < data.length; i++) {
option = document.createElement("option");
option.text = data[i].name;
option.value = data[i].id;
alert("optione:"+option.text+"_"+option.value);
var divPosition = document.getElementById("divisionId").options.length;
document.getElementById("divisionId").add(option, divPosition);
}
});
}
}如果做了一个非空选择,则根据当前选择,改变Division的选项。
在struts.xml里getSubDivision配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />    <package name="default" namespace="/" extends="struts-default">

       <action name="toReport" class="action.ReportAction"
method="toReport">
<result name="success">/report.jsp</result>
</action>
<action name="getSubDivision" class="action.ReportAction"
method="getSubDivision">
<interceptor-ref name="defaultStack" />
</action>
    </package></struts>
而ReportAction如下package action;import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import dao.DB;
import model.Organization;public class ReportAction extends ActionSupport implements ServletResponseAware{ private List<Organization> sectors;
private List<Organization> divisions;
private String parentSectorId;
private HttpServletResponse response;

public String toReport() {

sectors = DB.getAllSectors();
System.out.println("secots:"+sectors);
return SUCCESS;
}

public String getSubDivision() {

if (parentSectorId == "") {
return null;
}

int secId = Integer.parseInt(parentSectorId);
divisions = DB.getDivisionsBySecId(secId);
System.out.println("SectorId "+ secId +"'s divisions:");
String str = "";
for(Organization div: divisions) {
str += div.getId()+":"+div.getName();
}
try {
this.getResponse().getWriter().print("Action say");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ERROR;
}
return null;
}

public List<Organization> getSectors() {
return sectors;
}

public void setSectors(List<Organization> sectors) {
this.sectors = sectors;
}

public List<Organization> getDivisions() {
return divisions;
}

public void setDivisions(List<Organization> divisions) {
this.divisions = divisions;
} public String getParentSectorId() {
return parentSectorId;
} public void setParentSectorId(String parentSectorId) {
this.parentSectorId = parentSectorId;
} public HttpServletResponse getResponse() {
return response;
} @Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
}
}