You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Struts2 Action类中不使用OGNL从请求获取POJO对象的方法

嘿,这个需求我之前做项目时刚好碰到过,给你两个实用的方案,完全满足你不想用OGNL的get/set、也不想逐个取参数的要求:

方案一:借助Apache Commons BeanUtils自动映射

这个方案的核心思路是:先拿到HttpServletRequest对象,提取所有参数后处理掉前缀user.,再用BeanUtils的工具方法自动把参数值填充到User对象里,不用手动写一百个set方法。

步骤和代码示例:

  1. 让你的Action实现ServletRequestAware接口,拿到HttpServletRequest实例:
public class UserAction extends ActionSupport implements ServletRequestAware {
    private HttpServletRequest request;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String execute() throws Exception {
        // 1. 创建空的User对象
        User user = new User();
        
        // 2. 处理请求参数:去掉所有参数键的"user."前缀
        Map<String, String[]> rawParamMap = request.getParameterMap();
        Map<String, String[]> processedParams = new HashMap<>();
        for (Map.Entry<String, String[]> entry : rawParamMap.entrySet()) {
            String paramKey = entry.getKey();
            if (paramKey.startsWith("user.")) {
                // 截取掉"user.",比如"user.userid"变成"userid"
                String newKey = paramKey.substring(5);
                processedParams.put(newKey, entry.getValue());
            }
        }
        
        // 3. 用BeanUtils自动填充User对象
        org.apache.commons.beanutils.BeanUtils.populate(user, processedParams);
        
        // 现在user对象已经包含所有请求参数的值了
        System.out.println(user.getUserid());
        System.out.println(user.getUsername());
        
        return SUCCESS;
    }
}
  1. 别忘了引入Apache Commons BeanUtils的依赖(如果用Maven的话):
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.4</version>
</dependency>

方案二:用Spring BeanWrapper(项目已引入Spring时推荐)

如果你的项目已经在用Spring框架,用BeanWrapper会更灵活,它的类型转换能力比BeanUtils更强,还能忽略未知属性避免报错。

代码示例:

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

// 同样先实现ServletRequestAware拿到request,然后在execute方法里:
public String execute() throws Exception {
    User user = new User();
    BeanWrapper beanWrapper = new BeanWrapperImpl(user);
    
    // 处理参数前缀(和方案一一样的逻辑)
    Map<String, String[]> rawParamMap = request.getParameterMap();
    Map<String, String[]> processedParams = new HashMap<>();
    for (Map.Entry<String, String[]> entry : rawParamMap.entrySet()) {
        String paramKey = entry.getKey();
        if (paramKey.startsWith("user.")) {
            processedParams.put(paramKey.substring(5), entry.getValue());
        }
    }
    
    // 填充属性,true表示忽略User类里没有的属性(避免参数多了报错)
    beanWrapper.setPropertyValues(processedParams, true);
    
    // 使用user对象即可
    return SUCCESS;
}

注意事项:

  • 两个方案都完全绕开了Struts2的OGNL自动注入逻辑,直接从HttpServletRequest获取参数,符合你的要求。
  • 如果参数里有复杂类型(比如日期、自定义对象),需要给BeanUtils或者BeanWrapper配置对应的类型转换器,不过基本类型(String、int、boolean等)会自动转换。

内容的提问来源于stack exchange,提问作者Mahendra Rathod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:04:50