Files
struts2-demo/web/WEB-INF/classes/com/demo/action/UserAction.java
2026-03-18 18:12:20 +08:00

85 lines
2.2 KiB
Java

package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class UserAction extends ActionSupport {
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private String username;
private String email;
private String phone;
private String submittedAt;
private String profileStage;
public String submit() {
username = normalize(username);
email = normalize(email);
phone = normalize(phone);
if (!isValid()) {
return INPUT;
}
submittedAt = LocalDateTime.now().format(TIME_FORMATTER);
profileStage = (phone != null && phone.length() >= 7) ? "Profile ready for follow-up demos." : "Profile captured. Add a stronger phone number next time.";
return SUCCESS;
}
private boolean isValid() {
boolean valid = true;
if (username == null || username.length() < 3) {
addFieldError("username", "Username must be at least 3 characters.");
valid = false;
}
if (email == null || !email.contains("@")) {
addFieldError("email", "Enter a valid email address.");
valid = false;
}
if (phone == null || phone.replaceAll("[^0-9]", "").length() < 7) {
addFieldError("phone", "Enter at least 7 digits for the phone number.");
valid = false;
}
return valid;
}
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSubmittedAt() {
return submittedAt;
}
public String getProfileStage() {
return profileStage;
}
}