📄 loginaction.java
字号:
package com.wiely.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;
import com.wiely.service.IUserService;
import com.wiely.struts.form.LoginForm;
import com.wiely.struts.form.RegisterForm;
import com.wiely.vo.User;
public class LoginAction extends DispatchAction {
/*
* use spring to inject the userService and reseacherService
*/
protected IUserService userService;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
/*
* return ActionForward failure or success,then login the users
*/
@SuppressWarnings( { "deprecation", "unchecked" })
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// DynaActionForm daf = (DynaActionForm) form;
// String username = (String) daf.get("username");
// String password = (String) daf.get("password");
// String repassword = (String) daf.get("repassword");
LoginForm lf = (LoginForm) form;
String username = lf.getUsername();
String password = lf.getPassword();
User user = new User();
user.setPassword(password);
user.setUsername(username);
User user1 = userService.login(user);
if (user1 != null) {
HttpSession session = request.getSession(true);
session.setAttribute("user", username);
return mapping.findForward("success");
} else {
return mapping.findForward("failure");
}
}
/*
* register the new user and validate the same
*/
@SuppressWarnings("deprecation")
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
RegisterForm lf = (RegisterForm) form;
String name = lf.getName();
String rpassword = lf.getRpassword();
User user = new User();
user.setUsername(name);
user.setPassword(rpassword);
int i = userService.findUser(user);
if (i == 0) {
userService.register(user);
return mapping.findForward("register");
} else {
ActionErrors errors = new ActionErrors();
errors.add("sameuser", new ActionMessage("errors.sameuser"));
saveErrors(request, errors);
return mapping.findForward("sameuser");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -