Struts2 - Is this a great strategy for a "MVC" framework?
Im using JSF time ago, and i see th开发者_如何学Cat 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
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.
精彩评论