From ebe4f0467c8a175c0cc7acd38f6d290e9d6fd498 Mon Sep 17 00:00:00 2001 From: likingcode Date: Tue, 10 Mar 2026 00:23:36 +0800 Subject: [PATCH] fix(structs): resolve calc input result and add utf-8/meta plus form validation --- .../com/example/struts2/UserFormAction.java | 16 ++++++++++++++ src/main/resources/struts.xml | 3 +++ src/main/webapp/calculator.jsp | 1 + src/main/webapp/error-invalid-input.jsp | 21 +++++++++++++++++++ src/main/webapp/error-rate-limit.jsp | 21 +++++++++++++++++++ src/main/webapp/hello.jsp | 1 + src/main/webapp/index.jsp | 1 + src/main/webapp/interceptor-demo.jsp | 1 + src/main/webapp/learn.jsp | 1 + src/main/webapp/user-form.jsp | 4 ++++ src/main/webapp/user-list.jsp | 1 + 11 files changed, 71 insertions(+) create mode 100644 src/main/webapp/error-invalid-input.jsp create mode 100644 src/main/webapp/error-rate-limit.jsp diff --git a/src/main/java/com/example/struts2/UserFormAction.java b/src/main/java/com/example/struts2/UserFormAction.java index c17361c..6217e78 100644 --- a/src/main/java/com/example/struts2/UserFormAction.java +++ b/src/main/java/com/example/struts2/UserFormAction.java @@ -68,6 +68,22 @@ public class UserFormAction extends ActionSupport { return SUCCESS; } + @Override + public void validate() { + if (user == null) { + return; + } + if (user.getName() == null || user.getName().trim().isEmpty()) { + addFieldError("user.name", "姓名不能为空"); + } + if (user.getEmail() == null || !user.getEmail().contains("@")) { + addFieldError("user.email", "邮箱格式不正确"); + } + if (user.getAge() == null || user.getAge() < 1 || user.getAge() > 120) { + addFieldError("user.age", "年龄必须在 1 到 120 之间"); + } + } + // Getters and Setters public User getUser() { return user; } public void setUser(User user) { this.user = user; } diff --git a/src/main/resources/struts.xml b/src/main/resources/struts.xml index 3643269..960e1ff 100644 --- a/src/main/resources/struts.xml +++ b/src/main/resources/struts.xml @@ -72,6 +72,7 @@ /calculator.jsp + /calculator.jsp @@ -85,6 +86,7 @@ user + /user-form.jsp @@ -93,6 +95,7 @@ user + /user-form.jsp diff --git a/src/main/webapp/calculator.jsp b/src/main/webapp/calculator.jsp index de5dab5..45cca10 100644 --- a/src/main/webapp/calculator.jsp +++ b/src/main/webapp/calculator.jsp @@ -3,6 +3,7 @@ + 计算器 - Struts2 表单示例 + + +
+

🚫 检测到无效参数

+

您的请求包含可疑内容,已被拦截。

+

此页面由 ValidationInterceptor 拦截器生成

+
+

← 返回学习中心

+ + \ No newline at end of file diff --git a/src/main/webapp/error-rate-limit.jsp b/src/main/webapp/error-rate-limit.jsp new file mode 100644 index 0000000..7d81b71 --- /dev/null +++ b/src/main/webapp/error-rate-limit.jsp @@ -0,0 +1,21 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + 请求过于频繁 + + + +
+

⚠️ 请求过于频繁

+

您的请求次数超过限制,请稍后再试。

+

此页面由 RateLimitInterceptor 拦截器生成

+
+

← 返回学习中心

+ + \ No newline at end of file diff --git a/src/main/webapp/hello.jsp b/src/main/webapp/hello.jsp index f198665..7ede201 100644 --- a/src/main/webapp/hello.jsp +++ b/src/main/webapp/hello.jsp @@ -2,6 +2,7 @@ + Hello 示例 - Struts2