ITeye博客

Click Framework最佳实践-包和类

bill.end · 2009-03-16 10:03 · Click · Apache · Ajax · Spring · log4j · Bean · Markdown

水平有限

 

一个优秀的设计你的工程包结构的方式是按应用技术(technology)分类。所以在一个Click应用中,所有页面放在page包下。这样做也能和Page自动映射特性很好的搭配使用。

 

所有工程的域实体(domain entity)类会被包含在entity包中,并且service类在service目录中。注意entity包的名字可替换为包含domain或者model。通常会有一个util包包含那些不适合在其他包中的类。

 

在java中通常包的名字是单数的,所以是util不是utils。

 

下图说明MyCorp web应用例子的工程结构:

 

在这个例子中,使用声明的角色和基于安全的路径。在admin包和目录的page需要有admin角色才能被访问,而在user包和目录的page需要有user角色才能被访问。

 

 

Page Classes

在编写Page类时,最好的方法是把共通的方法放在Page的基类中。通常是提供访问应用的services和logger对象的方法。
例如,BasePage提供访问Spring配置的service和Log4J日志对象:

public class BasePage extends Page implements ApplicationContextAware {
	
    /** The Spring application context. */
    protected ApplicationContext applicationContext;
    
    /** The page Logger instance. */
    protected Logger logger;
	
    /**
     * Return the Spring configured Customer service.
     *
     * @return the Spring configured Customer service
     */
    public CustomerService getCustomerService() {
        return (CustomerService) getBean("customerService");
    }
	
    /**
     * Return the Spring configured User service.
     *
     * @return the Spring configured User service
     */
    public UserService getUserService() {
        return (UserService) getBean("userService");
    }
	
    /**
     * Return the page Logger instance.
     *
     * @return the page Logger instance
     */
    public Logger getLogger() {
        if (logger == null) {
            logger = Logger.getLogger(getClass());
        }
        return logger;
    }

    /**
     * @see ApplicationContextAware#setApplicationContext(ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext applicationContext)  {
        this.applicationContext = applicationContext;
    }

    /**
     * Return the configured Spring Bean for the given name.
     *
     * @param beanName the configured name of the Java Bean
     * @return the configured Spring Bean for the given name
     */
    public Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

} 

 

通常有一个border模板和一个继承BasePage并定义这个模板的BorderPage:

public class BorderPage extends BasePage {

    /** The root Menu item. */
    public Menu rootMenu = new Menu();
	
    /**
     * @see Page#getTemplate()
     */
    public String getTemplate() {
        return "/border-template.htm";
    }
} 

 

应用中的大部分page都是BorderPage的子类,除了AJAX page,它不需要HTML border模板通常继承BasePage。BorderPage不要包含共通逻辑,只需要呈现border模板。共通的page逻辑应该被定义在BasePage中。

 

为了防止Page基类自动映射被访问,要确保没有page模板能和它们的类名匹配。例如,BorderPage不会被自动映射到border-template.htm上。

评论(2)

  1. #2 楼 · bill.end · 2009-03-16 15:31

    mazzystar writes:

    where is your point

    需要么?这是我翻译的。 http://incubator.apache.org/click/docs/best-practices.html#packages-classes

  2. #1 楼 · mazzystar · 2009-03-16 15:18

    where is your point

评论功能维护中,当前仅展示历史评论。