2

Im using JSF time ago, and i see that the concept of MODEL is really important there. Here, on struts2, i see that this concept is a bit obsolete. For example, seems that is impossible to set a Bean session scoped; instead i need to put the whole Bean (object) into the session (manually).

So, in some methods, this is not nice. For example, for the page-switch, i have implemented (on JSP page) this :

<s:div cssClass="content" id="content">
<%
    String pageValue=request.getParameter("page");
    if((pageValue!=null) && (pageValue.compareTo("articles")==0)) {
        %>
            <s:include value="articles/articles.jsp"></s:include>
        <%
    } else {
        %>
            <s:include value="homepage/homepage.jsp"></s:include>
        <%
    }
%>
</s:div> 

is this the right way to work with Struts2? Or is better put some values into Beans and generate the page accordings to the Beans values? (Model concept, but REALLY i don't know hot to set the Bean scope, and i wont put them on session. Else is like do procedual coding, and i can use PHP to do this :)).

I Don't know how to do it otherwise :)

Cheers

Roman C
  • 49,761
  • 33
  • 66
  • 176
markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

1

There are several ways...

1) Struts uses spring for DI, you too can use spring for this purpose look into the struts2-spring-plugin

2) You can use SessionAware as I mentioned in a previous question. However I did mention that interceptors work in conjunction because session scoped objects are cross cutting concerns by their very nature. Without the interceptors a solution will be forced to exist in your actions or worse the view layer... repeated over and over... which as you put it "is not nice".

3) If you are strongly MVC oriented look into modelDriven and scopedModelDriven. The later will set a model for your action which can be in the session scope (other scopes are possible) if the model does not exist then it will be instantiated for you... This is good for multi page forms, the successful completion of the form can then remove this object. I don't really like modelDriven it can complicate access to the Action.

Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • 1
    Actually, Struts2 uses XWork for DI, but there are plug-ins for using Spring or Guice. – Steven Benitez Dec 12 '10 at 16:47
  • But why Spring? I don't want to use Spring. Spring it's another framework, isnt it? How can I do it with a clear Struts2 application? – markzzz Dec 12 '10 at 21:22
  • All frameworks have their niche, struts2 is a good choice for a web framework. But if you want to externalize object creation and management then something that makes DI easy and makes that concern front and center is the right way to go. When it comes time to address serious DB issues an ORM framework will get you there. It never ends! – Quaternion Dec 12 '10 at 22:12