11

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:

Update: Relevant links (from comments below):

Community
  • 1
  • 1
Xtra Coder
  • 3,389
  • 4
  • 40
  • 59
  • I'm not familiar with ASP.NET, but JSF2 uses partial state saving, in order to save memory. You might not need to disable it. See http://stackoverflow.com/a/4391494/456062 – Adam Jan 30 '12 at 09:16
  • 2
    *"Is it possible to disable view state in JSF2?"*: Yes, it's possible: http://industrieit.com/blog/2011/11/stateless-jsf-high-performance-zero-per-request-memory-overhead/ – BalusC Jan 30 '12 at 11:52
  • Adam, thanks for the hint. It appears that 440 above mentioned chars are for 'partial' mode. I retried same use-case on another workstation and have got 225 chars for 'partial' and 2021 for 'non-partial' (both seems to be overhead for the form of 1 input field + submit button). But the good sign is adding 9 more (input + button) increased view state by only 50 chars - i.e. hopefully it will possible to design application with view state not growing to 100+K. – Xtra Coder Jan 30 '12 at 11:57
  • 1
    BalusC, your link seems to hit the point. However ... it does say that deep hacking is required and JSF authors actually failed to catch one of the really important aspects during years of JSF evolution since 2004. – Xtra Coder Jan 30 '12 at 12:16
  • 1
    @XtraCoder I like BalusC's link but it seems like an affront to the true purpose of JSF. I used to be an ASP.NET developer as well and I feel the same way about stateless ASP.NET, however the difference there is that there are no other well known solutions for stateless web browsing in .NET technologies that are not obsolete. Java doesn't have this problem as there are a number of frameworks that more naturally do stateless web pages, like JSP+Struts for example. – maple_shaft Jan 30 '12 at 12:24
  • @maple_shaft, to my mind good framework should let people do what they want if they know what they are doing :) I would not argue that "being stateful is bad", I'm saying that it is bad "to be stateful all the time". In 10% of cases I would not need ViewState and moreover usage scenario will be greatly degraded because I'm not able to disable ViewState. Should I re-implement 10% of the site in another technology? – Xtra Coder Jan 30 '12 at 16:37
  • 1
    @XtraCoder In my mind a good framework is one that perfects, simplifies and streamlines a solution to a specific type of problem, not one that tries to poorly cover every possible solution that could be needed. Just like with restaurants, would you go to one that had an enormous menu with mediocre food, or one that has a small menu of items that the chef put a lot of focus on making delicious? The latter tends to be more successful. – maple_shaft Jan 31 '12 at 12:20

2 Answers2

10

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?

Community
  • 1
  • 1
planetjones
  • 12,469
  • 5
  • 50
  • 51
  • 3
    +1 For a good answer. JSF by its very nature is intended to be stateful. Asking for stateless JSF is like needing a pet cat but trying to domesticate a puma. Yeah it might work with a lot of effort, but you end up with a very unusual and difficult to maintain puma that might end up killing you later. Theoretically it is possible, but why even bother? Struts or Stripes would be so much more appropriate. – maple_shaft Jan 30 '12 at 12:18
8

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555