📄 loginaction.java
字号:
package com.pegasus.framework.acl.action;
import java.util.Enumeration;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.pegasus.framework.acl.exception.AuthorizationException;
import com.pegasus.framework.acl.form.LoginForm;
import com.pegasus.framework.acl.pojo.UserSession;
import com.pegasus.framework.acl.pojo.WebUser;
import com.pegasus.framework.acl.service.BeanNames;
import com.pegasus.framework.acl.service.ILoginService;
import com.pegasus.framework.acl.service.IUserService;
import com.pegasus.framework.action.BaseAction;
import com.pegasus.framework.config.AppConfig;
import com.pegasus.framework.exception.WebException;
import com.pegasus.framework.service.ServiceManager;
import com.pegasus.framework.util.StringUtil;
public class LoginAction extends BaseAction {
private Log logger = LogFactory.getLog(LoginAction.class);
private IUserService userService;
private ILoginService loginService;
/**
*
*/
@Override
public void initService() {
userService = (IUserService)ServiceManager.getService(BeanNames.BEAN_NAME_USER);
loginService = (ILoginService)ServiceManager.getService(BeanNames.BEAN_NAME_LOGIN);
}
/**
* @param mapping .
* @param form .
* @param request .
* @param response .
* @return .
* @throws Exception .
*/
public ActionForward doExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws WebException {
logger.info("execute");
request.getSession().removeAttribute(UserSession.USERSESSION_KEY);
try {
return doLogin(form, request, mapping);
}
catch (AuthorizationException e) {
super.addError(request,"error.login.failed", e.getMessage());
return mapping.getInputForward();
}
}
/**
* @param form .
* @param request .
* @param mapping .
* @return .
* @throws Exception .
*/
private ActionForward doLogin(ActionForm form, HttpServletRequest request, ActionMapping mapping)
throws WebException {
LoginForm aform = (LoginForm) form;
String loginname = aform.getLoginname();
if (StringUtil.isEmpty(loginname)) {
String msg = getResources(request, "global").getMessage("login.jsp.loginname");
throw new AuthorizationException(getResources(request, "global").getMessage("errors.required", msg));
}
String password = aform.getPassword();
WebUser webUser = loginService.loadUser(loginname, password);
String msg = getResources(request, "global").getMessage("login.jsp");
if (webUser == null) {
throw new AuthorizationException(getResources(request, "global").getMessage("error.login.password"));
}
logger.info("login success!");
Locale locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);
UserSession userSession = new UserSession(webUser);
userSession.setLoginDate(new java.util.Date());
userSession.setLastVisitDate(new java.util.Date());
userSession.setLastActName(mapping.getPath().substring(1));
if (locale.getLanguage().equals("en")) {
userSession.setLocale(false);
}
else {
userSession.setLocale(true);
}
request.getSession().setAttribute(UserSession.USERSESSION_KEY, userSession);
return new ActionForward(AppConfig.getHomePage(), true);
}
/**
* @return .
*/
public ILoginService getLoginService() {
return loginService;
}
/**
* @param loginService .
*/
public void setLoginService(ILoginService loginService) {
this.loginService = loginService;
}
/**
* @return .
*/
public IUserService getUserService() {
return userService;
}
/**
* @param userService .
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -