Extjs登录(用到DB)

来源:互联网 发布:java中类的生命周期 编辑:程序博客网 时间:2024/06/05 00:57
这是第一次用Extjs ,肯定很多漏洞,写给自己的,万一忘记了。。 演示-------------如下: 
 
 
 
 
 




 
[plain] view plaincopyprint?
  1.    
  2. Extjs版本:3.3.0  
  3.    
  4. 1 建表  
  5. use  Barcode  
  6. /*        (此表為了測試自己添加)  
  7.           EL_User :表名  
  8.           Id 自動增長類型的主鍵  
  9.           UserName 用戶名  
  10.           Password 密碼  
  11.           NickName 昵稱  
  12.           Level 級別(普通用戶或管理員)  
  13. */  
  14. drop table EL_User   
  15. create table EL_User(  
  16.           Id int identity(1,1)primary key,  
  17.           UserName nvarchar(30),  
  18.           Password nvarchar(20),  
  19.           NickName nvarchar(30),  
  20.           Level int       
  21. )  
  22. insert into EL_User values('舒婷','123456','微笑釋懷',1) --level級別 0代表普通用戶,1  
  23.   
  24.   
  25. 代表高級用戶  
  26. insert into EL_User values('Tom','123123','BigMan',0)  
  27. insert into EL_User values('dog','111111','玫瑰男人',0)  
  28. 2 登录界面 login.jsp  
  29.   
  30.   
  31. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  32. <%  
  33. String path = request.getContextPath();  
  34. String basePath = request.getScheme()+"://"+request.getServerName  
  35.   
  36.   
  37. ()+":"+request.getServerPort()+path+"/";  
  38. %>  
  39.    
  40. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  41. <html>  
  42.   <head>  
  43.     <base href="<%=basePath%>">  
  44.       
  45.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  46.     <meta http-equiv="description" content="this is my page">  
  47.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  48.       
  49.        <link rel="stylesheet" type="text/css" href="Extjs/3.3.0/resources/css/ext-  
  50.   
  51.   
  52. all.css"/>  
  53.     <script type="text/javascript" src="Extjs/3.3.0/adapter/ext/ext-base.js"></script>  
  54.     <script type="text/javascript" src="Extjs/3.3.0/ext-all.js"></script>  
  55.       
  56. <script type="text/javascript">  
  57.  Ext.onReady(function(){    
  58.      Ext.QuickTips.init();    
  59.      Ext.form.Field.prototype.msgTarget="qtip";    
  60.      var loginform = new Ext.form.FormPanel({    
  61.          renderTo:'container',    
  62.          title:'用戶登陆',    
  63.          labelSeparator:":",    
  64.          collapsible :true,    
  65.          width:300,    
  66.          height:150,    
  67.          id:'login',    
  68.          labelWidth: 80,    
  69.          bodyStyle: 'padding:5px 5px 0 0',    
  70.          border: false,    
  71.          name:'login',    
  72.          frame:true,    
  73.          defaults:{width:178,xtype:'textfield'},    
  74.          items:[    
  75.              new Ext.form.TextField({    
  76.              frame:true,    
  77.              fieldLabel:"用户名",    
  78.              name:'userName',    
  79.              allowBlank:false,    
  80.              blankText:'用户名不能为空'    
  81.          }),{    
  82.              fieldLabel:"密码",    
  83.              name:'password',    
  84.              allowBlank:false,    
  85.              inputType:'password',    
  86.              blankText:'密码不能为空'    
  87.          }],   
  88.          keys:{    
  89.              key: 13,    
  90.              fn:login    
  91.          },    
  92.          buttons:[{    
  93.              text:'提 交',    
  94.              handler:login    
  95.          },{    
  96.             text:'重 置',  
  97.             handler:function reset(){    
  98.                loginform.form.reset();    
  99.             }    
  100.          }]    
  101.      });    
  102.      function login(){  
  103.         var form=Ext.getCmp("login").getForm();  
  104.         form.submit({  
  105.           //clientValidation: true,  
  106.           url:"/Env_1/BarCode/selectUser.action",  
  107.           params:{  
  108.             
  109.           },  
  110.           success: function(form, action) {  
  111.           Ext.Msg.alert('Success', "恭喜,登陆成功!");  
  112.               location.href = 'index.jsp';  
  113.           },  
  114.           failure: function(form, action) {  
  115.             switch (action.failureType) {  
  116.                 case Ext.form.Action.CLIENT_INVALID:  
  117.                     Ext.Msg.alert('Failure', 'Form fields may not be submitted with   
  118.   
  119.   
  120. invalid values');  
  121.                     break;  
  122.                 case Ext.form.Action.CONNECT_FAILURE:  
  123.                     Ext.Msg.alert('Failure', 'Ajax communication failed');  
  124.                     break;  
  125.                 case Ext.form.Action.SERVER_INVALID:  
  126.                    Ext.Msg.alert('Failure', "您的输入有误,请核实后重新输入");  
  127.                    loginform.form.reset();    
  128.             }  
  129.           }  
  130.         });  
  131.      }           
  132.  })   
  133. </script>  
  134.   </head>  
  135. <body>  
  136.   <div id="outer" align="center">  
  137.   <br><br><br><br><br><br><br><br>   
  138.     <div id="container"></div>  
  139.   </div>  
  140.   </body>  
  141. </html>  
  142.   
  143.   
  144. 3 Model下的EL_User.java(实体类)  
  145.   
  146.   
  147. package com.barcode.Model;  
  148.    
  149. import javax.persistence.Column;  
  150. import javax.persistence.Entity;  
  151. import javax.persistence.Id;  
  152. import javax.persistence.Table;  
  153. @Entity  
  154. @Table(name="EL_User")  
  155. public class EL_User {  
  156.     private int Id;  
  157.     private String UserName;  
  158.     private String Password;  
  159.     private String NickName;  
  160.     private int level;  
  161.     @Id  
  162.     @Column(name="Id")  
  163.     public int getId() {  
  164.         return Id;  
  165.     }  
  166.     public void setId(int id) {  
  167.         Id = id;  
  168.     }  
  169.     @Column(name="UserName")  
  170.     public String getUserName() {  
  171.         return UserName;  
  172.     }  
  173.     public void setUserName(String userName) {  
  174.         UserName = userName;  
  175.     }  
  176.     @Column(name="Password")  
  177.     public String getPassword() {  
  178.         return Password;  
  179.     }  
  180.     public void setPassword(String password) {  
  181.         Password = password;  
  182.     }  
  183.     @Column(name="NickName")  
  184.     public String getNickName() {  
  185.         return NickName;  
  186.     }  
  187.     public void setNickName(String nickName) {  
  188.         NickName = nickName;  
  189.     }  
  190.     @Column(name="Level")  
  191.     public int getLevel() {  
  192.         return level;  
  193.     }  
  194.     public void setLevel(int level) {  
  195.         this.level = level;  
  196.     }  
  197. }  
  198. dao层  
  199. @Repository  
  200. public class ELLoginDao {  
  201.    
  202.    
  203.           private HibernateTemplate hibernateTemplate;  
  204.           @SuppressWarnings("unused")  
  205.           @Autowired  
  206.           private void setHibernateTemplate (SessionFactory sessionFactory){  
  207.                     this.hibernateTemplate =new HibernateTemplate(sessionFactory);  
  208.           }  
  209.             
  210.           @SuppressWarnings("unchecked")  
  211.           public List isHaveUser(String username,String password){  
  212.                     String hql = "from ELUser u where u.userName = '"+username+"' and   
  213.   
  214.   
  215. u.password = '"+password+"'";  
  216.                     System.out.println("hql"+hql);  
  217.                      return this.hibernateTemplate.find(hql);  
  218.                       
  219.              
  220.                       
  221.           }  
  222. }  
  223.    
  224. 4 service层  
  225. package com.barcode.Service;  
  226.    
  227. import java.util.Iterator;  
  228. import java.util.List;  
  229.    
  230. import org.springframework.beans.factory.annotation.Autowired;  
  231. import org.springframework.stereotype.Service;  
  232. import org.springframework.transaction.annotation.Transactional;  
  233.    
  234. import com.barcode.DAO.EL_UserDAO;  
  235. import com.barcode.Model.EL_User;  
  236.    
  237. @Service  
  238. public class EL_UserService {  
  239.     @SuppressWarnings("unused")  
  240.     private EL_UserDAO el_UserDAO;  
  241.     @Autowired  
  242.     public void setEl_UserDAO(EL_UserDAO el_UserDAO) {  
  243.         this.el_UserDAO = el_UserDAO;  
  244.     }  
  245.     @Transactional("barcode")  
  246.     public boolean selectUser(String username,String password){  
  247.         List<EL_User>list= this.el_UserDAO.selectUser(username,password);  
  248.         if(list.size()==0){  
  249.             return false;  
  250.         }else{  
  251.             return true;  
  252.         }  
  253.     }  
  254. }  
  255.   
  256.   
  257. 5 web层  
  258.   
  259.   
  260. package com.barcode.Web;  
  261.    
  262. import java.util.Enumeration;  
  263. import java.util.HashMap;  
  264. import java.util.Map;  
  265.    
  266. import javax.servlet.http.HttpServletRequest;  
  267.    
  268. import org.springframework.beans.factory.annotation.Autowired;  
  269. import org.springframework.stereotype.Controller;  
  270. import org.springframework.web.bind.annotation.ModelAttribute;  
  271. import org.springframework.web.bind.annotation.RequestMapping;  
  272. import org.springframework.web.bind.annotation.ResponseBody;  
  273.    
  274. import com.barcode.Model.EL_User;  
  275. import com.barcode.Service.EL_UserService;  
  276.    
  277. @Controller  
  278. public class EL_UserController {  
  279.       
  280.     private EL_UserService el_UserService;  
  281.     @Autowired  
  282.      public void setEl_UserService(EL_UserService el_UserService) {  
  283.         this.el_UserService = el_UserService;  
  284.     }  
  285.     @ModelAttribute("user")  
  286.     public EL_User formbackObject(Integer id, HttpServletRequest request) throws   
  287.   
  288.   
  289. Exception{  
  290.         //獲得更改的各項屬性  
  291.         Enumeration enumeration = request.getParameterNames();  
  292.         System.out.println(enumeration.hasMoreElements());  
  293.         while (enumeration.hasMoreElements()) {  
  294.              String paramName = (String) enumeration.nextElement();  
  295.              System.out.println("request parameter [" + paramName + "]=" +  
  296.              request.getParameter(paramName));  
  297.         }  
  298.         EL_User user=null;  
  299.         if(id!=null&&id!=0){  
  300.             //ug = us.getUsefulGroup(id);  
  301.         }else{  
  302.             user = new EL_User();  
  303.         }  
  304.         return user;  
  305.     }  
  306.       
  307.     @SuppressWarnings("unchecked")  
  308.     @RequestMapping(value="BarCode/selectUser.action")  
  309.     public @ResponseBody Map<String,? extends Object> selectUser(@ModelAttribute  
  310.   
  311.   
  312. ("user") EL_User user,HttpServletRequest request){  
  313.         System.out.println(user.getUserName());  
  314.         System.out.println(user.getPassword());  
  315.         String username=user.getUserName();  
  316.         String password=user.getPassword();  
  317.         boolean boo=this.el_UserService.selectUser(username,password);  
  318.         Map map=new HashMap<String,Object>();  
  319.         if(boo){  
  320.         map.put("data",user);  
  321.         map.put("success",true);  
  322.         map.put("message","You are successful !!");  
  323.         }else{  
  324.             map.put("failure", false);  
  325.             map.put("message","username or password is error!");  
  326.         }  
  327.         return map;  
  328.     }  
  329. }