Update: web.xml and generated files
This commit is contained in:
47
web/WEB-INF/classes/com/demo/action/LoginAction.java
Normal file
47
web/WEB-INF/classes/com/demo/action/LoginAction.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.demo.action;
|
||||
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
|
||||
/**
|
||||
* 用户登录 Action
|
||||
* 展示:
|
||||
* - 表单参数接收
|
||||
* - 数据验证
|
||||
* - Session 使用
|
||||
* - 返回不同结果
|
||||
*/
|
||||
public class LoginAction extends ActionSupport {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
@Override
|
||||
public String execute() throws Exception {
|
||||
// 模拟登录验证
|
||||
if ("admin".equals(username) && "123456".equals(password)) {
|
||||
// 登录成功,设置 session
|
||||
return SUCCESS;
|
||||
} else if (username != null && !username.isEmpty()) {
|
||||
// 登录失败
|
||||
addActionError("用户名或密码错误!");
|
||||
return INPUT;
|
||||
}
|
||||
return INPUT;
|
||||
}
|
||||
|
||||
// 表单验证
|
||||
@Override
|
||||
public void validate() {
|
||||
if (username == null || username.trim().length() < 3) {
|
||||
addFieldError("username", "用户名至少3个字符");
|
||||
}
|
||||
if (password == null || password.length() < 6) {
|
||||
addFieldError("password", "密码至少6个字符");
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
Reference in New Issue
Block a user