package com.example.struts2;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import java.util.HashMap;
import java.util.Map;

/**
 * 学习示例 Action
 * 
 * 学习点：
 * - 获取原始 HttpServletRequest
 * - 多种结果类型
 * - Action 配置
 */
public class LearnAction extends ActionSupport implements ServletRequestAware {
    
    private HttpServletRequest request;
    private Map<String, Object> data;
    
    // 默认方法 - 显示学习菜单
    public String execute() {
        data = new HashMap<>();
        data.put("message", "欢迎学习 Struts2！");
        data.put("features", new String[]{
            "MVC 架构模式",
            "拦截器机制",
            "OGNL 表达式语言",
            "标签库",
            "类型转换",
            "输入验证"
        });
        data.put("actions", new String[]{
            "hello - 基础示例",
            "calculator - 表单处理",
            "user - CRUD 操作",
            "learn/info - 本页面"
        });
        return SUCCESS;
    }
    
    // 显示请求信息
    public String info() {
        data = new HashMap<>();
        data.put("method", request.getMethod());
        data.put("contextPath", request.getContextPath());
        data.put("remoteAddr", request.getRemoteAddr());
        data.put("header_UserAgent", request.getHeader("User-Agent"));
        return SUCCESS;
    }
    
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    
    public Map<String, Object> getData() { return data; }
    public void setData(Map<String, Object> data) { this.data = data; }
}