0

I get this error when I click on a p:commandButton in my page

java.lang.IllegalStateException: PWC3999: Cannot create a session after the response has been committed

The button is in an h:form and looks like this:

<p:commandButton value="Save" action="#{discussionManager.save}" ajax="false"/>  

But an h:commandButton works fine:

<h:commandButton value="Save" action="#{discussionManager.save}"/>  (this works)

This is the bean with the method in it

@Named
@RequestScoped
public class DiscussionManager {

    private static final Logger logger = Logger.getLogger("DiscussionManager");

    @Inject
    private DiscussionDao discussionDao;

    private Discussion discussion = new Discussion();

    @Produces
    @Named
    @RequestScoped
    public Discussion getDiscussion() {
        return discussion;
    }

    public String save() {
        logger.info("Hello");
        discussionDao.create(discussion);
        return "list";
    }
}

I've waited all day before posting this question because I feel like I should know how to get this working. But I've read and re-read my book, and loads of other posts. I just don't understand why it's not working.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Richard
  • 1,731
  • 2
  • 23
  • 54
  • If there's a way to get better debugging information out of JSF/Primefaces that would help me debug it that would also be really great. – Richard Apr 29 '12 at 19:43

3 Answers3

1

I suspect that it is because the bean goes out of scope. When you turn off ajax it submits the form and you are actually dealing with a new request. Try expanding your scope to a view scope.

SteveS
  • 1,008
  • 3
  • 18
  • 32
  • Thanks Steve. I changed it and I now get the same error message but now it comes out when I visit the page (rather than after the button is pressed). This is the first line of the exception which I forgot to add in my question: SEVERE: Error Rendering View[/discussion/new.xhtml] – Richard Apr 30 '12 at 13:43
  • What else is it saying in the stack trace? Check the console window as I have often found that there are other errors in there that do not get reported back to the browser when it displays the stack trace. – SteveS Apr 30 '12 at 14:07
  • I've figured it out (phew!). It's not that button that's causing the problem. I'm using a PrimeFaces editor widget. The button I mention submits the entered text (well, the whole page), but there's also a 'Clear' button that erases the contents, and THAT is the button that's causing the error. It's copied from the showcase example. I need to investigate that a bit more and update my answer etc when I know more. Not much else in any consoles btw, very little to go on from what I can see. If I find anything I'll post it here though. Thanks – Richard Apr 30 '12 at 14:36
1

Not quite sure what I changed to get it working, because now the whole thing is working fine. But for completeness, here's the relevant part of my page

                <h3>Write your message below</h3>
                <p:editor id="editor" 
                        value="#{discussion.message}" 
                        widgetVar="editor" 
                        width="600"
                        required="true"/>  

                <h:message for="editor"/>                                                

                <h:panelGrid columns="2" style="margin-top:10px">
                    <p:commandButton id="submitButton" value="Save" action="#{discussionManager.save}" ajax="false" icon="ui-icon-disk"/> 
                    <p:commandButton id="clearButton" type="button" value="Clear" onclick="editor.clear()" icon="ui-icon-close" />
                </h:panelGrid>    

And here's my bean. Hope someone finds this useful...

@Named
@RequestScoped
public class DiscussionManager implements Serializable {

    private static final Logger logger = Logger.getLogger("DiscussionManager");

    @Inject
    private DiscussionDao discussionDao;

    private Discussion discussion = new Discussion();

    @Produces
    @Named
    @RequestScoped
    public Discussion getDiscussion() {
        return discussion;
    }

    public String save() {
        logger.info("Hello");
        discussionDao.create(discussion);
        return "list";
    }
}
Richard
  • 1,731
  • 2
  • 23
  • 54
1

I can't comment on your final answer so I have to add another answer. I believe what you did to make your example work was to add widgetVar to your editor component. I had this problem as well. The example on the PrimeFaces Demo wouldn't work properly until I added the widgetVar line. You need to reference the widgetVar name in your onclick call rather than the id. I use different names between the id and widgetVar for clarity so I would change your editor code to:

<h3>Write your message below</h3>
 <p:editor id="editorID" 
        value="#{discussion.message}" 
        widgetVar="editorWidget" 
        width="600"
         required="true"/>  

  <h:message for="editor"/>                                                

<h:panelGrid columns="2" style="margin-top:10px">
    <p:commandButton id="submitButton" value="Save" action="#{discussionManager.save}" ajax="false" icon="ui-icon-disk"/> 
    <p:commandButton id="clearButton" type="button" value="Clear" onclick="editorWidget.clear()" icon="ui-icon-close" />
</h:panelGrid>    

I hope that helps clarify.

SteveS
  • 1,008
  • 3
  • 18
  • 32
  • Ah, thanks for this it is helpful. That could be the cause, I think I have had a similar problem in the past with widgetVar and something else. It's a shame it doesn't break in a more obvious way, though I suspect I might be looking in the wrong place. Someone pointed me in the direction of which I'm sure will also come in handy. Thanks for the tip though, appreciate it. – Richard May 01 '12 at 14:30