File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Alternative Implementations of MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Alternative Implementations of MVC" Watch "Alternative Implementations of MVC" New topic
Author

Alternative Implementations of MVC

Alex Prohorov
Greenhorn

Joined: Oct 02, 2012
Posts: 17
On the springsource.org site I read:
If you do not want to use Spring's web MVC, but intend to leverage other solutions that Spring offers, you can integrate the web MVC framework of your choice with Spring easily. Simply start up a Spring root application context through its ContextLoaderListener, and access it through its ServletContext attribute (or Spring's respective helper method) from within a Struts or WebWork action. No "plug-ins" are involved, so no dedicated integration is necessary. From the web layer's point of view, you simply use Spring as a library, with the root application context instance as the entry point.

So it tells about using Spring as a library. I am confused because calling from Struts (I assume same as from WebWork) action implies that we are already using Struts for MVC, not an alternative. An example I would like to see (if it is possible at all): you hit an URL and (as in all samples) get the first page but already rendered by some alternative MVC implementation say of your own.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

What they are saying is for the web tier, you can use Spring MVC, Struts, Tapestry, JBoss Seam, Servlets, etc and still use the Spring Framework for the middle tier. Spring MVC != Spring Framework. Spring MVC is a module to the Spring Framework for the web tier.

So in Struts I can have an Action class extend Spring's ActionSupport class which gives it a getApplicationContext method so that I could in an Action call getBean("orderService")

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Alex Prohorov
Greenhorn

Joined: Oct 02, 2012
Posts: 17
Thank you Mark!

What I am asking for is if it is possible tho use proprietary presentation layer without even mentioning (not to tell - using) existing things - Struts, Velocity etc. Not even JSP. Ideally there would be an API for that.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Alex Prohorov wrote:Thank you Mark!

What I am asking for is if it is possible tho use proprietary presentation layer without even mentioning (not to tell - using) existing things - Struts, Velocity etc. Not even JSP. Ideally there would be an API for that.


I still don't understand.

I can use any presentation layer I want, even internally created ones. But internally ones will have to write their own basic integration to Spring Framework. When you say not even JSP, I can point to the Play Framework as an example of a presentation layer that isn't Servlet/JSP spec for web that has Spring integration. There is also Swing, or public static void main() as "client layers" to access Spring ApplicationContext. It runs anywhere in any environment. Because it is a POJO.

Hope that helps

Mark
Alex Prohorov
Greenhorn

Joined: Oct 02, 2012
Posts: 17
Mark!

Actually you got that right. Spring integration of Play! or Swing is very close to the point of my interest. The problem is that I am not much familiar with those two and just a beginner in Spring. And my question is - any hints on how that integration may be done in general. The document I quoted ("Simply start up a Spring root application context through its ContextLoaderListener, and access it through its ServletContext attribute (or Spring's respective helper method) from within a Struts") sounds a bit fuzzy on that. There should be a way NOT using Struts.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Alex Prohorov wrote:Mark!

Actually you got that right. Spring integration of Play! or Swing is very close to the point of my interest. The problem is that I am not much familiar with those two and just a beginner in Spring. And my question is - any hints on how that integration may be done in general. The document I quoted ("Simply start up a Spring root application context through its ContextLoaderListener, and access it through its ServletContext attribute (or Spring's respective helper method) from within a Struts") sounds a bit fuzzy on that. There should be a way NOT using Struts.


Yes, so first to explain the ContextLoaderListener. And to say it works as long as you are using any Web Framework in existence that is built on top the JSP/Servlet spec with a web.xml file. So the ContextLoaderListener is a ServletListener that gets called back when a .war file is first being deployed in a Web Container. As soon as the ServletContext is created, the listener gets called and it creates an XmlWebApplicationContext. It then puts this into the ServletContext, which is available application wide. So any component in the Web Tier, Struts Actions, Servlets, Spring MVC Controllers, etc can all have access to the ServletContext which gives them access to the ApplicationContext.

If you are not in a JSP/Servlet container, then you have to find some other means to Bootstrap and create that ApplicationContext. So based on the environment that it is in, find some bootstrap way to run. Since Play just has a public static void main method as the start of the web app, you would put the code in there to create an ApplicationContext.

Hope that helps clear it up for you.

Mark
Alex Prohorov
Greenhorn

Joined: Oct 02, 2012
Posts: 17
Thank you, Mark, all that is helpful. Actually what I am thinking about is adding one more presentation layer implementation to Spring specifically HybridJava which has some good features that are currently not present in Spring MVC but obviously lacks all the rest (out of MVC) facilities of Spring. It may be the case that I really do not need ServletContext cause HybridJava (as a presentation layer) does well without it but obviosly ApplicationContext has to be somehow created for the business and so on layers.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

I would assume HybridJava is deployed in a Servlet environment too. So in that code just use WebApplicationContextUtils.getWebApplicationContext(servletContext)

Mark
Alex Prohorov
Greenhorn

Joined: Oct 02, 2012
Posts: 17
Mark Spritzler wrote:I would assume HybridJava is deployed in a Servlet environment too.
Absolutely yes (but not in a JSP environment). But it has its own mechanisms of binding data within presentation layer (extremely straightforward, simple and fast) so I do not see what for may I need ApplicationContext in presentation layer if I reuse HybridJava and thus maybe there is no need to push ApplicationContext into ServletContext - its usage may be limited to Business Layer and below.

I am thinking about transforming the presentation part generated by HybridJava compiler into a library that may be taken as a whole and just inserted into the Spring environment. Maybe (to begin with) I will be lucky in taking the simple non-WEB application example from a book (like HelloWorldSpringDI from Pro.Spring.3.pdf) and hooking up the Sample application of HybridJava to it. Interface is not very clear yet.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Alex Prohorov wrote:
Mark Spritzler wrote:I would assume HybridJava is deployed in a Servlet environment too.
Absolutely yes (but not in a JSP environment). But it has its own mechanisms of binding data within presentation layer (extremely straightforward, simple and fast) so I do not see what for may I need ApplicationContext in presentation layer if I reuse HybridJava and thus maybe there is no need to push ApplicationContext into ServletContext - its usage may be limited to Business Layer and below.

I am thinking about transforming the presentation part generated by HybridJava compiler into a library that may be taken as a whole and just inserted into the Spring environment. Maybe (to begin with) I will be lucky in taking the simple non-WEB application example from a book (like HelloWorldSpringDI from Pro.Spring.3.pdf) and hooking up the Sample application of HybridJava to it. Interface is not very clear yet.


The ApplicationContext in the ServletContext IS ONLY the Business Layer and below components. For Spring MVC there is a separate ApplicationContext deployed and contained in the DispatcherServlet, so within the Front Controller Servlet there is a second ApplicationContext for the web tier components. So everything I said before is still what I was saying, and still applies. If I use a different view layer than Spring MVC, makes absolutely no difference when it comes to loading the ContextLoaderListener to create a middle tier business layer ApplicationContext to be exposed to the web tier components in any web framework.

" Absolutely yes (but not in a JSP environment)."

In Servlet but not JSP enviroment, there is no such difference. The Servlet/JSP environment is the same, it is the JSP/Servlet spec. Not two separate things. Now what you meant is that HybridJava runs in the JSP/Servlet environment/spec, but just doesn't use any jsp pages/files.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Alternative Implementations of MVC