fix(structs): resolve calc input result and add utf-8/meta plus form validation
This commit is contained in:
@@ -68,6 +68,22 @@ public class UserFormAction extends ActionSupport {
|
|||||||
return SUCCESS;
|
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
|
// Getters and Setters
|
||||||
public User getUser() { return user; }
|
public User getUser() { return user; }
|
||||||
public void setUser(User user) { this.user = user; }
|
public void setUser(User user) { this.user = user; }
|
||||||
|
|||||||
@@ -72,6 +72,7 @@
|
|||||||
|
|
||||||
<action name="calc" class="com.example.struts2.CalculatorAction">
|
<action name="calc" class="com.example.struts2.CalculatorAction">
|
||||||
<result>/calculator.jsp</result>
|
<result>/calculator.jsp</result>
|
||||||
|
<result name="input">/calculator.jsp</result>
|
||||||
</action>
|
</action>
|
||||||
|
|
||||||
<action name="calc_execute" class="com.example.struts2.CalculatorAction" method="calculate">
|
<action name="calc_execute" class="com.example.struts2.CalculatorAction" method="calculate">
|
||||||
@@ -85,6 +86,7 @@
|
|||||||
|
|
||||||
<action name="user_add" class="com.example.struts2.UserFormAction" method="add">
|
<action name="user_add" class="com.example.struts2.UserFormAction" method="add">
|
||||||
<result type="redirectAction">user</result>
|
<result type="redirectAction">user</result>
|
||||||
|
<result name="input">/user-form.jsp</result>
|
||||||
</action>
|
</action>
|
||||||
|
|
||||||
<action name="user_edit" class="com.example.struts2.UserFormAction" method="edit">
|
<action name="user_edit" class="com.example.struts2.UserFormAction" method="edit">
|
||||||
@@ -93,6 +95,7 @@
|
|||||||
|
|
||||||
<action name="user_update" class="com.example.struts2.UserFormAction" method="update">
|
<action name="user_update" class="com.example.struts2.UserFormAction" method="update">
|
||||||
<result type="redirectAction">user</result>
|
<result type="redirectAction">user</result>
|
||||||
|
<result name="input">/user-form.jsp</result>
|
||||||
</action>
|
</action>
|
||||||
|
|
||||||
<action name="user_delete" class="com.example.struts2.UserFormAction" method="delete">
|
<action name="user_delete" class="com.example.struts2.UserFormAction" method="delete">
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>计算器 - Struts2 表单示例</title>
|
<title>计算器 - Struts2 表单示例</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
|
body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
|
||||||
|
|||||||
21
src/main/webapp/error-invalid-input.jsp
Normal file
21
src/main/webapp/error-invalid-input.jsp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>参数无效</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; max-width: 600px; margin: 100px auto; padding: 20px; text-align: center; }
|
||||||
|
h1 { color: #e74c3c; }
|
||||||
|
.error-box { background: #fdeaea; border: 1px solid #e74c3c; padding: 30px; border-radius: 10px; }
|
||||||
|
.info { color: #7f8c8d; margin-top: 20px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="error-box">
|
||||||
|
<h1>🚫 检测到无效参数</h1>
|
||||||
|
<p>您的请求包含可疑内容,已被拦截。</p>
|
||||||
|
<p class="info">此页面由 ValidationInterceptor 拦截器生成</p>
|
||||||
|
</div>
|
||||||
|
<p><a href="learn">← 返回学习中心</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
21
src/main/webapp/error-rate-limit.jsp
Normal file
21
src/main/webapp/error-rate-limit.jsp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>请求过于频繁</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; max-width: 600px; margin: 100px auto; padding: 20px; text-align: center; }
|
||||||
|
h1 { color: #e74c3c; }
|
||||||
|
.error-box { background: #fdeaea; border: 1px solid #e74c3c; padding: 30px; border-radius: 10px; }
|
||||||
|
.info { color: #7f8c8d; margin-top: 20px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="error-box">
|
||||||
|
<h1>⚠️ 请求过于频繁</h1>
|
||||||
|
<p>您的请求次数超过限制,请稍后再试。</p>
|
||||||
|
<p class="info">此页面由 RateLimitInterceptor 拦截器生成</p>
|
||||||
|
</div>
|
||||||
|
<p><a href="learn">← 返回学习中心</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>Hello 示例 - Struts2</title>
|
<title>Hello 示例 - Struts2</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 900px; margin: 50px auto; padding: 20px; background:#f7f8fa; }
|
body { font-family: Arial, sans-serif; max-width: 900px; margin: 50px auto; padding: 20px; background:#f7f8fa; }
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>Struts2 Learning Scaffold</title>
|
<title>Struts2 Learning Scaffold</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 1100px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
body { font-family: Arial, sans-serif; max-width: 1100px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>拦截器演示 - Struts2</title>
|
<title>拦截器演示 - Struts2</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 900px; margin: 50px auto; padding: 20px; }
|
body { font-family: Arial, sans-serif; max-width: 900px; margin: 50px auto; padding: 20px; }
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>Struts2 学习中心</title>
|
<title>Struts2 学习中心</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 1000px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
body { font-family: Arial, sans-serif; max-width: 1000px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>用户表单 - Struts2</title>
|
<title>用户表单 - Struts2</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 700px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
body { font-family: Arial, sans-serif; max-width: 700px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
||||||
@@ -44,16 +45,19 @@
|
|||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>姓名:</label>
|
<label>姓名:</label>
|
||||||
<s:textfield name="user.name" placeholder="请输入姓名"/>
|
<s:textfield name="user.name" placeholder="请输入姓名"/>
|
||||||
|
<s:fielderror fieldName="user.name" cssStyle="color:#e74c3c;font-size:12px;"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>邮箱:</label>
|
<label>邮箱:</label>
|
||||||
<s:textfield name="user.email" placeholder="请输入邮箱"/>
|
<s:textfield name="user.email" placeholder="请输入邮箱"/>
|
||||||
|
<s:fielderror fieldName="user.email" cssStyle="color:#e74c3c;font-size:12px;"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>年龄:</label>
|
<label>年龄:</label>
|
||||||
<s:textfield name="user.age" placeholder="请输入年龄"/>
|
<s:textfield name="user.age" placeholder="请输入年龄"/>
|
||||||
|
<s:fielderror fieldName="user.age" cssStyle="color:#e74c3c;font-size:12px;"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit">保存</button>
|
<button type="submit">保存</button>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
<title>用户管理 - Struts2 CRUD 示例</title>
|
<title>用户管理 - Struts2 CRUD 示例</title>
|
||||||
<style>
|
<style>
|
||||||
body { font-family: Arial, sans-serif; max-width: 1000px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
body { font-family: Arial, sans-serif; max-width: 1000px; margin: 40px auto; padding: 20px; background:#f7f8fa; }
|
||||||
|
|||||||
Reference in New Issue
Block a user