论坛首页 Java企业应用论坛

webwork中为Action的变量设置初值的问题

浏览 4053 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-06-24  
在webwork的Action中,我想为其中的私有变量设置初值。
/**
 * @author Joa
 */
public class ListCity implements Action {
    
    private static final Log logger = LogFactory.getLog(ListCity.class);;
    
    private CityService cityService;

    private Page page;

    private List cities;
    
    private int number;

    /*
     * (non-Javadoc);
     * 
     * @see com.opensymphony.xwork.Action#execute();
     */
    public String execute(); throws Exception {
        
        logger.info("number:" + number);;
        
        logger.info("page # everyPage:" + page.getEveryPage(););;
        logger.info("page # currentPage:" + page.getCurrentPage(););;
        
        Result result = cityService.listCity(page);;
        
        page = result.getPage();;
        cities = result.getContent();;
        
        return SUCCESS;
    }

    /**
     * @return Returns the cities.
     */
    public List getCities(); {
        return cities;
    }

    /**
     * @return Returns the page.
     */
    public Page getPage(); {
        return page;
    }

    /**
     * @param cities
     *            The cities to set.
     */
    public void setCities(List cities); {
        this.cities = cities;
    }

    /**
     * @param page
     *            The page to set.
     */
    public void setPage(Page page); {
        this.page = page;
    }

    /**
     * @param cityService
     *            The cityService to set.
     */
    public void setCityService(CityService cityService); {
        this.cityService = cityService;
    }
    /**
     * @return Returns the number.
     */
    public int getNumber(); {
        return number;
    }
    /**
     * @param number The number to set.
     */
    public void setNumber(int number); {
        this.number = number;
    }
}


其中的number为基本类型int,page为一个对象:
public class Page {
    
    /** imply if the page has previous page */
    private boolean hasPrePage;
    
    /** imply if the page has next page */
    private boolean hasNextPage;
        
    /** the number of every page */
    private int everyPage;
    
    /** the total page number */
    private int totalPage;
       
    /** the number of current page */
    private int currentPage;
    
    /** the begin index of the records by the current query */
    private int beginIndex;
    
    
    /** The default constructor */
    public Page();{
        
    }
    
    /** construct the page by everyPage 
     * @param everyPage
     * */
    public Page(int everyPage);{
        this.everyPage = everyPage;
    }
    
    /** The whole constructor */
    public Page(boolean hasPrePage, boolean hasNextPage,  
            	 int everyPage, int totalPage, 
            	 int currentPage, int beginIndex); {
        this.hasPrePage = hasPrePage;
        this.hasNextPage = hasNextPage;
        this.everyPage = everyPage;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.beginIndex = beginIndex;
    }

    /**
     * @return 
     * Returns the beginIndex.
     */
    public int getBeginIndex(); {
        return beginIndex;
    }
    
    /**
     * @param beginIndex 
     * The beginIndex to set.
     */
    public void setBeginIndex(int beginIndex); {
        this.beginIndex = beginIndex;
    }
    
    /**
     * @return 
     * Returns the currentPage.
     */
    public int getCurrentPage(); {
        return currentPage;
    }
    
    /**
     * @param currentPage 
     * The currentPage to set.
     */
    public void setCurrentPage(int currentPage); {
        this.currentPage = currentPage;
    }
    
    /**
     * @return 
     * Returns the everyPage.
     */
    public int getEveryPage(); {
        return everyPage;
    }
    
    /**
     * @param everyPage 
     * The everyPage to set.
     */
    public void setEveryPage(int everyPage); {
        this.everyPage = everyPage;
    }
    
    /**
     * @return 
     * Returns the hasNextPage.
     */
    public boolean getHasNextPage(); {
        return hasNextPage;
    }
    
    /**
     * @param hasNextPage 
     * The hasNextPage to set.
     */
    public void setHasNextPage(boolean hasNextPage); {
        this.hasNextPage = hasNextPage;
    }
    
    /**
     * @return 
     * Returns the hasPrePage.
     */
    public boolean getHasPrePage(); {
        return hasPrePage;
    }
    
    /**
     * @param hasPrePage 
     * The hasPrePage to set.
     */
    public void setHasPrePage(boolean hasPrePage); {
        this.hasPrePage = hasPrePage;
    }
    
    /**
     * @return Returns the totalPage.
     * 
     */
    public int getTotalPage(); {
        return totalPage;
    }
    
    /**
     * @param totalPage 
     * The totalPage to set.
     */
    public void setTotalPage(int totalPage); {
        this.totalPage = totalPage;
    }
    
}


我希望在配置文件里面为page对象中的everyPage设置初值为10:
<action name="listCity" class="com.ebt.action.city.ListCity" >
			<param name="number">10</param>
			<param name="page.everyPage">10</param>
			<result name="success">/city/city_list.jsp</result>
			<result name="exception">/no_object.jsp</result>
		</action>


结果是:number可以正确被打印出来结果:10,但是page对象却无法被创建,抛出NullPointerException。

不知道像这样的对象设置初值,webwork如何进行配置?
   发表时间:2005-06-24  
who init page?

Page page=new Page();
0 请登录后投票
   发表时间:2005-06-24  
private Page page =new Page();

或者

public Page getPage() {
       if(null == page)
       {
          page= new Page();
       }
        return page;
    }
0 请登录后投票
   发表时间:2005-06-24  
楼上两位可能误解我的意思了。

我完全可以通过:
private Page page = new Page(10);
来解决我的问题。

只是我觉得将10这样的数字直接写在程序里面我觉得不爽,要是能在配置文件里面指定多好啊。
0 请登录后投票
   发表时间:2005-06-24  
你为什么不按照我们写的试试哪...

webwork不会自动创建page如果你没创建的话,你getPage得到null,然后 null.setPage 当然报错了...



这世界到底谁误解谁哪?
0 请登录后投票
   发表时间:2005-06-24  
问题已经解决。谢谢楼上的答复,我一开始没有明白你的意思。不过我就是不喜欢在Action的变量里面写new,所以希望完全通过注入来解决问题。

原本我的配置文件都正确,只是我忘记在Spring的applicationContext里面定义page了,所以page本身没有被初始化,那么webwork就无法设置值。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics