本文经过自身测试,可以需要修改一些参数;若是转载,请标注本文所在地址,谢谢!
一、创建User对象:
/**
UserBean
*/
public class User implements Serializable{
private Integer primaryId;
private String userName;
private String passWord;
//set/get方法
....
}
二、配置user.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dss.model.User" table="MEMB_MS" lazy="true">
<id name="primaryId" column="MEMB_CODE"/>
<property name="userName" column="MEMB_ID"></property>
<property name="passWord" column="MEMB_PWD"></property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dss.model.User" table="MEMB_MS" lazy="true">
<id name="primaryId" column="MEMB_CODE"/>
<property name="userName" column="MEMB_ID"></property>
<property name="passWord" column="MEMB_PWD"></property>
</class>
</hibernate-mapping>
三、编写DAO层、service层、action层
1、DAO层
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
SQLQuery query;
public boolean isLogin(String userName, String passWord) {
String hql = "from User where userName = ? and passWord = ?";
try {
@SuppressWarnings("rawtypes")
List result = getHibernateTemplate().find(hql, new Object[] { userName, passWord });
System.out.println(hql);
if (result.size() > 0)
return true;
} catch (Exception ex) {
ex.getStackTrace();
}
return false;
}
}
2、service层
/**
* 判断登录service实现类
*/
public boolean isLogin(String userName, String passWord) {
try {
return this.userDao.isLogin(userName, passWord);
} catch (Exception ex) {
System.out.println("判断是否登录成功异常:" + ex.getMessage());
}
return false;
}
3、action层
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
private HttpServletRequest request = null;
private String result;
//创建get\set方法...
/**
* 登录
* @return
*/
public String loginMethod() {
String status;
try {
request = ServletActionContext.getRequest();
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
System.out.println("参数:"+userName+"\t"+passWord);
JSONObject json = new JSONObject(); //创建json对象
if(!userService.isLogin(userName, passWord)){
status = "false";
json.put("message", status); //将结果转换为json对象
result = json.get("message").toString(); //将json对象赋值给result返回到前台
System.out.println("错误时返回结果:"+json);
return "input";
}else{
status = "true";
json.put("message", status); //将结果转换为json对象
result = json.get("message").toString(); //将json对象赋值给result返回到前台
System.out.println("正确时返回结果:"+json);
System.out.println("登录成功...");
return "success";
}
} catch (Exception ex) {
System.out.println("查询异常:"+ex.getMessage());
ex.printStackTrace();
}
return "input";
}
四、struts.xml配置
<!-- 登录action ,jsonAjax -->
<action name="loginAction" class="com.dss.controller.UserAction" method="loginMethod">
<!-- 类名需要修改 -->
<result name="success" type="json">
<param name="root">result</param>
</result>
<result name="input" type="json">
<param name="root">result</param>
</result>
<interceptor-ref name="myStack"></interceptor-ref>
</action>
五、编写jsp页面将前台数据通过ajax传到后台
(只显示ajax提交代码..)
var data = {
"userName" : userName,
"passWord" : passWord
};
var returnVal;
$.ajax({
type : "post",
url : "loginAction.action", //传入struts.xml中配置好的action名称
data : data,
async : true,
dataType : "json",//设置需要返回的数据类型
success : function(data){
returnVal = eval("("+data+")");
if(returnVal.toString() == "true"){//登录成功跳转
window.location.href = "infoPersonal.jsp";
}else if(returnVal.toString() == "false"){//提示错误
var mess = "UserName Or PassWord Error";
//jquery的layer插件
layer.msg(mess,{
time:2000, //时间为5秒
maxWidth : '300px', //最大宽度为300px
shade : [0.1,'#000',true], //弹出遮罩(透明度,颜色,是否弹出)
});
}
}
});
html页面只需把form表单中的按钮改为button类型即可;