用struts2 and prototype.js实现无刷新删除是的发放啊大

来源:百度文库 编辑:神马文学网 时间:2024/05/01 10:40:48
一、首先下载struts2-dojo-plugin-2.1.6.jar 和 prototype.js(网址:http://www.prototypejs.org/download)导入包,并在WebRoot下建js文件夹,把prototype.js和application.js放入js下核心jar包如下:freemarker-2.3.13.jarognl-2.6.11.jarstruts2-core-2.1.6.jarxwork-2.1.2.jarcommons-fileupload-1.2.1.jarcommons-logging-1.0.4.jarstruts2-dojo-plugin-2.1.6.jar二、应用名ajax,包名ajax.noFresh,类名AjaxNoFresh.javaAjaxNoFresh.java代码如下:package ajax.noFresh;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import ajax.entity.User;import com.hx.qpxnet.util.JdbcUtil;
import com.opensymphony.xwork2.ActionSupport;public class AjaxNoFresh extends ActionSupport {
 static Connection conn=JdbcUtil.getConnection();
 Statement sta=null;
 ResultSet rs=null;
 private List list=null;
 private int id;
 
 public int getId() {
  return id;
 } public void setId(int id) {
  this.id = id;
 } public List getList() {
  return list;
 } public void setList(List list) {
  this.list = list;
 } public String execute() throws Exception{
  sta=conn.createStatement();
  conn.setAutoCommit(false);
  String sql="select id,username,password from fjjuser";
  rs=sta.executeQuery(sql);
  User user =null;
  list = new ArrayList();
  while(rs.next()){
   user = new User();
   user.setId(rs.getInt(1));
   user.setUsername(rs.getString(2));
   user.setPassword(rs.getString(3));
   list.add(user);
  }
  conn.commit();
  return SUCCESS;
 }
 public String delete()throws Exception{
  sta=conn.createStatement();
  conn.setAutoCommit(false);
  String sql="delete from fjjuser where id="+id;
  sta.executeUpdate(sql);
  conn.commit();
  return SUCCESS;
 }}
三、xml代码如下:
    
       
            /AjaxNoFresh1.jsp
       

       
            show
       

       
            /AjaxNoFresh2.jsp
       

  

四、jsp页面AjaxNoFresh1.jsp代码:<%@ taglib prefix="s" uri="/struts-tags"%>
  
      
        显示所有用户  
          
          
          
          
      
  
   
  
          
   
  
  
   AjaxNoFresh2.jsp <%@ taglib prefix="s" uri="/struts-tags"%>

 
 
     
    
      
      
      
      
    
    

   
     

       
       
       
       
     
     

   
   
idusernamepassworddelete

          
           delete
       

 
 application.jsfunction deleteUser(id){
var url = "delete.action?id="+id;  
    new Ajax.Updater (  
        'div_ajaxNoFresh2',  
        url,   
        {  
            onLoading:function (){  
            },  
            onSuccess:function (request) {  
                alert('删除成功');  
            },   
            onFailure:function (request) {  
                alert("服务器故障,请稍候重试");  
            }  
        }  
    );  
}最后通过访问http://localhost:8080/ajax/ajaxno.action,点击delete链接看看,是不是实现无刷新删除了,OK,完了。