1. 程式人生 > >關於struts2中action獲取引數的三種方法

關於struts2中action獲取引數的三種方法

    public String add() throws Exception {
        // ------------------------------方法一:通過設定get與set方法來獲取引數
        System.out.println(this.username);
        // ------------------------------方法二:通過request物件來獲取引數
        /*
         * public class SupperAction extends ActionSupport implements

         * ServletRequestAware{

          *private HttpServletRequest request;

         * private  HttpSession session;

         * private Application application;

         * private  ActionContext context;這是SupportAction,所有的子類action extends該父類,得到所有物件
         */
        System.out.println(this.getRequest().getParameter("username"));
        // ------------------------------方法三:通過ActionContext物件來獲取引數
        // **import com.opensymphony.xwork2.ActionContext;
        String[] username = (String[]) this.getContext().getParameters().get("username");
        System.out.println(username[0] + username.length);
        return "success";

    }

有些博文寫到在Context.getParameters();後轉換成Map,本人表示不解,轉換成Map後再去獲取引數,這時候就沒有get(“引數名”)這樣一個方法了。個人愚見 :覺得轉換成map是多此一舉。