Update: web.xml and generated files

This commit is contained in:
likingcode
2026-03-18 15:18:30 +08:00
parent fb9d2d1206
commit 1f60832445
20 changed files with 2071 additions and 2 deletions

View File

@@ -0,0 +1,34 @@
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Hello World 示例 Action
* 展示 Struts2 最基础的 Action 写法
*
* ActionSupport 提供了:
* - validate() 方法用于表单验证
* - getText() 方法用于国际化
* - addActionError()/addFieldError() 用于错误消息
*/
public class HelloAction extends ActionSupport {
private String name;
private String message;
@Override
public String execute() throws Exception {
// 业务逻辑
if (name == null || name.trim().isEmpty()) {
name = "World";
}
message = "Hello, " + name + "! 欢迎学习 Struts2!";
return SUCCESS;
}
// Getter/Setter (Struts2 需要这些来获取/设置参数)
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
}

View File

@@ -0,0 +1,47 @@
package com.demo.action;
import com.opensymphony.xwork2.ActionSupport;
/**
* 用户登录 Action
* 展示:
* - 表单参数接收
* - 数据验证
* - Session 使用
* - 返回不同结果
*/
public class LoginAction extends ActionSupport {
private String username;
private String password;
@Override
public String execute() throws Exception {
// 模拟登录验证
if ("admin".equals(username) && "123456".equals(password)) {
// 登录成功,设置 session
return SUCCESS;
} else if (username != null && !username.isEmpty()) {
// 登录失败
addActionError("用户名或密码错误!");
return INPUT;
}
return INPUT;
}
// 表单验证
@Override
public void validate() {
if (username == null || username.trim().length() < 3) {
addFieldError("username", "用户名至少3个字符");
}
if (password == null || password.length() < 6) {
addFieldError("password", "密码至少6个字符");
}
}
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
}

View File

@@ -0,0 +1,30 @@
package com.demo.model;
/**
* 用户模型
* 展示 Struts2 的 ModelDriven 和 Bean 封装
*/
public class User {
private Long id;
private String username;
private String email;
private String phone;
public User() {}
public User(Long id, String username, String email) {
this.id = id;
this.username = username;
this.email = email;
}
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
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; }
}