使用场景:关闭页面弹窗时执行sql语句。
其实js里执行sql语句有多种方式。
方式一:直接在js代码里调用sql语句,原则上不能使用,因为这将sql直接暴露在客户端,安全性极差。
方式二:在js里运用ajax技术,调用后台方法执行sql语句。
方式三:运用dwr框架,其实是对ajax技术进行了封装。
一、首先编写一个dwr.xml文件
<"1.0" encoding="utf-8""-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr//dwr20.dtd"> <dwr> <allow> <!-- 对呼叫类型的操作 --> <create javascript="CallTypeOpDwr" creator="new"> <param name="class" value="cn.etcom.jfgl.tel.fee.callType.CallTypeOpDwr" /> <include method="addCallType" /> <include method="modifyCallType" /> <include method="deleteCallType" /> <include method="updateSp" /> </create> </allow> <!-- 用户信息管理操作中用到的map配置 --> <signatures> <![CDATA[ import java.util.List; import cn.etcom.jfgl.customer.tel.userInfo.UserInfoManageOpDwr; UserInfoManageOpDwr.addUsersInfo(String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,Map<String,String>); UserInfoManageOpDwr.addUserInfo(String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,Map<String,String>); ]]> </signatures> </dwr>
二、在web.xml文件里配置servlet
<!-- DWR相关配置 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name> allowGetForSafariButMakeForgeryEasier </param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
三、写一个普通的JAVA类
package cn.etcom.jfgl.tel.fee.callType; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.json.JSONException; import org.json.JSONObject; import cn.etcom.common.utility.DBUtil; import cn.etcom.common.utility.RsHandler; import cn.etcom.common.utility.SqlActuator; import cn.etcom.common.utility.StringUtil; /******************************************************************************* * * @author 刘彦宁 2011-08-12 * */ // 处理呼叫类型的dwr public class CallTypeOpDwr { /*************************************************************************** * DWR 方法,执行存储过程。 * */ public String updateSp() { String sql = "exec Pub_Sp_UpdateFieldMemo "; Connection conn = DBUtil.getConnection(); PreparedStatement psmt = null; try { psmt = conn.prepareStatement(sql); psmt.setString(1, "CALL"); boolean a = psmt.execute(); DBUtil.close(null, psmt, conn); conn = DBUtil.getConnection(); psmt = conn.prepareStatement(sql); psmt.setString(1, "USER"); boolean b = psmt.execute(); System.out.println(a); System.out.println(b); System.out.println("关闭弹窗执行存储过程 "+sql); } catch (SQLException e) { e.printStackTrace(); }finally { DBUtil.close(null, psmt, conn); } return null; } }
四、写一个JSP页面,主要导入engine.js和相应的java类映射的js文件,然后编写js即可调用
//以下为jsp的引入js文件部分 <script type="text/javascript"> //设置根路径,放在 tab.js 前 var glbRootPath = "<%=basePath%>"; </script> <script src="/UploadFiles/2021-04-02/lhgcore.min.js">五、js文件里调用后台代码
var basePath = ""; if(typeof(glbRootPath) != "undefined"){ basePath = glbRootPath; document.write("<script type='text/javascript' src='"+basePath+"\/dwr\/engine.js'><\/script><script type='text/javascript' src='"+basePath+"\/dwr\/interface\/CallTypeOpDwr.js'><\/script>"); } /* 中间省略若干代码 */ . . . if(typeof(glbRootPath) != "undefined"&&$(this).parent().children().eq(0).text()=="呼叫类型设置"){ console.log("进来了"); DWREngine.setAsync(false); CallTypeOpDwr.updateSp(); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。