Struts2,Spring,jquery兑现的登录例子

Struts2,Spring,jquery实现的登录例子

1、创建工程:

Struts2,Spring,jquery兑现的登录例子

 

2、创建数据库:(这里使用Mysql)

Struts2,Spring,jquery兑现的登录例子

 

3、创建表及插入数据:

Struts2,Spring,jquery兑现的登录例子

 

4、在工程中导入struts2和spring的相关包:

Struts2,Spring,jquery兑现的登录例子

 

5、导入Jquery的包:

Struts2,Spring,jquery兑现的登录例子

 

6、编写相应的Java文件:

Struts2,Spring,jquery兑现的登录例子

BaseAction:

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.base;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10.   
  11. import com.opensymphony.xwork2.ActionContext;  
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. /** 
  15.  * @author ps 
  16.  * 
  17.  */  
  18. public class BaseAction extends ActionSupport {  
  19.     protected ActionContext ctx = ActionContext.getContext();  
  20.     /** 
  21.      *  
  22.      */  
  23.     private static final long serialVersionUID = 1L;  
  24.     /** 
  25.      * 获取Response 
  26.      * @return 
  27.      */  
  28.     protected HttpServletResponse getResponse(){  
  29.         HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);  
  30.         return response;  
  31.     }  
  32.     /** 
  33.      * 获取Request 
  34.      * @return 
  35.      */  
  36.     protected HttpServletRequest getRequest(){  
  37.         HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);  
  38.         return request;  
  39.     }  
  40. }  


 

LoginAction类:

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.login.action;  
  5.   
  6. import java.util.Map;  
  7.   
  8. import org.apache.struts2.json.JSONUtil;  
  9.   
  10. import com.gzedu.base.BaseAction;  
  11. import com.gzedu.login.entity.Login;  
  12. import com.gzedu.login.service.ILoginService;  
  13.   
  14. /** 
  15.  * @author ps 
  16.  * 
  17.  */  
  18. public class LoginAction extends BaseAction {  
  19.     private ILoginService loginService;  
  20.     private Login login;  
  21.     /** 
  22.      *  
  23.      */  
  24.     private static final long serialVersionUID = 1L;  
  25.     /** 
  26.      * 登录事件 
  27.      * @return 
  28.      */  
  29.     public String login() throws Exception{  
  30.         return SUCCESS;  
  31.     }  
  32.     /** 
  33.      * 检查登录用户是否存在 
  34.      * @return 
  35.      */  
  36.     public String checkLogin()throws Exception{  
  37.         Map<String,String> msgMap = loginService.login(login);  
  38.         boolean isLogin = Boolean.valueOf(msgMap.get("isLogin"));  
  39.           
  40.         String json = "success";  
  41.         if(!isLogin){  
  42.             json = "error";  
  43.         }  
  44.           
  45.         json = JSONUtil.serialize(json);  
  46.         getResponse().setCharacterEncoding("utf-8");  
  47.         getResponse().getWriter().write(json);  
  48.         return null;  
  49.     }  
  50.       
  51.     public ILoginService getLoginService() {  
  52.         return loginService;  
  53.     }  
  54.     public void setLoginService(ILoginService loginService) {  
  55.         this.loginService = loginService;  
  56.     }  
  57.   
  58.   
  59.     public Login getLogin() {  
  60.         return login;  
  61.     }  
  62.   
  63.   
  64.     public void setLogin(Login login) {  
  65.         this.login = login;  
  66.     }  
  67.       
  68. }  

DAO接口:

 

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.login.dao;  
  5.   
  6. import java.util.Map;  
  7.   
  8. import com.gzedu.login.entity.Login;  
  9.   
  10. /** 
  11.  * @author ps 
  12.  * 
  13.  */  
  14. public interface ILoginDao {  
  15.     /** 
  16.      * 用户登录 
  17.      * @param login 用户登录信息 
  18.      * @return 登录返回信息 
  19.      */  
  20.     public Map<String,String> login(Login login);  
  21. }  

