package com.example.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class ValidationLabAction extends ActionSupport {
    private String username;
    private String email;
    private Integer age;
    private String resultMessage;

    @Override
    public String execute() {
        return SUCCESS;
    }

    public String submit() {
        resultMessage = String.format("提交成功：username=%s, email=%s, age=%s", username, email, age);
        return SUCCESS;
    }

    @Override
    public void validate() {
        if ("submit".equals(getActionName())) {
            if (username == null || username.trim().length() < 2) {
                addFieldError("username", "用户名至少 2 个字符");
            }
            if (email == null || !email.contains("@")) {
                addFieldError("email", "邮箱格式不正确");
            }
            if (age == null || age < 1 || age > 120) {
                addFieldError("age", "年龄必须在 1 到 120 之间");
            }
        }
    }

    private String actionName;
    public String getActionName() { return actionName; }
    public void setActionName(String actionName) { this.actionName = actionName; }
    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
    public Integer getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }
    public String getResultMessage() { return resultMessage; }
}
