Controller 及 Action 的命名

Posted by Bruce Tsai

原則

  • 清楚描述功能內容
  • 名稱儘可能簡短
  • 不要加上非功能意義的名稱

狀況:含有不必要內容的名稱

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespaces({
    // 'controller' 在 url 中不需呈現
    @Namespace("/billInfoController"),
    // 'controller' 在 url 中不需呈現
    @Namespace("/portal/billInfoController")
})
@Results({ @Result(
    name = "detail.info",
    location = "/pages/bill/info/detail.jsp") })
public class BillInfoController implements IBillInfoController {

    @Override
    @Action("getDetailInfo");  // 'get' 在 url 中不需呈現
    public String getDetailInfo() {
        // code here
    }

}
  • 實際顯示網址
http://www.somewhere.com/billInfoController/getDetailInfo.action

正確做法

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespaces({
    @Namespace("/billInfo"),
    @Namespace("/portal/billInfo")
})
@Results({ @Result(
    name = "detail.info",
    location = "/pages/bill/info/detail.jsp") })
public class BillInfoController implements IBillInfoController {

    @Override
    @Action("detailInfo");
    public String getDetailInfo() {
        // code here
    }

}
  • 實際顯示網址
http://www.somewhere.com/billInfo/detailInfo.action

狀況:難以理解的命名

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace(value = "/fourGUsedbytesSearchController")
@Results({ @Result(
    name = "usedBytesQueryPage",
    location = "/pages/esp/fourg/four-g-usedbytes-search.jsp") })
public class FourGUsedbytesSearchController
    implements IFourGUsedbytesSearchController {

    @Override
    @Action("usedBytesQueryPage")
    public String usedBytesQuery() throws FailedResultException {
        // code here
    }

}
  • 實際顯示網址
http://www.somewhere.com/fourGUsedbytesSearchController/
usedBytesQueryPage.action

建議做法

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace(value = "/4g")
@Results({ @Result(
    name = "instant.usage",
    location = "/pages/4glte/instance.usage.jsp") })
public class LteDataUsageController
    implements ILteDataUsageController {

    @Override
    @Action("instantUsage")
    public String getInstantUsage() {
        // code here
    }

}
  • 實際顯示網址
http://www.somewhere.com/4g/instantUsage.action

results matching ""

    No results matching ""