Grails 与其他 Java Web 框架相比有哪些优势?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/896320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:16:37  来源:igfitidea点击:

What are the Grails advantages over other Java Web Frameworks?

javagrailsjakarta-ee

提问by victor hugo

I've worked with JSF, Spring MVC and Struts and I think I got a good level on these frameworks. Recently I heard that many good developers I worked with are learning Grails and using it in their projects.

我曾使用过 JSF、Spring MVC 和 Struts,我认为我对这些框架有很好的了解。最近听说和我一起工作的很多优秀的开发人员都在学习 Grails 并在他们的项目中使用它。

What are the practical advantages of Grails over other frameworks? Is worth to learn it besides I know other frameworks? What is all the buzz around Grails, is it only because of Groovy?

与其他框架相比,Grails 的实际优势是什么?除了我了解其他框架外,还值得学习吗?围绕 Grails 的所有嗡嗡声是什么,仅仅是因为 Groovy 吗?

NOTE: I did research in SO and the only related question I found was thisand Grails is not mentioned

注意:我在 SO 中做了研究,我发现的唯一相关问题是这个,没有提到 Grails

回答by Jon

Grails is, like you say, built off Groovy which gives the immediate benefit of being more productive. The Groovy syntax is much terser than Java, it's much easier to do things in one line of Groovy code that would take you several in Java.

正如您所说,Grails 是建立在 Groovy 之上的,它提供了提高生产力的直接好处。Groovy 语法比 Java 简洁得多,在一行 Groovy 代码中完成的事情要容易得多,而在 Java 中却需要多行代码。

Grails specifically provides you with a number of advantages over other web frameworks (I can only talk for Struts 1.x, 2.x and SpringMVC however) - Grails is actually built on top of SpringMVC by the way and you can integrate other components using Spring.

Grails 专门为您提供了许多优于其他 Web 框架的优势(不过,我只能谈论 Struts 1.x、2.x 和 SpringMVC)——顺便说一下,Grails 实际上是建立在 SpringMVC 之上的,您可以使用春天。

  • Database Migrations and Versioning - no more application out of sync with database schema syndrome. Most Java web apps suffer from this.

  • Artefacts - which make creating new controllers and components easier. No more create a controller, configure it and stuff it into the right place in your web app. doh! Scaffolding also provides you with all some initial components to allow you to start building your pages and customising

  • Simpler validation (def simpler than Struts 1.x), e.g. username(size:6..10, blank:false)specifies two validation rules for a username field to be of a specific length and non blank. It's a bit harder in the other Java web app frameworks.

  • Built in Webflow (via Spring webflow) which makes creating complex workflows much simpler. Struts 2 can support Webflow, but via a plugin which is a little odd if I rememeber. Spring can too.

  • Interceptors - SpringMVC also has these.

  • Flash scope, see http://grails.org/doc/docs/1.1/ref/Controllers/flash.html

  • Better data binding - Struts 2 is pretty good, much better than Struts 1. SpringMVC is also good.

  • 数据库迁移和版本控制 - 不再有应用程序与数据库模式综合症不同步。大多数 Java Web 应用程序都受此影响。

  • Artefacts - 使创建新控制器和组件更容易。无需再创建控制器、配置它并将其填充到您的 Web 应用程序中的正确位置。哦!脚手架还为您提供了所有一些初始组件,以允许您开始构建页面和自定义

  • 更简单的验证(定义比 Struts 1.x 更简单),例如username(size:6..10, blank:false)为用户名字段指定两个验证规则,使其具有特定长度且非空白。在其他 Java Web 应用程序框架中,这有点困难。

  • 内置 Webflow(通过 Spring webflow),这使得创建复杂的工作流变得更加简单。Struts 2 可以支持 Webflow,但是通过一个插件,如果我记得的话,这有点奇怪。春天也可以。

  • 拦截器 - SpringMVC 也有这些。

  • Flash 范围,请参阅http://grails.org/doc/docs/1.1/ref/Controllers/flash.html

  • 更好的数据绑定——Struts 2 相当不错,比 Struts 1 好很多。SpringMVC 也不错。

There's a few more, check out the documentation for more details: http://grails.org/doc/1.1.1/

还有一些,查看文档了解更多详细信息:http: //grails.org/doc/1.1.1/

回答by Adeel Ansari

BTW, Grails is not considered as a Java Framework, its for Groovy. Groovy has the capability to work with Java seamlessly, but thats the different story.

顺便说一句,Grails 不被视为 Java 框架,而是用于 Groovy。Groovy 能够无缝地使用 Java,但那是另一回事。

Yes, its about Groovy, and like Java its awesomely good. Groovy follows the notion "Code less, do more", whereas Java is something like "Code more, do more". No offense to Java folks, I am one of you.

是的,它是关于 Groovy 的,就像 Java 一样,它非常好。Groovy 遵循“少写代码,多做事”的概念,而 Java 则类似于“多做代码,多做事”。无意冒犯 Java 人,我是你们中的一员。

You might want to read these:

你可能想阅读这些:

回答by Heinrich Filter

Grails adds Groovy goodness to existing Java frameworks and provides the tooling to use it easily:

Grails 为现有的 Java 框架添加了 Groovy 的优点,并提供了轻松使用它的工具:

  • Grails (web part) = Groovyfied Spring MVC(like @Jon points out)
  • GORM= Groovyfied Hibernate
  • GroovyTestCase = Groovyfied JUnit TestCase
  • Grails (web part) = Groovyfied Spring MVC(就像@Jon 指出的那样)
  • GORM= Groovyfied 休眠
  • GroovyTestCase = Groovyfied JUnit 测试用例

@Jon already mentioned some concrete practical advantages. I would like to point out two more fundamental concepts in the Grails approach:

@Jon 已经提到了一些具体的实际优势。我想指出 Grails 方法中两个更基本的概念:

  1. Convention over configuration. If you adhere to the conventions set out by Grails (which are very sensible btw) you can become productive very quickly.
  2. Don't Repeat Yourself (DRY)Grails enables you to write DRY code and in itself is a good example of how to be DRY. If you have any existing Java code reusing it in Grails should be no problem.
  1. 约定优于配置。如果您遵守 Grails 制定的约定(顺便说一句,这是非常明智的),您可以很快提高工作效率。
  2. 不要重复自己 (DRY)Grails 使您能够编写 DRY 代码,它本身就是一个很好的示例,说明如何做到 DRY。如果您有任何现有的 Java 代码,在 Grails 中重用它应该没有问题。

So,

所以,

Is it worth learning?

值得学习吗?

I think for you yes. Since you already know Java and some of the available frameworks (especially Spring MVC) the learning curve should not be steep at all.

我认为对你来说是的。由于您已经了解 Java 和一些可用的框架(尤其是 Spring MVC),因此学习曲线根本不应该很陡峭。

What is all the buzz around Grails, is it only because of Groovy?

围绕 Grails 的所有嗡嗡声是什么,仅仅是因为 Groovy 吗?

The buzz could be explained because Grails demonstrates that Groovy can be leveraged to create a very usable "product".

可以解释这种嗡嗡声是因为 Grails 表明可以利用 Groovy 创建一个非常有用的“产品”。