我在Spring Webflow 2.3中使用JSF2.0。
我一直在尝试获取有关我的页面中点击了哪些链接以及点击了多少次的信息。现在它可以工作,当我留在流程,只是去另一个流程,但当我重定向,然后返回它只是重置我的bean。我觉得我错过了什么,但我不知道是什么。
我已经尝试过我的小脑袋可以想到的一切,我已经尝试过研究,但我的猜测是我的搜索参数不正确,因为我找到的所有内容都无济于事。
这是我的支持bean:
@SessionScoped
public class CountBean implements Serializable{
private static final long serialVersionUID = 7498596369206276696L;
private static Log logger = LogFactory.getLog(CountBean.class);
private String link;
private Map<String,Integer> counter;
public CountBean(){
if(counter == null || counter.isEmpty()){
counter = new LinkedHashMap<String,Integer>();
}
link = null;
}
public void countTheCount(){
if(link != null){
int value;
if(counter.containsKey(link)){
value = counter.get(link);
value++;
counter.put(link, value);
} else {
value = 1;
counter.put(link, value);
}
logger.debug("Click: "+link+" , times: "+value);
}
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
这是我的流程:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<var name="testBean" class="be.admb.myadmbresearch.beans.TestBean" />
<var name="countBean" class="be.admb.myadmbresearch.beans.CountBean" />
<view-state id="test-view" model="testBean">
<transition on="logTest">
<evaluate expression="testBean.testWarning()" />
<evaluate expression="countBean.countTheCount()" />
</transition>
<transition on="gotoxy" to="sample-view">
<evaluate expression="countBean.countTheCount()" />
</transition>
<transition on="gotoab" to="sample-redirect-view">
<evaluate expression="countBean.countTheCount()" />
</transition>
<transition on="gotogoogle" to="gotoGoogle-view">
<evaluate expression="countBean.countTheCount()" />
</transition>
</view-state>
<view-state id="sample-redirect-view" view="externalRedirect:contextRelative:sample.html" />
<view-state id="gotoGoogle-view" view="externalRedirect:http://www.google.be" />
<view-state id="sample-view">
<transition on="keerdekewere" to="test-view">
<evaluate expression="countBean.countTheCount()"/>
</transition>
</view-state>
编辑:忘记添加重定向的样本流:
<var name="countBean" class="be.admb.myadmbresearch.beans.CountBean" />
<view-state id="sample-view" >
<transition on="keerdekewere" to="test-view">
<evaluate expression="countBean.countTheCount()"/>
</transition>
</view-state>
<view-state id="test-view" view="externalRedirect:contextRelative:test.html" />
如果有人能帮助我,甚至想到任何事情,我会非常感激。
先谢谢。
圣拉斐尔
答案 0 :(得分:0)
我已经通过使用HttpSession修复它,每次运行countTheCount()时都会直接设置计数器,现在这似乎有效,但我不知道它是否是正确的解决方案,一些反馈会很好。
这就是我得到的:
public CountBean(){
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
if(session != null){
counter = (Map<String, Integer>) session.getAttribute("counter");
if(counter == null || counter.isEmpty()){
counter = new LinkedHashMap<String,Integer>();
session.setAttribute("counter", counter);
}
}
link = null;
}
public void countTheCount(){
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
if(link != null){
int value;
if(counter.containsKey(link)){
value = counter.get(link);
value++;
counter.put(link, value);
session.setAttribute("counter", counter);
} else {
value = 1;
counter.put(link, value);
session.setAttribute("counter", counter);
}
logger.debug("Click: "+link+" , times: "+value);
}
}