JSP页面
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'product.jsp' starting page</title>
 
<script type="text/javascript">

var xmlHttp;
var $ = document.getElementById;
function loadDept(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttp = new XmlHttpRequest();
}
xmlHttp.onreadyStateChange = getDept;
xmlHttp.Open("GET","cate!findCate.action",true);
xmlHttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded;charset=UTF-8;');
xmlHttp.Send(null);
}

function getDept(){
if(xmlHttp.readyState==4){
if(xmlHttp.Status == 200){

var depts = xmlHttp.responseText;
var js = eval(depts);
var sel = document.getElementById("depts");
for(var i in js){
var Option=document.createElement("OPTION");
Option.value=js[i].categoryId; 
Option.text=js[i].categoryName;
sel.options.add(Option);

}
}

}
}
function getDate(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttp = new XmlHttpRequest();
}
var categoryId = document.getElementById("depts").value;
alert(categoryId);
xmlHttp.onreadyStateChange = selectChange;
var url = "cate!findByIdPro.action?categoryId="+categoryId;
alert(url);
xmlHttp.Open("GET",url,true);

xmlHttp.Send(null);

}

function selectChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status ==200){
var datas=xmlhttp.responseText;
var jes=eval(datas);
alert("+++"+dates);
var  tbs=document.getElementById("tbs");
for(var i=tbs.rows.length-1;i>=0;i--)
{
tbs.deleteRow(i);
}
for( var w in jes)
{
var newRow=tbs.insertRow(tbs.rows.length);
newRow.insertCell(0).innerHTML=jes.productId;
newRow.insertCell(1).innerHTML=jes.productName;
newRow.insertCell(2).innerHTML=jes.price;
newRow.insertCell(3).innerHTML=jes.createDate;
newRow.insertCell(4).innerHTML="<a href='javascript:del("+jes[w].productId+")'>删除</a>";
}
}
}
}

function del(productId)
{
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttp = new XmlHttpRequest();
}
xmlhttp.onreadyStateChange=selectChange;
var url = "cate!findByIdPro.action?categoryId="+productId;
xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded;charset=UTF-8;');
xmlhttp.send(null);

}
</script>
  </head>
  
  <body>
  
   <form>
   <input type="button" value="加载部门"  onclick="loadDept()"/>
   <select id="depts" name="dept" onchange="getDate()">
   <option value="-1">请选择:</option>
   </select><br/>
   <table id ="tbs" border="1" align="center"></table>
   </form>
  </body>
</html>
对应Action[b][/b]
public class CateAction extends ActionSupport {
CateService service = new CateService();
ProService pservice = new ProService();
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub

return null;
}

public String findCate(){

List<Categorys> list = service.findAll();
for(Categorys cate:list){
cate.setProducts(null);
}
HttpServletResponse resp = ServletActionContext.getResponse();
resp.setCharacterEncoding("utf-8");
JSONArray json = new JSONArray().fromObject(list);
try {
resp.getWriter().print(json.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;

}

public String findByIdPro(){

HttpServletRequest req = ServletActionContext.getRequest();
String id = req.getAttribute("categoryId").toString();
System.out.println("==="+id);
List<Product> pro =  pservice.findById(Integer.parseInt(id));
for(Product cate:pro){
cate.setCategorys(null);
}
if(pro!=null){
HttpServletResponse resp = ServletActionContext.getResponse();
resp.setCharacterEncoding("utf-8");
JSONArray json = new JSONArray().fromObject(pro);
try {
resp.getWriter().println(json.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

public String delPro(){
HttpServletRequest req = ServletActionContext.getRequest();
String id = req.getAttribute("categoryId").toString();
System.out.println("==="+id);
List<Product> pro =  pservice.delPro(Integer.parseInt(id));
for(Product cate:pro){
cate.setCategorys(null);
}
if(pro!=null){
HttpServletResponse resp = ServletActionContext.getResponse();
resp.setCharacterEncoding("utf-8");
JSONArray json = new JSONArray().fromObject(pro);
try {
resp.getWriter().println(json.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;

}


}
比较头疼的问题第一个加载部门方法可以响应,但后面的直接不能进Action,求大神帮忙啊 急!Ajaxstruts

解决方案 »

  1.   

    代码这么乱,什么action进不去都描述不清楚,怎么回答啊?
      

  2.   

    不好意思!确实没有描述清楚,第一次发帖。
    加载部门时loadDept()方法能够从后台获取数据,下拉框的onChange事件中那个方法却不能够进入到后台,这两个方法是在同一个Action里面
      

  3.   

    xmlHttp.onreadyStateChange = getDept;
    xmlHttp.Open("GET","cate!findCate.action",true);
    xmlHttp.setRequestHeader('Content-type',
    'application/x-www-form-urlencoded;charset=UTF-8;');
    xmlHttp.Send(null);这个两个不是同一个action啊