Struts2,Spring,jquery兑现的登录例子
Struts2,Spring,jquery实现的登录例子1、创建工程:?2、创建数据库:(这里使用Mysql)?3、创建表及插入数据:?4、
Struts2,Spring,jquery实现的登录例子
1、创建工程:
?
2、创建数据库:(这里使用Mysql)
?
3、创建表及插入数据:
?
4、在工程中导入struts2和spring的相关包:
?
5、导入Jquery的包:
?
6、编写相应的Java文件:
BaseAction:
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.base;??
- ??
- import?javax.servlet.http.HttpServletRequest;??
- import?javax.servlet.http.HttpServletResponse;??
- ??
- import?org.apache.struts2.ServletActionContext;??
- ??
- import?com.opensymphony.xwork2.ActionContext;??
- import?com.opensymphony.xwork2.ActionSupport;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?class?BaseAction?extends?ActionSupport?{??
- ????protected?ActionContext?ctx?=?ActionContext.getContext();??
- ????/**?
- ?????*??
- ?????*/??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ????/**?
- ?????*?获取Response?
- ?????*?@return?
- ?????*/??
- ????protected?HttpServletResponse?getResponse(){??
- ????????HttpServletResponse?response?=?(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);??
- ????????return?response;??
- ????}??
- ????/**?
- ?????*?获取Request?
- ?????*?@return?
- ?????*/??
- ????protected?HttpServletRequest?getRequest(){??
- ????????HttpServletRequest?request?=?(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);??
- ????????return?request;??
- ????}??
- }??
?
LoginAction类:
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.login.action;??
- ??
- import?java.util.Map;??
- ??
- import?org.apache.struts2.json.JSONUtil;??
- ??
- import?com.gzedu.base.BaseAction;??
- import?com.gzedu.login.entity.Login;??
- import?com.gzedu.login.service.ILoginService;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?class?LoginAction?extends?BaseAction?{??
- ????private?ILoginService?loginService;??
- ????private?Login?login;??
- ????/**?
- ?????*??
- ?????*/??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ????/**?
- ?????*?登录事件?
- ?????*?@return?
- ?????*/??
- ????public?String?login()?throws?Exception{??
- ????????return?SUCCESS;??
- ????}??
- ????/**?
- ?????*?检查登录用户是否存在?
- ?????*?@return?
- ?????*/??
- ????public?String?checkLogin()throws?Exception{??
- ????????Map<String,String>?msgMap?=?loginService.login(login);??
- ????????boolean?isLogin?=?Boolean.valueOf(msgMap.get("isLogin"));??
- ??????????
- ????????String?json?=?"success";??
- ????????if(!isLogin){??
- ????????????json?=?"error";??
- ????????}??
- ??????????
- ????????json?=?JSONUtil.serialize(json);??
- ????????getResponse().setCharacterEncoding("utf-8");??
- ????????getResponse().getWriter().write(json);??
- ????????return?null;??
- ????}??
- ??????
- ????public?ILoginService?getLoginService()?{??
- ????????return?loginService;??
- ????}??
- ????public?void?setLoginService(ILoginService?loginService)?{??
- ????????this.loginService?=?loginService;??
- ????}??
- ??
- ??
- ????public?Login?getLogin()?{??
- ????????return?login;??
- ????}??
- ??
- ??
- ????public?void?setLogin(Login?login)?{??
- ????????this.login?=?login;??
- ????}??
- ??????
- }??
DAO接口:
?
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.login.dao;??
- ??
- import?java.util.Map;??
- ??
- import?com.gzedu.login.entity.Login;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?interface?ILoginDao?{??
- ????/**?
- ?????*?用户登录?
- ?????*?@param?login?用户登录信息?
- ?????*?@return?登录返回信息?
- ?????*/??
- ????public?Map<String,String>?login(Login?login);??
- }??
DAO实现类:
?
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.login.dao.impl;??
- ??
- ??
- import?java.util.HashMap;??
- import?java.util.List;??
- import?java.util.Map;??
- ??
- import?org.springframework.jdbc.core.support.JdbcDaoSupport;??
- ??
- import?com.gzedu.login.dao.ILoginDao;??
- import?com.gzedu.login.entity.Login;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?class?LoginDaoImpl?extends?JdbcDaoSupport?implements?ILoginDao{??
- ????@Override??
- ????public?Map<String,String>?login(Login?login)?{??
- ????????String?sql?=?"select?*?from?t_user?where?username?=???and?password?=??";??
- ??????????
- ????????List?list?=?getJdbcTemplate().queryForList(sql,new?Object[]{login.getUsername(),login.getPassword()});??
- ??????????
- ????????String?isLogin?=?"true";??
- ????????if(list.isEmpty()){??
- ????????????isLogin?=?"false";??
- ????????}??
- ????????Map<String,String>?map?=?new?HashMap<String,?String>();??
- ????????map.put("isLogin",?isLogin);??
- ????????return?map;??
- ????};??
- }??
Login中间层接口:
?
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.login.service;??
- ??
- import?java.util.Map;??
- ??
- import?com.gzedu.login.entity.Login;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?interface?ILoginService?{??
- ????/**?
- ?????*?用户登录?
- ?????*?@param?login?用户登录信息?
- ?????*?@return?登录返回信息?
- ?????*/??
- ????public?Map<String,String>?login(Login?login);??
- }??
中间层的实现类:
?
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.login.service.impl;??
- ??
- import?java.util.Map;??
- ??
- import?com.gzedu.login.dao.ILoginDao;??
- import?com.gzedu.login.entity.Login;??
- import?com.gzedu.login.service.ILoginService;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?class?LoginServiceImpl?implements?ILoginService{??
- ????private?ILoginDao?loginDao;??
- ??
- ????public?ILoginDao?getLoginDao()?{??
- ????????return?loginDao;??
- ????}??
- ??
- ????public?void?setLoginDao(ILoginDao?loginDao)?{??
- ????????this.loginDao?=?loginDao;??
- ????}??
- ??????
- ????@Override??
- ????public?Map<String,String>?login(Login?login)?{??
- ????????return?loginDao.login(login);??
- ????}??
- }??
login实体类:、
?
?
[java]?view plaincopy
- /**?
- ?*??
- ?*/??
- package?com.gzedu.login.entity;??
- ??
- /**?
- ?*?@author?ps?
- ?*?
- ?*/??
- public?class?Login?{??
- ????/**?
- ?????*?用户名?
- ?????*/??
- ????private?String?username;??
- ????/**?
- ?????*?密码?
- ?????*/??
- ????private?String?password;??
- ??????
- ????public?String?getUsername()?{??
- ????????return?username;??
- ????}??
- ????public?void?setUsername(String?username)?{??
- ????????this.username?=?username;??
- ????}??
- ????public?String?getPassword()?{??
- ????????return?password;??
- ????}??
- ????public?void?setPassword(String?password)?{??
- ????????this.password?=?password;??
- ????}??
- }??
applicationContext.xml配置
?
?
[html]?view plaincopy
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <beans???
- ????xmlns="http://www.springframework.org/schema/beans"??
- ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
- ????xmlns:aop="http://www.springframework.org/schema/aop"???
- ????xmlns:tx="http://www.springframework.org/schema/tx"??
- ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.1.xsd??
- ????http://www.springframework.org/schema/tx???
- ????http://www.springframework.org/schema/tx/spring-tx-3.1.xsd??
- ????http://www.springframework.org/schema/aop???
- ????http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">??
- ??
- ????<bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource">??
- ????????<property?name="username">??
- ????????????<value>root</value>??
- ????????</property>??
- ????????<property?name="password">??
- ????????????<value>root</value>??
- ????????</property>??
- ????????<property?name="driverClassName">??
- ????????????<value>com.mysql.jdbc.Driver</value>??
- ????????</property>??
- ????????<property?name="url">??
- ????????????<value>jdbc:mysql://127.0.0.1:3306/jqdb</value>??
- ????????</property>??
- ????</bean>??
- ????<!--?jdbcTemplate?配置?-->??
- ??????
- ????<!--?Login?配置?-->??
- ????<bean?id="loginDao"?class="com.gzedu.login.dao.impl.LoginDaoImpl">??
- ????????<property?name="dataSource"?ref="dataSource"?/>??
- ????</bean>??
- ????<bean?id="loginService"?class="com.gzedu.login.service.impl.LoginServiceImpl">??
- ????????<property?name="loginDao"?ref="loginDao"?/>??
- ????</bean>??
- ????<!--?//Login?配置?-->??
- </beans>??
login的xml
?
?
[html]?view plaincopy
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <!DOCTYPE?struts?PUBLIC??
- ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"??
- ????"http://struts.apache.org/dtds/struts-2.0.dtd">??
- <struts>??
- ????<package?name="loginpackage"?extends="struts-default"?namespace="/login">??
- ????????<action?name="login"?method="login"?class="com.gzedu.login.action.LoginAction">??
- ????????????<result?name="success">/pages/login/main.jsp</result>??
- ????????</action>??
- ????????<action?name="checkLogin"?method="checkLogin"?class="com.gzedu.login.action.LoginAction">??
- ????????</action>??
- ????</package>??
- ??????
- </struts>??
struts2 的xml
?
?
[html]?view plaincopy
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <!DOCTYPE?struts?PUBLIC??
- ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"??
- ????"http://struts.apache.org/dtds/struts-2.0.dtd">??
- ??
- <struts>??
- ??
- ????<constant?name="struts.devMode"?value="true"?/>??
- ????<constant?name="struts.objectFactory"?value="spring"?/>??
- ??????
- ????<!--?配置登录的配置文件?-->??
- ????<include?file="login.xml"></include>??
- </struts>??
struts.properties配置
?
?
[plain]?view plaincopy
- #该属性指定需要Struts?2处理的请求后缀,该属性的默认值是action,??
- #即所有匹配*.action的请求都由Struts?2处理.如果用户需要指定多个请求后缀,??
- #则多个后缀之间以英文逗号(,)隔开.??
- struts.action.extension?=?do??
?
jsp页面
?
login.jsp
?
[html]?view plaincopy
- <%@?page?language="java"?contentType="text/html;?charset=UTF-8"??
- ????pageEncoding="UTF-8"%>??
- <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">??
- <%??
- ????String?path?=?request.getContextPath();??
- %>??
- <html>??
- <head>??
- <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">??
- <title>登录</title>??
- <script?type="text/javascript"?src="<%=path?%>/js/jquery/1.7/jquery-1.7.1.min_production.js"></script>??
- </head>??
- <body>??
- ????<form?action="<%=path?%>/login/login.do"?method="post">??
- ????????用户名:<input?type="text"?name="login.username"?/><br/>??
- ????????密????码:<input??type="password"?name="login.password"?/><br/>??
- ????????<input?type="button"?value="登录"?onclick="login();">??
- ????????<input?type="button"?value="取消"><br/>??
- ????????<input?type="submit"?value="提交"?style="display:?none"><br/>??
- ????????<div?id="errMsg"?style="text-algin:center;color:?red"></div>??
- ????</form>??
- ??????
- ????<script?type="text/javascript">??
- ????????function?login(){??
- ????????????var?url?=?"<%=path?%>/login/checkLogin.do";??
- ????????????$.ajax({??
- ????????????????type?:?"POST",??
- ????????????????url?:?url,??
- ????????????????dataType?:?"json",??
- ????????????????data?:?{??
- ????????????????????"login.username":?$("input[name='login.username']").val(),??
- ????????????????????"login.password":?$("input[name='login.password']").val()??
- ????????????????????},??
- ????????????????success?:?function?(data){??
- ????????????????????if("error"?==?data){??
- ????????????????????????$("#errMsg").html("<p>用户名或者密码错误,请重新输入!</p>");??
- ??????????????????????????
- ????????????????????????setTimeout("$('#errMsg').html('')",10000);??
- ????????????????????}else{??
- ????????????????????????$("input[type=submit]").click();??
- ????????????????????}??
- ????????????????},??
- ????????????????error?:?function?(jqXHR,?textStatus,?errorThrown){??
- ????????????????????alert(textStatus);??
- ????????????????}??
- ????????????});??
- ????????}??
- ????</script>??
- </body>??
- </html>??
?
?
main.jsp
?
[html]?view plaincopy
- <%@?page?language="java"?contentType="text/html;?charset=UTF-8"??
- ????pageEncoding="UTF-8"%>??
- <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">??
- <html>??
- <head>??
- <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">??
- <title>主页</title>??
- </head>??
- <body>??
- ????<h2>我是主页,我怕谁!</h2>??
- </body>??
- </html>??
?
?
7、运行
?
正确登录:
1 楼 队长级人物 2012-04-27 很完整!!!