my faces jscookMenu 问题
winfastvv
2007-07-17
在开发中,使用到了my faces 的jscookmenu组件,其功能是显示出一个分类的功能
页面代码如下: <h:form> <t:jscookMenu layout="vbr" theme="ThemeOffice"> <t:navigationMenuItems id="navigationmenu" value="#{softWareCategoryBean.softWareList}" /> </t:jscookMenu> </h:form> 其中softWareCategoryBean.softWareList是通过递归出来的 item 类型为NavigationMenuItem 并设置了其的action及actionListener item.setValue(id);//id值是动态数据 item.setAction("#{softWareListBean.toSoftwarelist}"); item.setActionListener("#{softWareListBean.menuListener}"); 以下是action及actionListener定义 public String toSoftwarelist(){ return "softwarelist";//跳转到software.jsp页面 } public void menuListener(ActionEvent event){ if(event.getComponent() instanceof HtmlCommandJSCookMenu){ String tmp_id = (String)((HtmlCommandJSCookMenu)event .getComponent()).getValue(); this.setId(new Long(tmp_id));//this.setId()为当前bean的一个属性 } } software.jsp对应的backing bean 为 softWareListBean software.jsp的作用是跟据所选择的分类,显示此分类下的产品列表[带分页功能] 现在问题是,当 首次 进入software.jsp是工作正常的,但当我点下一页的时候,则丢失了在actionListener中设置的id 则[this.setId(new Long(tmp_id));],请问如何保存此Id呢? software.jsp中有此一段 <h:form> ... <h:selectOneMenu value="#{softWareListBean.page.currentPage}" > <f:selectItems value="#{softWareListBean.page.menuList}"/> </h:selectOneMenu> <h:inputHidden id="id" value="#{softWareListBean.id}"/> <t:commandButton value="go" action="#{softWareListBean.go}"/> <t:commandButton value="prvs" action="#{softWareListBean.prvs}" /> <t:commandButton value="next" action="#{softWareListBean.next}"/> </h:form> 本人才刚学习jsf,请望多多指教! MSN:winfastvv@163.com |
|
Javabengou
2007-07-17
public void menuListener(ActionEvent event){
if(event.getComponent() instanceof HtmlCommandJSCookMenu){ String tmp_id = (String)((HtmlCommandJSCookMenu)event .getComponent()).getValue(); this.setId(new Long(tmp_id));//this.setId()为当前bean的一个属性 } } 我没测试过你的这段代码,代码也是大概看了下 你这里有个地方 if(event.getComponent() instanceof HtmlCommandJSCookMenu) 这里只是判断你的组件是否为HtmlCommandJSCookMenu 你看试下,在HtmlCommandJSCookMenu这个组件的页面中设置他的ID属性 在后台代码中通过他的ID值来取得这个HtmlCommandJSCookMenu组件,看是否问题可以解决!即<t:jscookMenu layout="vbr" theme="ThemeOffice" ID="XXX"> 对于值的传递看能否用<t:updateActionListener/>看能否解决你上面的问题! |
|
winfastvv
2007-07-17
首先谢谢你的回复!
我后台是通过这个来生成的 item.setValue(id);//id值是动态数据 item.setAction("#{softWareListBean.toSoftwarelist}"); item.setActionListener("#{softWareListBean.menuListener}"); 如何利用你的这个? <t:updateActionListener/> 另外:我现在是首次进入页面是正常的..但当在页面有提交动作,则backing bean的id值丢失,而id值是从softWareListBean.menuListener监听来的...是否有关系.. 我的监听写在softWareListBean里可以的吗?? |
|
Javabengou
2007-07-17
说实话,通过这么几个代码真的看不出什么
如果有时间的话,把全部关联的代码贴上来 |
|
winfastvv
2007-07-17
显示jscookMenu的页面,如下: a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%> <%@ taglib prefix="tiles" uri="http://struts.apache.org/tags-tiles"%> <f:verbatim> <div id="myaccountsidebar3"style=" padding:1px;"> <div class="note" id="tab">Software Categories</div> <div class="s_c1" ></f:verbatim><h:form> <t:jscookMenu layout="vbr" theme="ThemeOffice"> <t:navigationMenuItems id="navigationmenu" value="#{softWareCategoryBean.softWareList}" /> </t:jscookMenu></h:form><f:verbatim> </div> </div> <div id="myaccountsidebar3blank"></div> <div id="myaccountsidebar3"style=" padding:1px;"> <div class="note" id="tab">Top 10 Software</div></f:verbatim> <h:dataTable var="sale" value="#{softWareSaleBean.saleList}" styleClass="blue12"> <h:column> <h:graphicImage value="/page/purchase/images/list_number_#{(softWareSaleBean.picnum)}.gif" ></h:graphicImage> <h:outputLink value="#{facesContext.externalContext.requestContextPath}/page/purchase/software.jsf" > <h:outputText value="#{sale[1]}" /> <f:param name="id" value="#{sale[0]}" /> </h:outputLink> </h:column> </h:dataTable><f:verbatim> </div> </f:verbatim> 产生jscookMenu的bean,如下: a.jsp对应的bean a.java public class SoftMenuList { private SoftWareCategoryDao dao; private List menuList = new ArrayList(); private List purchaseList = new ArrayList(); private List nextList = null; private int size = 0; public SoftMenuList(){} /** * 主方法,得到所有分类的列表 */ public List getSoftMenuList(){ if(menuList.size() > 0){ return menuList; }else{ List tempList = dao.getTopLevel(); if(null != tempList){ Iterator it = tempList.iterator(); TbSoftwareCategories tempSoft = null; NavigationMenuItem tempItem = null; while(it.hasNext()){ tempSoft = (TbSoftwareCategories)it.next(); nextList = dao.getNextLevel(tempSoft.getId()); if(nextList != null)size = nextList.size(); tempItem = getMenuNaviagtionItem(tempSoft.getName(),null, tempSoft.getId(),size); menuList.add(tempItem); loadNextMenu(tempSoft.getId(),tempItem); } } } return menuList; } /** * 递归加载分类菜单项 * @param parentId 父级编号 * @param item 菜单项 */ private void loadNextMenu(Long parentId,NavigationMenuItem item){ List tempList = dao.getNextLevel(parentId); if(null != tempList){ Iterator it = tempList.iterator(); TbSoftwareCategories tempSoft = null; NavigationMenuItem tempItem = null; while(it.hasNext()){ size = 0; tempSoft = (TbSoftwareCategories)it.next(); nextList = dao.getNextLevel(tempSoft.getId()); if(nextList != null)size = nextList.size(); tempItem = getMenuNaviagtionItem(tempSoft.getName(),null, tempSoft.getId(),size); item.add(tempItem); loadNextMenu(tempSoft.getId(),tempItem); } } } /** * 实例一个菜单项,并设置该菜单属性 * @param label 显示的菜单名 * @param action 该菜单的动作 * @param id 该菜单的值 * @return 返回一个NavigationMenuItem的菜单对象 */ private static NavigationMenuItem getMenuNaviagtionItem(String label, String action,Long id,int _size){ NavigationMenuItem item = new NavigationMenuItem(label, action); item.setValue(id); if(_size == 0){ item.setAction("#{softWareListBean.toSoftwarelist}"); item.setActionListener("#{softWareListBean.menuListener}"); } return item; } public void setDao(SoftWareCategoryDao _dao){ dao = _dao; } } 此页是当点击jscookmenu组件后,所跳转到的页面 b.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%> <%@ taglib prefix="tiles" uri="http://struts.apache.org/tags-tiles"%> <f:verbatim> <div id="myaccountmainbody3"> <div style="padding:0px 0 10px 0"> <div class="bar" id="position" style="background:url(images/in.jpg) no-repeat 110px 40% ">Purchase</div> <div id="line1"></div> </div> </f:verbatim> <h:form> <h:inputHidden id="id" value="#{softWareListBean.id}"/> <h:dataTable var="product" value="#{softWareListBean.productList}" styleClass="tablebody" rowClasses="softlist,softlist2" headerClass="head" cellpadding="1" cellspacing="1"> <h:column id="a"> <f:facet name="header"><h:outputText value="Name"/></f:facet> <h:outputLink value="#{facesContext.externalContext.requestContextPath}/page/purchase/software.jsf" > <h:outputText value="#{product.name}"/> <f:param name="id" value="#{product.id}"/> </h:outputLink> </h:column> <h:column> <f:facet name="header"><h:outputText value="Rating"/></f:facet> <h:graphicImage url="../myaccount/images/s.gif"/> <h:outputText value="#{product.SRating}"/> <h:graphicImage url="../myaccount/images/u.gif"/> <h:outputText value="#{product.URating}"/> </h:column> <h:column> <f:facet name="header"><h:outputText value="Credit"/></f:facet> <h:graphicImage url="../myaccount/images/credit_07.jpg"/> <h:outputText value="#{product.credits}"/> </h:column> <h:column> <f:facet name="header"><h:outputText value="Date Added"/></f:facet> <h:outputText value="#{product.dateAdded}"> <f:convertDateTime dateStyle="both" pattern="MM/dd/yyyy"/> </h:outputText> </h:column> <h:column> <h:commandLink value="Add" action="#{shoppingCartBean.ShoppingNavigater}"> <f:param name="id" value="#{product.id}"/> <f:param name="credits" value="#{product.credits}"/> </h:commandLink> </h:column> </h:dataTable> </h:form> <f:verbatim> <div style="padding-top:10px;"> <table width="100%" height="25" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"> <tr> <td valign="middle" style="padding-right:10;"> </td> </tr> </table> </div> </div> </f:verbatim> b.jsp所对应的bean b.java public class SoftWareListBean{ //属性 private Long id ; private Page page = new Page(); private SoftWareProductDao softWareProductDao; private List productList = null; public List getProductList() { productList = softWareProductDao.getSoftwareList(this.getId()); return productList; } public void setProductList(List productList) { this.productList = productList; } /** * 导航器 * @return */ public String toSoftwarelist(){ return "softwarelist"; } /** * 监听器 * @param event * @return */ public void menuListener(ActionEvent event){ if(event.getComponent() instanceof HtmlCommandJSCookMenu){ String tmp_id = (String)((HtmlCommandJSCookMenu)event .getComponent()).getValue(); this.setId(new Long(tmp_id)); } } public SoftWareProductDao getSoftWareProductDao() { return softWareProductDao; } public void setSoftWareProductDao(SoftWareProductDao softWareProductDao) { this.softWareProductDao = softWareProductDao; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } } faces的配置文件 <managed-bean> <managed-bean-name>softWareListBean</managed-bean-name> <managed-bean-class> com.spotmau.purchase.view.SoftWareListBean </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>id</property-name> <value>#{param.id}</value> </managed-property> <managed-property> <property-name>softWareProductDao</property-name> <value>#{softWareProductDao}</value> </managed-property> </managed-bean> <navigation-rule> <from-view-id>*</from-view-id> <navigation-case> <from-outcome>softwarelist</from-outcome> <to-view-id>/softwarepagelist.jsf</to-view-id> </navigation-case> </navigation-rule> |