登录思路: 前台发送一个请求,然后通过spring的自己主动注參注入username和password,将password加密后与数据库中查找的做比較。返回是否通过。
这里还使用了EasyUI的校验控件。不得不说,使用框架真快。不须要写那么多繁杂的脚本代码。
1.页面代码
<form id="ff" method="post" action="UserInfo_login"> <div><label for="name">邮箱:</label> <input id="name" name="email" type="text" /></div> <div><label for="email">密码:</label> <input id="pwd" name="passwords" type="password"/></div> </form>
2.脚本代码
<script> $(function() { $('#login').dialog({ title : 'CRM后台登录', modal : true, closable : false, draggable:false, buttons : [ { text : '登录', iconCls : 'icon-ok', handler : function() { var isValid = $('#ff').form('validate'); if (!isValid){ return false; }else{ $('#ff').submit(); } } } ], }); start(); }); function start() { $('#name').validatebox({ required : true, validType : 'email', missingMessage : "邮箱为空 ", invalidMessage : "请输入正确的邮箱" }); $('#pwd').validatebox({ required : true, missingMessage : "password为空 " }); } </script>
3.Action层代码
注解:
@Controller("userInfoAction") public class UserInfoAction implements ModelDriven<UserInfo>, SessionAware { @Autowired private UserInfoService userInfoService; private UserInfo userInfo; private String result; private Map<String, Object> session; private Object jsondata;
解释一下:
UserInfoService: 通过spring的自己主动注參能够取到业务类对象。
UserInfo: 通过模型驱动,实现getModel方法。就可以获取页面上的输入控件的值,仅仅要name为userInfo中的属性就可以。
result: 是为了转发的,比如登录失败。多种情况时。
session: session的取得是通过实现SessionAware接口,还要实现一个setSession方法。
当然也有ActionContext和ServletActionContext,这样的原理是基于IOC(控制反转)为Action注入參数。其原理就是反射。
jsondata: 这个是为了转发json数据而存在的。
简单来说,这里面的东西就是一个大容器。到处都是spring的自己主动注參。特别是service层,action层,数据持久层,连structs转发也有它的身影。
mybatis+spring+structs2
public String login() { String password = userInfo.getPasswords(); // 原密码 userInfo.setPasswords(Encrypt.md5(userInfo.getPasswords())); userInfo = userInfoService.login(userInfo); if (userInfo != null) { userInfo.setPasswords(password); // 在页面上显示的是未加密的密码,便于改动 session.put(CrmConstants.USERINFO, userInfo); //存入session中 result = ""; if (userInfo.getPl() == 2) { // 老板 result += "boss/frame"; // ${result}.jsp -> workers/frame.jsp } else if (userInfo.getPl() == 1) { // 员工 result += "workers/frame"; } else if (userInfo.getPl() == 0) { // 客户 result += "customers/frame"; } else if (userInfo.getPl() == 3) { // 超级管理员 result += "workers/frame"; } return "login_success"; } else { return "fail"; } }
4.Service业务层代码
@Service("userInfoService") public class UserInfoServiceImpl implements UserInfoService{ <span style="white-space:pre"> </span>@Autowired <span style="white-space:pre"> </span>private UserInfoMapper userInfoMapper;
方法
public UserInfo login(UserInfo userInfo) { return userInfoMapper.findUser(userInfo); }
5.数据持久层
public interface UserInfoMapper { UserInfo getUserInfoById(int id); int insertUserInfo(UserInfo userInfo); UserInfo findUser(UserInfo userInfo); int updateUserInfoDate(UserInfo userInfo); int vailEmail(String email); int modify(UserInfo userInfo); int deleteUserByUid(int uid); }
UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.yc.crm.mapper.UserInfoMapper"> <resultMap id="BaseResultMap" type="com.yc.crm.entity.UserInfo"> <id column="CRM_ID" property="uid" /> <result column="EMAIL" property="email" /> <result column="PASSWORDS" property="passwords" /> <result column="CNAME" property="cname" /> <result column="PHONE" property="phone" /> <result column="SEX" property="sex" /> <result column="AGE" property="age" /> <result column="ADDRESS" property="address" /> <result column="USERLEVEL" property="level" /> <result column="PL" property="pl" /> <result column="CREATEDATE" property="createdate" /> <association property="bussiness" resultMap="com.yc.crm.mapper.BusinessMapper.BaseResultMap" /> </resultMap> <select id="findUser" parameterType="UserInfo" resultMap="BaseResultMap"> select * from crm_user_info u join crm_business b on u.bussiness_id=b.business_id where u.email=#{email} and u.passwords=#{passwords} </select> </mapper>
说明的是: 1. 数据库不同。编写的语句也不同。比如mysql的获取自增主键ID和oracle不同,这个问题至少困扰了几天。
2. 查询的时候。由于实体类和数据库中的字段不同,比如字段为企业ID,而实体类中属性为企业实体类,这个时候查询须要使用join将business表联合起来,否则获取的business实体对象除了ID。其余属性皆为空。
3.假设在这里使用了keyword,不必惊慌。能够加上"UID"来解决。加了"UID"必须大写。
4.mybatis中的标签要合理使用,比如update时候使用if-set标签。if标签的整形推断条件,以下来几个坑过我的样例。
eg 1.0
<update id="modify" parameterType="UserInfo"> update crm_user_info <set> <if test="passwords!=null and passwords!=''"> passwords=#{passwords}, </if> phone=#{phone},sex=#{sex},"AGE"=#{age},userlevel=#{level},address=#{address} where email=#{email} </set> </update>
eg 1.1
<select id="find" parameterType="OrderDetail" resultMap="BaseResultMap"> select * from crm_order_detail det join crm_order de on det.o_id=de.o_id join crm_user_info cu on cu.crm_id=de.c_id join crm_gods gods on gods.g_id=det.g_id where de.e_id=#{order.worker.uid} <if test="state!=null and state>=0"> and det.state=#{state} </if> <if test="god!=null and god.name!=null and god.name!='' "> and gods.g_name like '%'||#{god.name}||'%' </if> <!--<if test="odate!=null and udate!=null"> <![CDATA[ >= ]]> and odate between to_date(#{odate},'yyyy-mm-dd') and to_date(#{udate},'yyyy-mm-dd') </if> --> <if test="order!=null and order.customer!=null and order.customer.cname!=null and order.customer.cname!=''"> and cu.cname like '%'||#{order.customer.cname}||'%' </if> </select>
1.须要说明的是,上面的日期我还没解决。从前台控件取的日期,到action层为java.util.Date,而数据库的是java.sql.Date,这倆者须要在oracle比較。
2.上面的推断条件不是一步到位,而是一步一步的推断是否为空。
order!=null and order.customer!=null and order.customer.cname!=null and order.customer.cname!=''
3.like查询使用了字符拼接。使用自带函数我没有成功过。
至于找回password,发送请求,后台发送一封邮件至客户邮箱,这个技术不是非常难。
我是菜鸟,我在路上。