阅读:8133回复:1
如何实现自定义登录失败提示信息
当登录失败时,如何实现自定义提示消息,如下图:
图片:QQ图片20160329174443.jpg |
|
沙发#
发布于:2016-03-29 19:08
通过FacesContext.getCurrentInstance().addMessage方法在登录实现代码中添加自定义FacesMessage,后台代码格式如下:
package com.sunsheen.jfids.dsi.sysman.security.login; import java.util.List; import java.util.Map; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import org.hibernate.Query; import org.hibernate.transform.Transformers; import org.jboss.seam.security.Identity; import org.jboss.seam.web.IdentityRequestWrapper; import org.osoa.sca.annotations.Reference; import com.sunsheen.jfids.dsi.sysman.department.Ts_department; import com.sunsheen.jfids.dsi.sysman.security.login.authgroup.Role; import com.sunsheen.jfids.system.base.composite.dao.IDao; import com.sunsheen.jfids.system.base.composite.data.query.QueryParameterImpl; import com.sunsheen.jfids.system.security.login.ILogin; import com.sunsheen.jfids.system.sysman.ISysUser; public class LoginImpl implements ILogin { IDao dao; @Reference public void setDao(IDao dao) { this.dao = dao; } @Override public ISysUser doLogin(Identity identity, Map data) { FacesContext.getCurrentInstance().addMessage( null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "用户名密码错误55", "谁也不知道是什么问题这是详细描述信息")); SysUser currUser = new SysUser(); if (FacesContext.getCurrentInstance() != null) { currUser.getInfo().put( "request", ((IdentityRequestWrapper) FacesContext.getCurrentInstance() .getExternalContext().getRequest()).getRequest()); currUser.getInfo().put( "ip", ((IdentityRequestWrapper) FacesContext.getCurrentInstance() .getExternalContext().getRequest()).getRequest() .getRemoteAddr()); } Query query = dao .getSession() .createSQLQuery( "select * from SV_USERDEPART where account=:account and password=:password"); data.put("account", identity.getCredentials().getUsername()); MD5 m = new MD5(); data.put("password", m.getMD5ofStr(identity.getCredentials().getPassword())); QueryParameterImpl qp = new QueryParameterImpl(); query = qp.initParameter(query, data); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); List userDataList = (List) query.list();// 涉及到多部门,就会有多条记录 if (userDataList.size() > 0) { for (int i = 0; i < userDataList.size(); i++) { Map userData = (Map) userDataList.get(i); if (i == 0) { currUser.setAccount((String) data.get("USERNAME")); currUser.setId((String) userData.get("ID")); currUser.setMemo((String) userData.get("MEMO")); currUser.setEmpid((String) userData.get("EMPID")); currUser.setUsername((String) userData.get("USERNAME")); currUser.setSexname((String) userData.get("SEXNAME")); currUser.setAccount((String) userData.get("ACCOUNT")); currUser.getInfo().put("SKIN", userData.get("SKIN")); data.clear(); data.put("id", currUser.getId()); Query roleQuery = dao.getSession().createSQLQuery( "select * from SV_USERROLE where userid=:id"); roleQuery = qp.initParameter(roleQuery, data); roleQuery .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); List<Map<String, String>> roleList = roleQuery.list(); if (roleList != null) { for (int j = 0; j < roleList.size(); j++) { String roleid = roleList.get(j).get("ROLEID"); String text = roleList.get(j).get("TEXT"); identity.addRole(roleid); currUser.getRole().add(new Role(roleid, text)); } } } Ts_department depart = new Ts_department(); depart.setId((String) userData.get("DEPARTID")); depart.setText((String) userData.get("DEPARTNAME")); currUser.getDepart().add(depart); System.out.println(111); currUser.getInfo().put("aaa", "ddd"); } return currUser; } return null; } }前台代码格式如下:(showDetail属性可以配置是否查看详细信息“谁也不知道是什么问题这是详细描述信息”) <div class="errors"> <h:messages id="messages" showDetail="true" globalOnly="true" /> </div> |
|
|