步骤1:在com.bstek.dorado.sample.standardlesson.junior包中新建LoginService.java,代码如下:
package com.bstek.dorado.sample.standardlesson.junior;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.sample.standardlesson.dao.SlEmployeeDao;
import com.bstek.dorado.sample.standardlesson.entity.SlEmployee;
import com.bstek.dorado.web.DoradoContext;
@Component
public class LoginService {
@Resource
private SlEmployeeDao slEmployeeDao;
@Expose
public Map doLogin(Map param) {
String username = (String) param.get("username");
String password = (String) param.get("password");
Map result = new HashMap();
if (isValid(username, password)) {
// 设置验证成功需要跳转的页面
result.put("url",
"com.bstek.dorado.sample.standardlesson.junior.main.Main.d");
result.put("result", true);
return result;
} else {
String errormsg = "用户名或者密码不正确";
result.put("errormsg", errormsg);
result.put("result", false);
return result;
}
}
// 以下方法需要替换为自己的验证用户名密码的业务逻辑
public boolean isValid(String username, String password) {
DetachedCriteria dc = DetachedCriteria.forClass(SlEmployee.class);
if (username != null && !"".equals(username)) {
dc.add(Restrictions.eq("userName", username.toUpperCase()));
}
List<SlEmployee> employees = slEmployeeDao.find(dc);
if (employees.size() > 0) {
SlEmployee employee = employees.get(0);
if (password.equals(employee.getPassword())) {
// 验证成功后将用户信息放入session中
DoradoContext ctx = DoradoContext.getCurrent();
HttpServletRequest request = request = ctx.getRequest();
request.getSession().setAttribute("user", employee);
return true;
} else {
return false;
}
} else {
return false;
}
}
@Expose
public Map doLogout() {
DoradoContext ctx = DoradoContext.getCurrent();
HttpServletRequest request = ctx.getRequest();
request.getSession().setAttribute("user", null);
Map result = new HashMap();
result.put("url",
"com.bstek.dorado.sample.standardlesson.junior.Login.d");
result.put("result", true);
return result;
}
}
其实修改下doLogin方法,如下所示:
@Expose
public Map doLogin(String username, String password) {
Map result = new HashMap();
if (isValid(username,password)) {
//设置验证成功需要跳转的页面
result.put("url", "com.bstek.dorado.sample.standardlesson.junior.main.Main.d");
result.put("result", true);
return result;
} else {
String errormsg = "用户名或者密码不正确";
result.put("errormsg", errormsg);
result.put("result", false);
return result;
}
}
我们发现doLogin方法传入2个String类型的参数也是可以的,这是怎么回事呢?
这就需要了解Dorado7的智能方法适配,所谓职能方法适配是基于"约定优于配置"的设计思路而提供。这样在很多情况下我们不需要方法具有Dorado特性,也不需要引入Dorado的开发规范。由Dorado7自动判断这个方法中的各个参数以及返回值的用途。这是一种智能化的处理逻辑。这带给我们的好处:
- 几乎不需要引入任何Dorado7的API。
- 按照业务的需要而不是Dorado7的需要来为表现层编写代码。
- 可以令Dorado7与后台业务逻辑之间的粘合层代码更加简洁、易读。
在前台我们将entity作为参数传递给后台,后台就拿到一个Map参数,让后对loginService这个bean进行方法匹配,假如有方法适配了,那么就执行其方法。如果没有发现适配的方法,这个时候职能方法适配会将parameters中的参数拆开为username,password,这样就可以发现存在对应的doLogin方法,并进行调用。并将结果作为loginService调用的结果返回到前台。
本章登录功能完成后其实还有一点缺陷即并没有加入权限过滤器进行权限限制,由于这部分内容较简单,用户可以自行实现。