MVC in a Google App Engine Java world

2020-02-17 06:37发布

问题:

I'm coming to Java from C# & ASP.NET MVC, I'd love to find an equivalent in the Java world that I could use on the Google App Engine. I've already started to have a play with FreeMarker and even made the first steps towards writing a very simple framework. Ideally I wouldn't have to do all the hard work though, someone must have done this already! So my question is - what frameworks are there out there that would be familiar for me coming from ASP.NET MVC and I could use them on Google App Engine for Java.

The key things I'd want are:

  • Simple Routing - /products/view/1 gets mapped to the view action of the products controller with the productid of 1
  • Template Engine - some way of easily passing 'ViewData' to the view, and from the view easily accessing it, ideally I'd love to avoid anything that is too XMLy (thus why I like FreeMarker).

回答1:

I am currently working on a Google App Engine app using Spring MVC. It is a lot more mature than ASP.NET MVC so you shouldn't be disappointed. As an added bonus you have the whole IoC power of Spring.

For the view layer I am trying out Velocity. It is pretty simple but I have yet to decide if I will prefer it over JSPs. I had a brief look at FreeMaker but didn't like what I saw. If you want to stay away from XML'y JSP templates than I recommend you give Velocity a spin.

The only problem I have had with Spring on GAE is file uploading. The MultipartResolver implementations both rely on a temporary file directory. After writing my own implementation I'm back to seamless uploading of files in my models.



回答2:

There are a couple of MVC frameworks that you should consider (that's what I'm doing now). Initially, I went with Spring MVC (3.0) and the cold start on GAE is horrendous! It takes about 10 seconds to start (and I'm not even using anything complex, like spring security, etc), so I need to use a cron job to keep it alive. So I don't recommend that you use Spring at all on GAE.

Take a look at the following frameworks:

VRaptor

Slim3

Google Sitebricks

As for the templating, I use Sitemesh -- used it for quite a while now, so don't see a need to switch.

Hope this helps!



回答3:

Play Framework would fit the bill. It's a modern MVC framework for Java and has a Google App Engine module to provide compatibility. See http://www.playframework.org/modules/gae.

Another benefit to using Play would be portability. You can deploy Play using its built-in webserver (optionally behind apache for caching, load-balancing, etc) or you can bundle as a .war file for deployment to the many Java application servers (Glassfish, Tomcat, ...).



回答4:

I also have a strong preference for Freemarker. I suggest that you look at the Induction framework, its template engine is pluggable but the default support is for Freemarker. Induction is a light-weight and fast MVC framework (7.7K lines) but has many features absent in the major MVC frameworks, such as:

  • dynamic reloading during development when you change the controllers, views or models
  • file uploads so simple (not much different that a string input)
  • no configuration required for controllers
  • capability to analyze dependencies between your Models, Views and Controllers using your IDE
  • regular expression based URL mappings
  • best support for managing redirects of any MVC framework

As for the file upload issue raised by @pjesi, Induction allows you to set the size threshold at which files getting written to disk. If this is set large enough it should not try to write any files to disk.

Check out the getting started tutorial here: http://www.inductionframework.org/getting-started-tutorial.html



回答5:

JavaServer Faces (JSF) I've heard is similar to the microsoft MVC framework. (I use JSF but have never used .NET MVC). And I believe JSF 1.1 works fine on Google App Engine, and I suspect that the newer versions of JSF will work if you stick to using XHTML instead of JSPs as the templates. There are also some nice libraries which let you leverage an AJAX interface on the JSF framework, RichFaces is a popular one. Check out JavaServer Faces on sun.com and also JBoss RichFaces