81,094
社区成员
发帖
与我相关
我的任务
分享
@Namespace("/Login")
@ParentPackage("struts-default")
@Results({
@Result(name = "success",location = "/index.jsp"),
@Result(name = "error",location = "/login/fail.jsp")
})
@Controller
public class LoginAction extends BaseAction implements Serializable, ModelDriven<Admin> {
private static final long serialVersionUID = 1351937910832424955L;
private Admin admin = new Admin();
public Admin getModel() {
return admin;
}
private LoginService loginService;
@Autowired
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
@Action(value = "login")
@Validations(requiredStrings = {
@RequiredStringValidator(type = ValidatorType.SIMPLE, trim = true, fieldName = "admin.username", message = "请输入用户名.*"),
@RequiredStringValidator(type = ValidatorType.SIMPLE, trim = true, fieldName = "admin.password", message = "请输入密码.*")
}
)
public String login(){
httpServletRequest = ServletActionContext.getRequest();
admin = loginService.findAdminByNameAndPassword(admin.getUsername(), admin.getPassword());
if (admin != null) {
httpServletRequest.setAttribute("username", admin.getUsername());
return SUCCESS;
} else {
addFieldError("", "用户名密码错误");
return ERROR;
}
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title></title>
</head>
<body>
<s:form action="login" namespace="/Login" method="post">
<s:textfield label="用户名" name="username"/>
<s:password label="密码" name="password"/>
<s:submit label="登录"/>
</s:form>
</body>
</html>