EasyUI学习总结(五)——EasyUI组件使用_AlbenXie的博客-程序员秘密_组件中使用easy-ring

技术标签: JS框架  

一、EasyUI组件的简单介绍

  easyUI提供了很多组件让我们使用,如下图所示:

  

  使用这些组件可以帮助我们快速地进行项目开发,下面以一个用户登录程序为例讲解EasyUI组件的使用

二、EasyUI组件的使用

2.1、创建测试的JavaWeb项目

  

2.2、编写测试代码

  编写一个用户登录页面Login1.html,用于输入用户名和密码进行登录,使用JQuery的ajax方法异步提交表单

  Login1.html的代码如下:

复制代码
  1 <!DOCTYPE html>
  2 <html>
  3   <head>
  4     <title>EasyUI组件使用范例</title>
  5     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6       <!-- 引入JQuery -->
  7       <script type="text/javascript" src="jquery-easyui-1.4.1/jquery.min.js"></script>
  8       <!-- 引入EasyUI -->
  9       <script type="text/javascript" src="jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
 10       <!-- 引入EasyUI的中文国际化js,让EasyUI支持中文 -->
 11       <script type="text/javascript" src="jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
 12       <!-- 引入EasyUI的样式文件-->
 13       <link rel="stylesheet" href="jquery-easyui-1.4.1/themes/default/easyui.css" type="text/css"/>
 14       <!-- 引入EasyUI的图标样式文件-->
 15       <link rel="stylesheet" href="jquery-easyui-1.4.1/themes/icon.css" type="text/css"/>
 16       <script type="text/javascript" src="js/Utils.js"></script>
 17       <script type="text/javascript">
 18           $(function(){
 19               //console.info(g_contextPath);
 20               //console.info(g_basePath);
 21               //页面加载完成之后创建登录的dialog
 22               $('#loginAndRegisterForm').dialog({   
 23                   title: '用户登录',
 24                   width: 240,   
 25                   height: 150,   
 26                   closable: false,//设置dialog不允许被关闭
 27                   cache: false,   
 28                   modal: true,
 29                   buttons:[
 30                                 {
 31                                 text:'登录',
 32                                 iconCls: 'icon-ok',
 33                                 width:70,
 34                                 height:30,
 35                                 handler:function(){
 36                                     //console.info(g_contextPath+'/servlet/LoginHandleServlet');
 37                                     //console.info(g_basePath+'/servlet/LoginHandleServlet');
 38                                     //console.info($('#loginForm').serialize());//在火狐中打印的结果:userName=gacl&userPwd=123
 39                                     loginHandle();//处理用户登录
 40                                 }
 41                             },
 42                             {
 43                                 text:'重置',
 44                                 iconCls: 'icon-ok',
 45                                 width:70,
 46                                 height:30,
 47                                 handler:function(){
 48                                     doReset('loginForm'); 
 49                                 }
 50                             }
 51                         ]
 52 
 53               }); 
 54               
 55               /*重置form表单*/
 56               function doReset(formId){
 57                   $(':input','#'+formId)
 58                    .not(':button, :submit, :reset, :hidden')
 59                    .val('')
 60                    .removeAttr('checked')
 61                    .removeAttr('selected');
 62               }
 63               
 64               /*处理用户登录*/
 65               function loginHandle(){
 66                   $.ajax({
 67                     //url:g_contextPath+'/servlet/LoginHandleServlet',
 68                     url:g_basePath+'/servlet/LoginHandleServlet',//url表示服务器端处理用户登录的URL地址
 69                     /*data:{
 70                         //data表示要提交到服务器端的数据,通常的写法
 71                         "userName":$("#userName").val(),
 72                         "userPwd":$("#userPwd").val()
 73                     },*/
 74                     //data表示要提交到服务器端的数据,更加简洁的写法
 75                     data:$('#loginForm').serialize(),//serialize()方法的作用是将form表单中的内容序列化成字符串
 76                     cahe:false,
 77                     /*
 78                     用dataType来指明服务器端返回的数据格式是一个json字符串,客户端接收到返回的json字符串之后,
 79                     Jquery会自动把这个json字符串转换成一个Json对象
 80                     */
 81                     dataType:'json',
 82                     success:function(r){
 83                         //此时的r已经是经过Jquery处理过之后的Json对象了
 84                         //console.info(r.msg);
 85                         if(r && r.success){
 86                             //调用dialog的close方法关闭dialog
 87                             $('#loginAndRegisterForm').dialog('close');
 88                             $.messager.show({
 89                                 title:'消息',
 90                                 msg:r.msg
 91                             });
 92                             //登录成功后跳转到系统首页
 93                             //window.location.replace(g_basePath+'/index.jsp');
 94                             //window.location.href = g_basePath+'/index.jsp';
 95                         }else{
 96                             $.messager.alert('消息',r.msg);
 97                         }
 98                     }
 99                 });
100               }
101           });
102       </script>
103       
104   </head>
105   
106   <body>
107       孤傲苍狼
108       <div id="loginAndRegisterForm">
109           <form method="post" id="loginForm">
110               <table>
111                   <tr>
112                       <th style="text-align:left;">
113                           用户名:
114                       </th>
115                       <td>
116                           <!-- class="easyui-textbox"表示使用EasyUI的textbox组件-->
117                           <input type="text" id="userName" style="width:150px;" name="userName" class="easyui-textbox"/>
118                       </td>
119                   </tr>
120                   <tr>
121                       <th style="text-align:left;">
122                           密码:
123                       </th>
124                       <td>
125                           <input type="password" id="userPwd" style="width:150px;" name="userPwd" class="easyui-textbox"/>
126                       </td>
127                   </tr>
128               </table>
129           </form>
130       </div>
131   </body>
132 </html>
复制代码

  Login1.html页面运行效果如下:

  

  Login1.html中用到了一个Utils.js,Utils.js中有两个方法:getBasePath和getContextPath,分别用于获取Web应用的basePath和contextPath,获取Web应用的basePath和contextPath的目的就是为了在提交form表单到指定的Sevlet中进行处理时拼凑出处理请求的Servlet的绝对路径

例如:

  url:g_contextPath+'/servlet/LoginHandleServlet'

  url:g_basePath+'/servlet/LoginHandleServlet'

  这样无论Servlet如何映射url-pattern,都可以正确找到该Servlet

  Utils.js代码如下:

复制代码
 1 //立即执行的js
 2 (function() {
 3     //获取contextPath
 4     var contextPath = getContextPath();
 5     //获取basePath
 6     var basePath = getBasePath();
 7     //将获取到contextPath和basePath分别赋值给window对象的g_contextPath属性和g_basePath属性
 8     window.g_contextPath = contextPath;
 9     window.g_basePath = basePath;
10 })();
11 
12 /**
13  * @author 孤傲苍狼
14  * 获得项目根路径,等价于jsp页面中
15  *  <%
16         String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
17     %>
18  * 使用方法:getBasePath();
19  * @returns 项目的根路径
20  *  
21  */
22 function getBasePath() {
23     var curWwwPath = window.document.location.href;
24     var pathName = window.document.location.pathname;
25     var pos = curWwwPath.indexOf(pathName);
26     var localhostPath = curWwwPath.substring(0, pos);
27     var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
28     return (localhostPath + projectName);
29 }
30 
31 /**
32  * @author 孤傲苍狼
33  * 获取Web应用的contextPath,等价于jsp页面中
34  *  <%
35         String path = request.getContextPath();
36     %>
37  * 使用方法:getContextPath();
38  * @returns /项目名称(/EasyUIStudy_20141104)
39  */
40 function getContextPath() {
41     return window.document.location.pathname.substring(0, window.document.location.pathname.indexOf('\/', 1));
42 };
复制代码

  处理用户登录请求的Servlet的LoginHandleServlet代码如下:

复制代码
 1 package me.gacl.web.controller;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import com.alibaba.fastjson.JSON;
11 
12 import me.gacl.custom.model.Json;
13 
14 public class LoginHandleServlet extends HttpServlet {
15 
16     public void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         //服务器端使用UTF-8编码将响应内容输出到客户端
19         response.setCharacterEncoding("UTF-8");
20         //通知客户端浏览器以UTF-8编码显示内容,避免产生中文乱码问题
21         response.setHeader("content-type", "text/html;charset=UTF-8");
22         String userName = request.getParameter("userName");
23         String userPwd = request.getParameter("userPwd");
24         Json json = new Json();
25         if (userName.equals("gacl") && userPwd.equals("123")) {
26             json.setMsg("登录成功");
27             json.setSuccess(true);
28         }else {
29             json.setMsg("用户名或密码错误,登录失败!");
30             json.setSuccess(false);
31         }
32         //使用alibaba(阿里巴巴)的fastJson工具类将Json对象转换成一个json字符串
33         String jsonStr = JSON.toJSONString(json);
34         //将json字符串作为响应内容输出到客户端浏览器。
35         response.getWriter().write(jsonStr);
36     }
37 
38     public void doPost(HttpServletRequest request, HttpServletResponse response)
39             throws ServletException, IOException {
40         doGet(request, response);
41     }
42 }
复制代码

  运行结果如下所示:

  

  Login1.html中是以传统的ajax异步提交form表单的,下面我们来看看如何使用EasyUI提供的form组件来实现相同的功能,编写一个Login2.html,代码如下:

