<input type="file">、
<input type="file">
<navigation-rule> <from-view-id>*</from-view-id> <navigation-case> <from-outcome>createProduct</from-outcome> <to-view-id>/createProduct.jsp</to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id>/createProduct.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/uploadImage.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>retry</from-outcome> <to-view-id>/createProduct.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>cancel</from-outcome> <to-view-id>/productList.jsp</to-view-id> </navigation-case> </navigation-rule>
public String createAction() { try { Product product = ProductBeanBuilder.createProduct(this); //Save the product. this.serviceLocator.getCatalogService().saveProduct(product); //Store the current product id inside the session bean. //For the use of image uploader. FacesUtils.getSessionBean().setCurrentProductId(this.id); //Remove the productList inside the cache. this.logger.debug("remove ProductListBean from cache"); FacesUtils.resetManagedBean(BeanNames.PRODUCT_LIST_BEAN); } catch (DuplicateProductIdException de) { String msg = "Product id already exists"; this.logger.info(msg); FacesUtils.addErrorMessage(msg); return NavigationResults.RETRY; } catch (Exception e) { String msg = "Could not save product"; this.logger.error(msg, e); FacesUtils.addErrorMessage(msg + ": Internal Error"); return NavigationResults.FAILURE; } String msg = "Product with id of " + this.id + " was created successfully."; this.logger.debug(msg); FacesUtils.addInfoMessage(msg); return NavigationResults.SUCCESS; }
<managed-bean> <description> Backing bean that contains product information. </description> <managed-bean-name>productBean</managed-bean-name> <managed-bean-class>catalog.view.bean.ProductBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>id</property-name> <value>#{param.productId}</value> </managed-property> <managed-property> <property-name>serviceLocator</property-name> <value>#{serviceLocatorBean}</value> </managed-property> </managed-bean>
ServletContext context = FacesUtils.getServletContext(); this.appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context); this.catalogService = (CatalogService)this.lookupService(CATALOG_SERVICE_BEAN_NAME); this.userService = (UserService)this.lookupService(USER_SERVICE_BEAN_NAME);
public interface CatalogService { public Product saveProduct(Product product) throws CatalogException; public void updateProduct(Product product) throws CatalogException; public void deleteProduct(Product product) throws CatalogException; public Product getProduct(String productId) throws CatalogException; public Category getCategory(String categoryId) throws CatalogException; public List getAllProducts() throws CatalogException; public List getAllCategories() throws CatalogException; }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping package="catalog.model.businessobject"> <class name="Product" table="product"> <id name="id" column="ID" unsaved-value="null"> <generator class="assigned"/> </id> <property name="name" column="NAME" unique="true" not-null="true"/> <property name="price" column="PRICE"/> <property name="width" column="WIDTH"/> <property name="height" column="height"/> <property name="description" column="description"/> <set name="categoryIds" table="product_category" cascade="all"> <key column="PRODUCT_ID"/> <element column="CATEGORY_ID" type="string"/> </set> </class> </hibernate-mapping> CatalogDao is wired with HibernateTemplate by Spring: <!-- Catalog DAO Definition: Hibernate implementation --> <bean id="catalogDao" class="catalog.model.dao.hibernate.CatalogDaoHibernateImpl"> <property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property> </bean>