DAO实现类:

 

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.login.dao.impl;  
  5.   
  6.   
  7. import java.util.HashMap;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. import org.springframework.jdbc.core.support.JdbcDaoSupport;  
  12.   
  13. import com.gzedu.login.dao.ILoginDao;  
  14. import com.gzedu.login.entity.Login;  
  15.   
  16. /** 
  17.  * @author ps 
  18.  * 
  19.  */  
  20. public class LoginDaoImpl extends JdbcDaoSupport implements ILoginDao{  
  21.     @Override  
  22.     public Map<String,String> login(Login login) {  
  23.         String sql = "select * from t_user where username = ? and password = ?";  
  24.           
  25.         List list = getJdbcTemplate().queryForList(sql,new Object[]{login.getUsername(),login.getPassword()});  
  26.           
  27.         String isLogin = "true";  
  28.         if(list.isEmpty()){  
  29.             isLogin = "false";  
  30.         }  
  31.         Map<String,String> map = new HashMap<String, String>();  
  32.         map.put("isLogin", isLogin);  
  33.         return map;  
  34.     };  
  35. }  

Login中间层接口:

 

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.login.service;  
  5.   
  6. import java.util.Map;  
  7.   
  8. import com.gzedu.login.entity.Login;  
  9.   
  10. /** 
  11.  * @author ps 
  12.  * 
  13.  */  
  14. public interface ILoginService {  
  15.     /** 
  16.      * 用户登录 
  17.      * @param login 用户登录信息 
  18.      * @return 登录返回信息 
  19.      */  
  20.     public Map<String,String> login(Login login);  
  21. }  

中间层的实现类:

 

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.login.service.impl;  
  5.   
  6. import java.util.Map;  
  7.   
  8. import com.gzedu.login.dao.ILoginDao;  
  9. import com.gzedu.login.entity.Login;  
  10. import com.gzedu.login.service.ILoginService;  
  11.   
  12. /** 
  13.  * @author ps 
  14.  * 
  15.  */  
  16. public class LoginServiceImpl implements ILoginService{  
  17.     private ILoginDao loginDao;  
  18.   
  19.     public ILoginDao getLoginDao() {  
  20.         return loginDao;  
  21.     }  
  22.   
  23.     public void setLoginDao(ILoginDao loginDao) {  
  24.         this.loginDao = loginDao;  
  25.     }  
  26.       
  27.     @Override  
  28.     public Map<String,String> login(Login login) {  
  29.         return loginDao.login(login);  
  30.     }  
  31. }  

login实体类:、

 

 

[java] view plaincopy
  1. /** 
  2.  *  
  3.  */  
  4. package com.gzedu.login.entity;  
  5.   
  6. /** 
  7.  * @author ps 
  8.  * 
  9.  */  
  10. public class Login {  
  11.     /** 
  12.      * 用户名 
  13.      */  
  14.     private String username;  
  15.     /** 
  16.      * 密码 
  17.      */  
  18.     private String password;  
  19.       
  20.     public String getUsername() {  
  21.         return username;  
  22.     }  
  23.     public void setUsername(String username) {  
  24.         this.username = username;  
  25.     }  
  26.     public String getPassword() {  
  27.         return password;  
  28.     }  
  29.     public void setPassword(String password) {  
  30.         this.password = password;  
  31.     }  
  32. }  

applicationContext.xml配置

 

 

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans   
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"   
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  8.     http://www.springframework.org/schema/tx   
  9.     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  10.     http://www.springframework.org/schema/aop   
  11.     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">  
  12.   
  13.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  14.         <property name="username">  
  15.             <value>root</value>  
  16.         </property>  
  17.         <property name="password">  
  18.             <value>root</value>  
  19.         </property>  
  20.         <property name="driverClassName">  
  21.             <value>com.mysql.jdbc.Driver</value>  
  22.         </property>  
  23.         <property name="url">  
  24.             <value>jdbc:mysql://127.0.0.1:3306/jqdb</value>  
  25.         </property>  
  26.     </bean>  
  27.     <!-- jdbcTemplate 配置 -->  
  28.       
  29.     <!-- Login 配置 -->  
  30.     <bean id="loginDao" class="com.gzedu.login.dao.impl.LoginDaoImpl">  
  31.         <property name="dataSource" ref="dataSource" />  
  32.     </bean>  
  33.     <bean id="loginService" class="com.gzedu.login.service.impl.LoginServiceImpl">  
  34.         <property name="loginDao" ref="loginDao" />  
  35.     </bean>  
  36.     <!-- //Login 配置 -->  
  37. </beans>  

