---
title: "debug标签和getXxx方法"
author: "chengpan"
date: "2011-03-19T01:49:05Z"
tags: []
categories: ["企业架构"]
slug: "debug-getxxx"
draft: false
lastmod: "2011-03-19T01:49:05Z"
id: 967218
url: "https://chengpan.iteye.com/blog/967218"
---

最近在用struts2的 debug标签时发现了一些问题，比如我们要在action中依ID来要查找一个实体类，方法名一般我们会定义getById但是这个方法的执行需要有一个id，可是当我们没有为id赋值的时候，我们使用了debug 标签时，struts就会自动执行getXxx方法，问题就出现了，没有id，就会抛出异常。我看了一下源码，发现struts只所以执行所有的getXxx方法是因为，他要把所有值放入值栈中，我试了一下，get方法，get+数字的方法不会被执行。所以在对action定义方法时候应该注意方法的命名。

例如：
public XxxAction extends ActionSupport{
     private id;
     private UserService userService;

     public String getById(){
         userService.getById(id);    
    
    }
}
如果没有给id赋值，则抛出异常，当debug标签调用该方法时，new 出的action 中id是没有值的
则出现了错误。