首页 > 代码库 > day8——ajax传参到action(Struts2)

day8——ajax传参到action(Struts2)

第一种:url+?+参数

jsp中:

$(function(){
  $("[name=‘delemp‘]").click(function(){
  $this = $(this);
  $delid = $this.attr("delid");
  if(confirm("确认删除该条数据吗?")){
    $.ajax({
      type:"get",
      url:"deleteemployeebyid?delid="+$delid,
      dataType:"json",
      success:function(msg){
        $this.parent().parent().parent().remove();
        alert(msg);
    }
    });
  }else{
  return false;
}
})

 

action中:

public String delEmployeesById(){
  Map<String,Object> map = ActionContext.getContext().getParameters();
  Object[] delid = (Object[]) map.get("delid");
  String deleteid = (String) delid[0];
  Integer did = Integer.valueOf(deleteid);
  Employees emp = new Employees();
  emp.setId(did);
  employeesService.deleteEmployeeById(emp);
  return SUCCESS;
}

 

 

第二种:post请求传递,action属性接收(推荐

jsp:

  ………………

  $.ajax({
      type:"post",
      url:"deleteemployeebyid,
      dataType:"json",

      data:{"delId":$delid},
      success:function(msg){
        $this.parent().parent().parent().remove();
        alert(msg);
    }

  ………………

 

action:

private Integer delId;

getter/setter方法

 

private String jsonobj;  //删除success后返回的msg

getter/setter

………………具体方法中直接用delId

 

struts.xml:(json结果配置)

<!-- json响应,返回单个Object -->
<result name="retJsonObj" type="json">
  <param name="root">jsonObj</param>
</result>

day8——ajax传参到action(Struts2)