如果你讨厌XML,你可以尝试着使用基于注解的形式配置 prettyfaces也开始了annotation潮流。
在web.xml中加入以下的
<context-param> <param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name> <param-value>com.example.myapp,com.ocpsoft</param-value> </context-param>
com.example.myapp和com.ocpsoft指明了这两个包是要被annotation扫描到的。如果你一点都不想用annotation也可以加上下面的<context-param> <param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name> <param-value>none</param-value> </context-param>一般情况prettyfaces只会扫描到class路径的annotation,如果你也想让他扫描到lib引入的jar中的annotation可以加上下面这句话<context-param> <param-name>com.ocpsoft.pretty.SCAN_LIB_DIRECTORY</param-name> <param-value>true</param-value> </context-param>Simple URL mappings
@Named("bean") @RequestScoped @URLMapping(id = "store", pattern = "/store/", viewId = "/faces/shop/store.jsf") public class StoreBean { /* your code */ }最佳实践@Named("bean") @RequestScoped @URLMappings(mappings={ @URLMapping(id = "categoryBean", pattern = "/store/#{ bean.category }/", viewId = "/faces/shop/store.jsf"), @URLMapping(id = "categoryBean2", pattern = "/shop/#{ bean.category }/", viewId = "/faces/shop/store.jsf") }) public class CategoryBean { private String category; /* Getters & Setters */ }Page actions
@Named("bean") @RequestScoped @URLMapping(id = "viewItem", pattern = "/store/item/#{ bean.itemId }/", viewId = "/faces/shop/item.jsf") public class CategoryBean { private String itemId; private Item item; @Inject StoreItems items; @URLAction public String loadItem() { if ( itemId != null ) { this.item = items.findById(itemId); return null; } // Add a message here, "The item {..} could not be found." return "pretty:store"; } /* Getters & Setters */ }prettyfaces支持的我们如果采用一个页面 一个backbean的方式进行开发,那么我们可以在这个backBean的前面加上@URLMapping(id = "viewItem", pattern = "/store/item/#{ bean.itemId }/", viewId = "/faces/shop/item.jsf")类似这样的注解来restful化我们的应用同时注意一个小BUG,在我使用pretty faces 3.2.0 version的时候其中基于注解的配置方式pattern = "/store/item/#{ bean.itemId }/" 其中 #{ bean.itemId } 后边必须跟着 一个 / 来结束 要不然回出问题。