diff --git a/src/main/java/com/example/struts2/ValidationLabAction.java b/src/main/java/com/example/struts2/ValidationLabAction.java new file mode 100644 index 0000000..92db68a --- /dev/null +++ b/src/main/java/com/example/struts2/ValidationLabAction.java @@ -0,0 +1,46 @@ +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; } +} diff --git a/src/main/resources/struts.xml b/src/main/resources/struts.xml index 101265e..d5ae109 100644 --- a/src/main/resources/struts.xml +++ b/src/main/resources/struts.xml @@ -72,6 +72,16 @@ /ognl-lab.jsp + + + /validation-lab.jsp + + + + submit + /validation-lab.jsp + /validation-lab.jsp + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index a3978af..f89c0dd 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -66,6 +66,11 @@ 可视化体验普通字段、嵌套对象、多选列表是如何被 Struts2 自动绑定到 Action 的。 进入实验室 + + ⚠️ 验证与错误流实验 + 通过错误示例/正确示例切换,直观看到字段错误、input 返回和成功路径的区别。 + 进入实验室 + diff --git a/src/main/webapp/learn.jsp b/src/main/webapp/learn.jsp index 878a788..5d299ba 100644 --- a/src/main/webapp/learn.jsp +++ b/src/main/webapp/learn.jsp @@ -67,6 +67,11 @@ 通过真正可交互的表单观察 Struts2 的字段绑定、嵌套对象绑定和集合绑定。 打开 OGNL 实验室 + + ⚠️ 验证与错误流实验 + 学习为什么校验失败不会直接 500,而是回到 input 页面并显示字段级错误。 + 打开验证实验室 + 📚 学习路径 diff --git a/src/main/webapp/validation-lab.jsp b/src/main/webapp/validation-lab.jsp new file mode 100644 index 0000000..086af1d --- /dev/null +++ b/src/main/webapp/validation-lab.jsp @@ -0,0 +1,86 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="s" uri="/struts-tags" %> + + + + + 验证与错误流实验室 - Struts2 + + + + ⚠️ 验证与错误流实验室 + + + 实验任务卡 + + 先点“填入错误示例”,提交后观察字段级错误是如何显示的 + 再点“填入正确示例”,观察 Action 成功后的返回 + 思考:为什么 Struts2 的校验失败会回到 input 页,而不是直接报 500? + + + + + + 填入错误示例 + 填入正确示例 + + + + + + + + + 用户名 + + + + + 邮箱 + + + + + 年龄 + + + + 提交实验 + + + + + 学习点 + + 字段错误通过 addFieldError() 收集 + JSP 使用 <s:fielderror> 精准展示错误 + 成功路径与失败路径在同一个页面闭环,对学习最直观 + + + + ← 返回学习中心 + + + +
可视化体验普通字段、嵌套对象、多选列表是如何被 Struts2 自动绑定到 Action 的。
通过错误示例/正确示例切换,直观看到字段错误、input 返回和成功路径的区别。
通过真正可交互的表单观察 Struts2 的字段绑定、嵌套对象绑定和集合绑定。
学习为什么校验失败不会直接 500,而是回到 input 页面并显示字段级错误。
addFieldError()
<s:fielderror>
← 返回学习中心