Click Framework最佳实践-包和类

本文介绍了一种按技术分类的优秀工程包结构设计方法,适用于Click等Web应用。通过将页面、实体和服务分别组织在不同包中,实现清晰的模块化结构。此外,还详细解释了如何创建通用的基类Page和BorderPage来提高代码复用率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水平有限

 

一个优秀的设计你的工程包结构的方式是按应用技术(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上。



 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值