1

I was expecting that whenever I click the commandLink, I'd see the bean.count property increment.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
   xmlns:h="http://java.sun.com/jsf/html"> 
<h:head>
  <title>test page</title>
</h:head>
<h:body> 
   <h:form>
     <h:commandLink action="#{myBean.inc}" value="Click me"/><br/>
     #{myBean.count} 
   </h:form>
</h:body>
</html> 

With a bean

package com.test;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class MyBean implements Serializable{
  int count=0;
  public int getCount() {
    return count;
  }
  public void inc(){
    count++;
  }
}

and yet, it only increments from 0 to 1 on the first click. Could anyone tell me why?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
twisted
  • 742
  • 2
  • 11
  • 19

3 Answers3

1

Action has a special meaning as you can see here. Use an actionListener instead and add the ActionEvent parameter.

public void inc(ActionEvent event){
  count++;
}
 <h:commandLink actionListener="#{myBean.inc}" value="Click me"/><br/>
Grim
  • 1,938
  • 10
  • 56
  • 123
  • Thanks Peter, there's no way of just invoking the bean method as it stands, ie with no parameters? Strangely, the code I originally posted, worked for a short while, with the incremented value getting shown. When I then changed, it as you suggested, again it worked, for a while but then stopped working! I've tried restarting Tomcat, closing & reopening Firefox, clearing the browser cache & deleting all cookies. Still doesn't work though! – twisted Apr 07 '16 at 19:54
  • Actually according to this answer http://stackoverflow.com/a/3909382/564045, action would be correct in this scenario – PDStat Apr 08 '16 at 07:57
  • @PaulStatham actionListener would be more correct. Anyway, it was a session problem. – Grim Apr 08 '16 at 09:28
1

Well, I've found the cause of my problem.

It arose from the way I was navigating to & then testing my page in Firefox.

If I freshly start Firefox and put in the URL of my test page, everything works.

However, I'm deploying my test webapp to Tomcat. If I use the Tomcat Web Application Manager page and navigate to my test webapp from the Application Manager's List Applications page, the bean, supposedly session scoped seems to get recreated every time I click the link. Watching the JSESSIONID cookie, a new one gets created every time.

The same sequence of user actions in IE doesn't loose the session.

Firefox 45.0.1

IE 8

Tomcat 7.0.55

Mojarra 2.2.0

Thanks to Peter & Ravi for your help.

twisted
  • 742
  • 2
  • 11
  • 19
-1

The same code works correctly for me :)

package com.test;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class MyBean implements Serializable {
    int count = 0;

    public int getCount() {
        System.out.println("getCount " +count);
        return count;
    }

    public void inc() {
        count++;
        System.out.println("inc " +count);
    }
}

MyBean.xhtml page

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>test page</title>
</h:head>
<h:body>
    <h:form>
        <h:commandLink action="#{myBean.inc}" value="Click me" />
        <br />
     #{myBean.count} 
   </h:form>
</h:body>
</html>

Console log

getCount 0
inc 1
getCount 1
inc 2
getCount 2
inc 3
getCount 3
inc 4
getCount 4
inc 5
getCount 5
inc 6
getCount 6
inc 7
getCount 7
inc 8
getCount 8
inc 9
getCount 9
inc 10
getCount 10
inc 11
getCount 11
inc 12
getCount 12
inc 13
getCount 13
inc 14
getCount 14
inc 15
getCount 15
inc 16
getCount 16
inc 17
getCount 17
inc 18
getCount 18
inc 19
getCount 19
inc 20
getCount 20
inc 21
getCount 21

Test Environment : Eclipse Java EE IDE for Web Developers. Version: Mars.1 Release (4.5.1), JSF 2.2.4, JDK 1.8.0_73 , Tested it on IE , Chrome and Firefox.

Check the case when it fails does it even call the inc() method and if there is some session timeout or any message in the log file.

Ravi
  • 391
  • 2
  • 18
  • 1
    All due respect, "Works for me" is not an answer which solves the concrete problem as described in the question in its current form. If you're unable to explain and solve the problem, better post a comment next time when you have sufficient reputation. – BalusC Apr 08 '16 at 07:45
  • Thanks BalusC. Just started with my first set of contribution in stackoverflow. Hope to provide more specific answers and comments. – Ravi Apr 08 '16 at 14:22
  • I applaud attempts to reproduce the problem in a sandbox environment based on questions asked here. It's really a nice learning exercise for starters. Keep doing that. But posting "Works for me" as an Answer is just unhelpful to the asker. – BalusC Apr 08 '16 at 14:25