login的xml

 

 

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <package name="loginpackage" extends="struts-default" namespace="/login">  
  7.         <action name="login" method="login" class="com.gzedu.login.action.LoginAction">  
  8.             <result name="success">/pages/login/main.jsp</result>  
  9.         </action>  
  10.         <action name="checkLogin" method="checkLogin" class="com.gzedu.login.action.LoginAction">  
  11.         </action>  
  12.     </package>  
  13.       
  14. </struts>  

struts2 的xml

 

 

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.devMode" value="true" />  
  9.     <constant name="struts.objectFactory" value="spring" />  
  10.       
  11.     <!-- 配置登录的配置文件 -->  
  12.     <include file="login.xml"></include>  
  13. </struts>  

struts.properties配置

 

 

[plain] view plaincopy
  1. #该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,  
  2. #即所有匹配*.action的请求都由Struts 2处理.如果用户需要指定多个请求后缀,  
  3. #则多个后缀之间以英文逗号(,)隔开.  
  4. struts.action.extension = do  


 

jsp页面

Struts2,Spring,jquery兑现的登录例子

 

login.jsp

 

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <%  
  5.     String path = request.getContextPath();  
  6. %>  
  7. <html>  
  8. <head>  
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  10. <title>登录</title>  
  11. <script type="text/javascript" src="<%=path %>/js/jquery/1.7/jquery-1.7.1.min_production.js"></script>  
  12. </head>  
  13. <body>  
  14.     <form action="<%=path %>/login/login.do" method="post">  
  15.         用户名:<input type="text" name="login.username" /><br/>  
  16.         密    码:<input  type="password" name="login.password" /><br/>  
  17.         <input type="button" value="登录" onclick="login();">  
  18.         <input type="button" value="取消"><br/>  
  19.         <input type="submit" value="提交" style="display: none"><br/>  
  20.         <div id="errMsg" style="text-algin:center;color: red"></div>  
  21.     </form>  
  22.       
  23.     <script type="text/javascript">  
  24.         function login(){  
  25.             var url = "<%=path %>/login/checkLogin.do";  
  26.             $.ajax({  
  27.                 type : "POST",  
  28.                 url : url,  
  29.                 dataType : "json",  
  30.                 data : {  
  31.                     "login.username": $("input[name='login.username']").val(),  
  32.                     "login.password": $("input[name='login.password']").val()  
  33.                     },  
  34.                 success : function (data){  
  35.                     if("error" == data){  
  36.                         $("#errMsg").html("<p>用户名或者密码错误,请重新输入!</p>");  
  37.                           
  38.                         setTimeout("$('#errMsg').html('')",10000);  
  39.                     }else{  
  40.                         $("input[type=submit]").click();  
  41.                     }  
  42.                 },  
  43.                 error : function (jqXHR, textStatus, errorThrown){  
  44.                     alert(textStatus);  
  45.                 }  
  46.             });  
  47.         }  
  48.     </script>  
  49. </body>  
  50. </html>  


 

 

main.jsp

 

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>主页</title>  
  8. </head>  
  9. <body>  
  10.     <h2>我是主页,我怕谁!</h2>  
  11. </body>  
  12. </html>  


 

 

7、运行

Struts2,Spring,jquery兑现的登录例子

 

正确登录:

Struts2,Spring,jquery兑现的登录例子

1 楼 队长级人物 2012-04-27  
很完整!!!