loginaction.java
来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 65 行
JAVA
65 行
package com.vegeta.action;
import org.apache.struts2.interceptor.SessionAware;
import com.vegeta.model.user.User;
import com.vegeta.service.user.IUserService;
import com.vegeta.utils.Constants;
public class LoginAction extends BaseAction implements SessionAware {
private static final long serialVersionUID = -3060088004494579659L;
private IUserService userService;
private String username;
private String password;
public LoginAction(IUserService userService) {
this.userService = userService;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@SuppressWarnings("unchecked")
public String execute() {
User user = userService.findByUserName(username);
if (user != null && null != username && !"".equals(username) && password.equals(user.getPassword())) {
System.out.println("return SUCCESS");
session.put(Constants.USER, user);
return SUCCESS;
} else {
System.out.println("return ERROR");
addActionError(getText("authenticate.failed"));
return ERROR;
}
}
public void validate() {
/* Check that fields are not empty */
if (getPassword().length() == 0) {
addFieldError("password", getText("validate.password"));
}
if (getUsername().length() == 0) {
addFieldError("username", getText("validate.username"));
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?