复制代码
  1 <!DOCTYPE html>
  2 <html>
  3   <head>
  4     <title>EasyUI组件使用范例</title>
  5     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6      <!-- 引入JQuery -->
  7       <script type="text/javascript" src="jquery-easyui-1.4.1/jquery.min.js"></script>
  8       <!-- 引入EasyUI -->
  9       <script type="text/javascript" src="jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
 10       <!-- 引入EasyUI的中文国际化js,让EasyUI支持中文 -->
 11       <script type="text/javascript" src="jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
 12       <!-- 引入EasyUI的样式文件-->
 13       <link rel="stylesheet" href="jquery-easyui-1.4.1/themes/default/easyui.css" type="text/css"/>
 14       <!-- 引入EasyUI的图标样式文件-->
 15       <link rel="stylesheet" href="jquery-easyui-1.4.1/themes/icon.css" type="text/css"/>
 16       <script type="text/javascript" src="js/Utils.js"></script>
 17       <script type="text/javascript">
 18           $(function(){
 19               //console.info(g_contextPath);
 20               //console.info(g_basePath);
 21               $('#loginAndRegisterForm').dialog({   
 22                   title: '用户登录',
 23                   width: 240,   
 24                   height: 150,   
 25                   closable: false,//设置dialog不允许被关闭
 26                   cache: false,   
 27                   modal: true,
 28                     buttons:[
 29                                 {
 30                                 text:'登录',
 31                                 iconCls: 'icon-ok',
 32                                 width:70,
 33                                 height:30,
 34                                 handler:function(){
 35                                     //console.info(g_contextPath+'/servlet/LoginHandleServlet');
 36                                     //console.info(g_basePath+'/servlet/LoginHandleServlet');
 37                                     //console.info($('#loginForm').serialize());//在火狐中打印的结果:userName=gacl&userPwd=123
 38                                     loginHandle();//处理用户登录
 39                                 }
 40                             },
 41                             {
 42                                 text:'重置',
 43                                 iconCls: 'icon-ok',
 44                                 width:70,
 45                                 height:30,
 46                                 handler:function(){
 47                                     doReset('loginForm'); 
 48                                 }
 49                             }
 50                         ]
 51 
 52               }); 
 53               
 54               /*重置form表单*/
 55               function doReset(formId){
 56                   $(':input','#'+formId)
 57                    .not(':button, :submit, :reset, :hidden')
 58                    .val('')
 59                    .removeAttr('checked')
 60                    .removeAttr('selected');
 61               }
 62               
 63               /*处理用户登录*/
 64               function loginHandle(){
 65                   //使用EasyUI提供的form组件来提交表单
 66                   $('#loginForm').form('submit',{
 67                     //url:g_contextPath+'/servlet/LoginHandleServlet',
 68                     url:g_basePath+'/servlet/LoginHandleServlet',//url表示服务器端处理用户登录的URL地址
 69                     success:function(r){
 70                         //注意:此时的r只是一个普通的Json字符串,因此需要手动把它变成Json对象
 71                         //console.info(r);
 72                         //r = JSON.parse(r);//利用IE8支持的原生JSON对象的parse方法将json字符串转换成标准的JSON对象
 73                         //r=eval('('+r+')');//利用eval方法将将json字符串转换成标准的JSON对象
 74                         r = $.parseJSON(r);//利用Jquery的parseJSONfang将json字符串转换成标准的JSON对象
 75                         //console.info(r);
 76                         if(r && r.success){
 77                             //调用dialog的close方法关闭dialog
 78                             $('#loginAndRegisterForm').dialog('close');
 79                             $.messager.show({
 80                                 title:'消息',
 81                                 msg:r.msg
 82                             });
 83                             //登录成功后跳转到系统首页
 84                             //window.location.replace(g_basePath+'/index.jsp');
 85                             //window.location.href = g_basePath+'/index.jsp';
 86                         }else{
 87                             $.messager.alert('消息',r.msg);
 88                         }
 89                     }
 90                 });
 91               }
 92           });
 93       </script>
 94       
 95   </head>
 96   
 97   <body>
 98       孤傲苍狼
 99       <div id="loginAndRegisterForm">
100           <form method="post" id="loginForm">
101               <table>
102                   <tr>
103                       <th style="text-align:left;">用户名:</th>
104                       <!-- class="easyui-textbox"表示使用EasyUI的textbox组件-->
105                       <td><input type="text" id="userName" style="width:150px;" name="userName" class="easyui-textbox"/></td>
106                   </tr>
107                   <tr>
108                       <th style="text-align:left;">密码:</th>
109                       <td><input type="password" id="userPwd" style="width:150px;" name="userPwd" class="easyui-textbox"/></td>
110                   </tr>
111               </table>
112           </form>
113       </div>
114   </body>
115 </html>
复制代码

  运行效果如下:

  

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/AlbenXie/article/details/73832645

智能推荐

C - ZhangYu Speech (水题)_h1021456873的博客-程序员秘密

Descriptionas we all know, ZhangYu(Octopus vulgaris) brother has a very famous speech - “Keep some distance from me”. ZhangYu brother is so rich that everyone want to contact he, and scfkcf is o

curl不能支持https问题_curl不支持https_sylalak123的博客-程序员秘密

默认情况下,libcurl不支持https, 如果使用https链接,就会出现" Protocol https not supported or disabled in libcurl" 的错误提示。查看curl是否支持https可以使用命令:curl -V。解压:# tar -zxvf curl-7.49.0.tar.gz

黑马程序员-IOS UITableView NSIndexPath属性讲解_geng555zj的博客-程序员秘密

查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和delegate。  dataSource 是UITableViewDataSource类型,主要为UITableView提 供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和 reord

HTTP 文件下载时中文文件名乱码问题处理_DyanWang的博客-程序员秘密

之前有做文件下载处理,但由于文件名一直是英文的,所以并未发现有该问题,直到最近项目中有中文名出现.  以前的代码设置:header['Content-Disposition'] = 'attachment; filename=\"'+result['out_filename']+'\"';   现在的代码设置:result['out_filename'] = encodeURI

京东2012校园招聘软件研发笔试题_IT小卒的博客-程序员秘密

zhuanhttp://blog.csdn.net/lanyan822/article/details/7976381时间:2012-9-11 地点:川大我只能说第一家公司,不是一般的火爆。不得不吐槽一下:京东宣讲完全没有计划,只看到个下午两点半宣讲,结果跑过去,下午两点是宣讲管培的。在川大外的德克士呆了一下午。坑估计是没考虑那么多人,一个演播厅里面人山人海,连

struts2与jquery easyui中的easyui-datagrid出现异常ognl.MethodFailedException: Method_叶威1的博客-程序员秘密

jsp页面如下:String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>      ">        My

随便推点

十二、MyBatis框架(2)_u_hcy2000的博客-程序员秘密

文章目录1 回顾开发过程2 Mybatis连接池2.1 连接池初始化时机(DataSource)2.2 何时从连接池获取连接2.3 JNDI:(前提你的Mybatis环境必须是Web应用)(了解)3 动态SQL标签3.1 where和if标签3.2 foreach标签传入集合3.3 foreach标签传入数组3.4 foreach标签传入pojo对象4 MyBatis多表管理查询4.1 多表关联的关系分析4.2 多表关联的SQL语句表达4.3 一对一查询4.4 一对多查询4.5 多对多查询1 回顾开发过程

x4412 移植qt5.7显示图形界面_DreamGoo的博客-程序员秘密

       要移植qt图形界面到x4412前提是要保证x4412开发板的LCD驱动和触摸屏驱动已经移植ok。如果这两个已经可以正常使用,则参考博客https://blog.csdn.net/z961968549/article/details/82141861 这个博客已经详细介绍了tslib1.4和qt5.7的编译移植流程。我们只要按照博客上的步骤就可以正常编译生成我们需要的文件,然后直接拷贝...

超轻量级Web安全漏洞扫描工具Netsparker使用教程介绍_lc11535的博客-程序员秘密

Netsparker是一款web应用安全漏洞扫描工具Netsparter官网:https://www.netsparker.com/web-vulnerability-scanner/,与其他安全扫描工具相比更好检测SQL注入和跨站脚本攻击类型的安全漏洞。打开工具,点击start a new scan,选择full scan(全部扫描),单击开始在登录下进行扫描等待扫描...

ubuntu20.04 安装驱动+cuda11.1+cudnn8.0.5.39-1_ubuntu20.04安装cuda11.1_Ice0310的博客-程序员秘密

ubuntu20.04 安装驱动+cuda+cudnn安装驱动禁用nouveau驱动安装nvidia驱动卸载原有驱动更换国内源安装依赖安装nvidia驱动安装cuda卸载或删除原有的cuda安装cuda11.1安装cudnn删除原有的cudnn安装cudnn参考资料安装驱动禁用nouveau驱动首先禁用ubuntu系统自带的nouveau驱动sudo gedit /etc/modprobe.d/blacklist.conf在文本最后添加如下:blacklist rivafbblacklist

嵌套结构体初始化的几种方式_XDWX的博客-程序员秘密

转载自:https://www.cnblogs.com/skullboyer/p/8945818.html博主:壹点灵异来源:博客园#结构体定义structA definetypedef struct { char a; int b; float c;}struct_A_s;structB definetypedef struct{ char a; int b;...