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; }
}

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 开发模式 - 修改配置后自动reload -->
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!-- 编码设置 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- ==================== 示例包定义 ==================== -->
<package name="default" namespace="/" extends="struts-default">
<!-- 首页 -->
<action name="index">
<result>/index.jsp</result>
</action>
<!-- Hello World 示例 -->
<action name="hello" class="com.demo.action.HelloAction" method="execute">
<result>/hello.jsp</result>
</action>
<!-- 用户登录示例 -->
<action name="login" class="com.demo.action.LoginAction" method="execute">
<result name="success">/user/success.jsp</result>
<result name="input">/user/login.jsp</result>
</action>
<!-- 用户表单提交 -->
<action name="submitUser" class="com.demo.action.UserAction" method="submit">
<result name="success">/user/success.jsp</result>
<result name="input">/user/form.jsp</result>
</action>
<!-- 文件上传示例 -->
<action name="upload" class="com.demo.action.FileUploadAction" method="execute">
<result name="success">/upload/success.jsp</result>
<result name="input">/upload/index.jsp</result>
</action>
<!-- AJAX 示例 -->
<action name="ajax" class="com.demo.action.AjaxAction" method="execute">
<result type="json"/>
</action>
<!-- 验证示例 -->
<action name="validate" class="com.demo.action.ValidationAction" method="execute">
<result name="success">/validation/success.jsp</result>
<result name="input">/validation/form.jsp</result>
</action>
</package>
<!-- REST 风格示例 -->
<package name="rest" namespace="/api" extends="struts-default">
<action name="users" class="com.demo.action.rest.UserRestAction">
<result type="json"/>
</action>
</package>
</struts>