Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable ViewState?

I'm coming into Java world from MS and ASP.NET and looking for the similar to ASP.NET component-based HTML framework in Java. After reviewing tons of links in internet it looks like JSF2 (with facelets) is best match (is this true by the way? or there are other better choices?).

The problem I'm encountering during evaluation right now is correct usage of JSF's view state. My final usage scenario would be a clustered WEB server and i'm NOT going to have any session/server-stored objects and i'm NOT going to use network bandwidth for dummy view state (see another guy's somewhat related problem here JSF Tuning).

I took some JSF2 tutorial and after setting javax.faces.STATE_SAVING_METHOD = client got ViewState generated into HTML of 440 chars (omygod, page contains just 1 dummy text input and 1 submit button). In "POST on submit" I do need only text from text input (10 chars) and not that dummy view state (440 chars).

So the question is - Is it possible to disable view state in JSF2?

Relevant links:

  • Use-case in ASP.NET - "Disable View State for a Page":
    http://www.ironspeed.com/articles/Disable%20View%20State%20for%20a%20Page/Article.aspx

  • Not helpful answer on stackoverflow:
    How to reduce javax.faces.ViewState in JSF

Update: Relevant links (from comments below):

  • JSF 2.0 partial state saving does not seem to work

  • "Stateless JSF": http://industrieit.com/blog/2011/11/stateless-jsf-high-performance-zero-per-request-memory-overhead

like image 808
Xtra Coder Avatar asked Jan 30 '12 08:01

Xtra Coder


People also ask

What happens when ViewState is disabled?

If you disable ViewState, and a page needs it, the page will no longer work.

Which property is used to disable ViewState?

To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true , set the ViewStateMode property of the page to Disabled, and set the ViewStateMode property of the control to Enabled.

How do I enable ViewState?

View state enables a server control to maintain its state across HTTP requests. View state for a control is enabled if all of the following conditions are met: The EnableViewState property for the page is set to true . The EnableViewState property for the control is set to true .

Does ViewState affect performance?

To accomplish our tasks, we use ViewState a lot, but as we know, it doesn't come free - it has a performance overhead. The ViewState is stored on the page in the form of a hidden variable. Its always advised to use the ViewState as little as possible. We also have other ways to reduce the performance overhead.


2 Answers

JSF is a component based framework which is heavily stateful - so you need the state somewhere, either sent to the client over the wire and posted in again, or on the server side. So AFAIK the answer is No, you cannot disable the View state. But you can minimize it - however some state will always need storing. This link is relevant.

If you're looking for a Java web framework which is not so stateful - then maybe look at some Action based framework like Struts or Stripes, so you can work in Request scope and not need a component tree present (or rebuilt) on a postback. The Play framework has been gaining good press - which is specifically designed to target RESTful architectures. I do not have experience of this myself, but you may want to investigate it. Taken from the Play website:

Simple stateless MVC architecture

You’ve got a database on one side and a web browser on the other. Why should you have a state in between?

Stateful and component based Java Web frameworks make it easy to automatically save page state, but that brings a lot of other problems: what happens if the user opens a second window? What if the user hits the browser back button?

like image 52
planetjones Avatar answered Sep 19 '22 08:09

planetjones


Since Mojarra 2.1.19 and Mojarra 2.2.0-m10 it's possible to disable the state saving on a per-view basis by setting the transient attribute of <f:view> to true.

<f:view transient="true">
    ...
    <h:form>
        ...
    </h:form>
    ...
</f:view>

See also:

  • JSF is going stateless
  • What is the usefulness of statelessness in JSF?
like image 25
BalusC Avatar answered Sep 19 '22 08:09

BalusC