95 lines
2.5 KiB
Java
95 lines
2.5 KiB
Java
package com.demo.action;
|
|
|
|
import com.opensymphony.xwork2.ActionSupport;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
public class ValidationAction extends ActionSupport {
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
private String username;
|
|
private String email;
|
|
private Integer age;
|
|
private String bio;
|
|
private String scoreBand;
|
|
private String submittedAt;
|
|
private boolean seniorTrack;
|
|
|
|
@Override
|
|
public String execute() {
|
|
username = normalize(username);
|
|
email = normalize(email);
|
|
bio = normalize(bio);
|
|
seniorTrack = age >= 30;
|
|
scoreBand = seniorTrack ? "mid" : "early";
|
|
submittedAt = LocalDateTime.now().format(TIME_FORMATTER);
|
|
return SUCCESS;
|
|
}
|
|
|
|
@Override
|
|
public void validate() {
|
|
if (username == null || username.trim().length() < 3 || username.trim().length() > 20) {
|
|
addFieldError("username", "用户名长度需在 3 到 20 个字符之间。/ Username must be between 3 and 20 characters.");
|
|
}
|
|
if (email == null || !email.contains("@") || email.indexOf('@') == email.length() - 1) {
|
|
addFieldError("email", "请输入有效邮箱。/ Enter a valid email address.");
|
|
}
|
|
if (age == null || age < 18 || age > 60) {
|
|
addFieldError("age", "年龄需在 18 到 60 岁之间。/ Age must be between 18 and 60.");
|
|
}
|
|
if (bio != null && bio.trim().length() > 240) {
|
|
addFieldError("bio", "简介不能超过 240 个字符。/ Bio must stay under 240 characters.");
|
|
}
|
|
}
|
|
|
|
private String normalize(String value) {
|
|
return value == null ? null : value.trim();
|
|
}
|
|
|
|
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 getBio() {
|
|
return bio;
|
|
}
|
|
|
|
public void setBio(String bio) {
|
|
this.bio = bio;
|
|
}
|
|
|
|
public String getScoreBand() {
|
|
return scoreBand;
|
|
}
|
|
|
|
public String getSubmittedAt() {
|
|
return submittedAt;
|
|
}
|
|
|
|
public boolean isSeniorTrack() {
|
|
return seniorTrack;
|
|
}
|
|
}
|