HashMap单击计数器增量

时间:2016-06-06 08:44:55

标签: jsf click-counting

我正在使用JSF 2.2开发Web应用程序。我有以下HTML代码,一个包含文章的无序列表,其中包含指向其他页面的链接:

<h:form>    
<div class="wrapper col3">
  <div class="container">
    <div class="gallery">
      <ul>
        <li>
          <p class="imgtitle">CorelDRAW Installation Problems</p>
          <a class="fb_image" href="contracts/resources/imagini/corel.jpg"><h:graphicImage library="imagini" name="corel.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag1_corel.html" /></p>

        </li>
        <li>
          <p class="imgtitle">Facebook down? Current problems and status</p>
           <a class="fb_image" href="contracts/resources/imagini/facebook.jpg"><h:graphicImage library="imagini" name="facebook.jpg"/></a>       
           <p class="readmore" ><h:link value="Continue Reading &raquo;" onclick="#counter.hitCount" outcome="contracts/resources/data/imag2_facebook.html" /></p>
        </li>
        <li class="last">
          <p class="imgtitle">Samsung Galaxy S7 review: a return to old ways</p>
          <a class="fb_image" href="contracts/resources/imagini/galaxy.jpg" ><h:graphicImage library="imagini" name="galaxy.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag3_s7.html" /></p>
        </li>
        <li>
          <p class="imgtitle">Samsung wants you to feel VR, not just see it</p>
          <a class="fb_image" href="contracts/resources/imagini/vr.jpg" title="Image 1 Link Title Tag"><h:graphicImage library="imagini" name="vr.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag4_vr.html" /></p>
        </li>
        <li>
          <p class="imgtitle">Instagram feed will no longer be in chronological order</p>
          <a class="fb_image" href="contracts/resources/imagini/instagram.jpg" title="Image 1 Link Title Tag"><h:graphicImage library="imagini" name="instagram.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag5_instagram.html" /></p>
        </li>
        <li class="last">
          <p class="imgtitle">Everything we expect at Apple's event on March 21</p>
          <a class="fb_image" href="contracts/resources/imagini/apple.jpg" title="Image 1 Link Title Tag"><h:graphicImage library="imagini" name="apple.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag6_apple.html" /></p>
        </li>
          <li>
          <p class="imgtitle">Apple's iCloud may get tighter encryption</p>
          <a class="fb_image" href="contracts/resources/imagini/icloud.jpg" title="Image 1 Link Title Tag"><h:graphicImage library="imagini" name="icloud.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag7_icloud.html" /></p>
        </li>
        <li>
          <p class="imgtitle">Getting started with Windows 10's Messaging app</p>
          <a class="fb_image" href="contracts/resources/imagini/win10skype.jpg" title="Image 1 Link Title Tag"><h:graphicImage library="imagini" name="win10skype.jpg"/></a>
          <p class="readmore"><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag8_windows10.html" /></p>
        </li>
        <li class="last">
          <p class="imgtitle">How PlayStation VR compares with Oculus Rift, Vive</p>
          <a class="fb_image" href="contracts/resources/imagini/playstationvr.jpg" title="Image 1 Link Title Tag"><h:graphicImage library="imagini" name="playstationvr.jpg"/></a>
          <p class="readmore" ><h:link value="Continue Reading &raquo;" outcome="contracts/resources/data/imag9_sonyvr.html" /></p>
        </li>
      </ul>
      <br class="clear" />
      <p id="back-to-top"><a href="#top"><span></span>Back To Top</a></p>
    </div>
  </div>
</div>
    </h:form>

我有下面的ManagedBean类,这不是我需要的。

@ManagedBean(name="displaymostviewed")
@SessionScoped
public class DisplayMostViewed {
    private int count=0; 
    private static HashMap<Integer,String> map = new HashMap<Integer,String>();
    private static BufferedReader reader;

public DisplayMostViewed() {
    map.put(incrementCounter() , "awala");
    writeToFile(map);

}
public int incrementCounter(){
    count++;
    return count;    
}

public void writeToFile(HashMap<Integer,String> map){
    try{
    File fileTwo=new File("C:\\Users\\Puchu\\Documents\\NetBeansProjects\\mavenandJSF\\counter.txt");
    FileOutputStream fos=new FileOutputStream(fileTwo);
        PrintWriter pw=new PrintWriter(fos);

        for(Map.Entry<Integer,String> m :map.entrySet()){
            pw.println(m.getKey()+"="+m.getValue());
        }

        pw.flush();
        pw.close();
        fos.close();
    }catch(Exception e){}
}

我想要做的是将一些锚标记元素从HTML列表绑定到HashMap,并根据链接上网站访问者的点击量来增加计数器。

我想记住每个链接的计数器值,最后对它们进行排序并显示网页中访问最多的链接。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以创建一个循环来显示链接,使用ui:repeat或dataTable随意添加,然后链接到用于计算点击次数的操作方法(h:commandLink)...检索param name = param并单击键并增加特定计数器...

要检索点击的关键字:

FacesContext.getExternalContext().getRequestParameterMap().get("param");

前:

 <ul>
    <ui:repeat value="#{myBean.myMap.keySet().toArray()}" var="key">
     <h:commandLink action ="#{myBean.counterMethod}">
         <f:param name="param" value="#{key}" />
         <li>key:#{key}</li>
       <li>value:#{myBean.myMap[key]}</li>
    </h:commandLink>    
    </ui:repeat>
</ul>

但这只是一个想法,希望它有所帮助..