是否可以使用Web框架但不依赖于该框架?

时间:2022-06-09 08:22:32

I am investigating the use of web frameworks with my Java web-app. My basic requirements are pretty much easy maintainability, testability and no repetition.

我正在调查我的Java Web应用程序中Web框架的使用。我的基本要求是易于维护,可测试性和无重复。

I have explored writing my own MVC-type app using some sort of front controller pattern and JSP's for the views. The benefit of this is that I have complete control of all aspects of my web-app and if I design it properly it should not be hard to move it over to a more tested framework in the future if I so choose. However, the con is that I have to reinvent the wheel so to speak.

我已经探索过使用某种前端控制器模式和JSP来编写自己的MVC类应用程序。这样做的好处是我可以完全控制我的网络应用程序的所有方面,如果我正确地设计它,如果我这样选择,将来很难将它移动到更加经过测试的框架。然而,可以说,我必须重新发明*。

I hear good things about the currently available web frameworks. Some technologies that I have been looking at are Spring, Wicket, Struts, Guice, Hibernate and Tapestry.

我听到了有关当前可用的Web框架的好消息。我一直在关注的一些技术是Spring,Wicket,Struts,Guice,Hibernate和Tapestry。

I am a bit wary of Tapestry and Wicket. I dont know TOO much about them but they seem to deviate from the servlet->model jsp->view formula. I am not sure if I am comfortable with that. Although, I hear that Wicket is actually the best fit with Guice and is extremely testable.

我对Tapestry和Wicket有点警惕。我不太了解它们,但它们似乎偏离了servlet-> model jsp-> view formula。我不确定我是否对此感到满意。虽然,我听说Wicket实际上最适合Guice并且非常可测试。

Spring seems like it could be a nice fit, but I am very wary of frameworks which attempt to do everything. I would love to use spring-MVC, but can I swap in other components? Can I for example use Guice as my DI engine while using Spring-MVC as my framework?

Spring似乎很适合,但我非常警惕尝试做所有事情的框架。我很想使用spring-MVC,但是我可以交换其他组件吗?我可以使用Guice作为我的DI引擎,同时使用Spring-MVC作为我的框架吗?

I have briefly looked at Struts but it seems overly complex for my needs and again seems to be a complete package.

我简要地看了一下Struts,但它似乎对我的需求过于复杂,而且似乎又是一个完整的包。

I've never used Hibernate, but it seems to be the standard for ORM and if it's anything like ActiveRecord (which I have only been exposed to a little bit) I am sure it fits my needs.

我从来没有使用过Hibernate,但它似乎是ORM的标准,如果它像ActiveRecord(我只是暴露了一点点),我相信它符合我的需求。

I also have never really used Guice but people really seem to like it, and I am a fan of DI in general even though I am not sure how it is used in an actual application.

我也从未真正使用Guice,但人们似乎真的很喜欢它,我一般都是DI的粉丝,尽管我不确定它是如何在实际应用中使用的。

Basically, I am only really confident with writing Servlets / JSP's. I am not opposed to learning alternate technologies but I am looking for advice as to which ones would REALLY benefit me.

基本上,我对编写Servlets / JSP非常有信心。我不反对学习替代技术,但我正在寻找哪些建议真正让我受益的建议。

If I can make an MVC app using Servlets and JSP's is it worth it to incorporate Spring? Or should I just use Servlets / JSP's and incorporate a DI engine like Guice?

如果我可以使用Servlets和JSP创建一个MVC应用程序,那么合并Spring是否值得?或者我应该只使用Servlets / JSP并加入像Guice这样的DI引擎?

I am pretty sure I would like to use Hibernate for ORM, but I hear it can be pretty complex. All I am really looking for is a way to map my POJO's to a database, so if there is something better/easier to use I am willing to look it up.

我很确定我想将Hibernate用于ORM,但我听说它可能非常复杂。我真正想要的是将POJO映射到数据库的方法,所以如果有更好/更容易使用的东西,我愿意查阅。

I am feeling lost and am looking for a bit of direction from people knowledgable in the area, any opinions on any of these issues would be hugely appreciated. Thanks!

我感到迷茫,并且正在寻找该地区知识渊博的人的一些方向,对这些问题的任何意见都将非常感激。谢谢!

3 个解决方案

#1


"Spring seems like it could be a nice fit, but I am very wary of frameworks which attempt to do everything. I would love to use spring-MVC, but can I swap in other components? Can I for example use Guice as my DI engine while using Spring-MVC as my framework?"

“Spring似乎很合适,但我非常担心尝试做所有事情的框架。我很想使用spring-MVC,但是我可以交换其他组件吗?我可以使用Guice作为我的DI使用Spring-MVC作为我的框架引擎?“

Agreed Spring provides a lot of stuff, but it's totally modular. You can use DI with or without AOP and so forth. And yes you can use Spring MVC and Guice for DI together.

同意Spring提供了很多东西,但它完全是模块化的。您可以在有或没有AOP的情况下使用DI,依此类推。是的,你可以一起使用Spring MVC和Guice进行DI。

"I have briefly looked at Struts but it seems overly complex for my needs and again seems to be a complete package."

“我简要地看了一下Struts,但它似乎对我的需求过于复杂,而且似乎又是一个完整的包。”

I have used Struts for quite some time now, but even when I started using it, I found it easy as a breeze. The controller might seem overwhelming at first, but you will have real fun when you get the hang of it. The best way would be taking a look at some real world examples using Struts.

我已经使用Struts很长一段时间了,但即使我开始使用它,我发现它很容易轻松。一开始控制器可能看起来势不可挡,但是当你掌握它时,你将获得真正的乐趣。最好的方法是使用Struts查看一些真实世界的例子。

"I've never used Hibernate, but it seems to be the standard for ORM and if it's anything like ActiveRecord (which I have only been exposed to a little bit) I am sure it fits my needs."

“我从未使用过Hibernate,但它似乎是ORM的标准,如果它像ActiveRecord一样(我只是暴露了一点点),我相信它符合我的需求。”

Oh then if you found Struts to tough, Hibernate is huge. It requires a big learning curve. It pays at the end, but if you know ActiveRecord, I will suggest you to stick to it before you get a good amount of knowledge of Hibernate.

哦,如果你发现Struts很难,Hibernate是巨大的。它需要一个很大的学习曲线。它最终付出,但如果你知道ActiveRecord,我会建议你在获得Hibernate的大量知识之前坚持下去。

"I am pretty sure I would like to use Hibernate for ORM, but I hear it can be pretty complex."

“我很确定我想将Hibernate用于ORM,但我听说它可能非常复杂。”

IMHO, very true...at least for beginners. (Anyone suggesting a change here?)

恕我直言,非常真实......至少对于初学者来说。 (有人建议在这里改变吗?)

"If I can make an MVC app using Servlets and JSP's is it worth it to incorporate Spring?"

“如果我可以使用Servlets和JSP创建一个MVC应用程序,那么合并Spring是否值得?”

You mean without Struts or any other framework? How?

你的意思是没有Struts或任何其他框架?怎么样?

Seems like you are trying to take on too much too fast. Try considering one thing at a time. DI itself is a tricky thing to implement in real world. Oh yes conceptually it's great, but what I mean is you need to first get a hang of things one by one.

好像你试图承担得太快太多。尝试一次考虑一件事。 DI本身在现实世界中实施起来很棘手。哦,是的,概念上它很棒,但我的意思是你需要先逐一掌握一切。

#2


Very simply, if you are comfortable with JSPs and Servlets, then if you want to save some of the drudgery of web programming, I would look at Stripes or Struts 2.

很简单,如果你对JSP和Servlet感到满意,那么如果你想节省一些网络编程的苦差事,我会看看Stripes或Struts 2。

I am very familiar with Stripes, and only am aware that Struts 2 is similar, so I will focus this entry on Stripes.

我对Stripes非常熟悉,只知道Struts 2是类似的,所以我将把这个条目集中在Stripes上。

As an aside, Struts 1 is worthless. It offers no value (frankly).

另外,Struts 1毫无价值。它没有价值(坦率地说)。

Stripes has several features, but I will focus on only a few.

Stripes有几个功能,但我只关注几个。

The primary value of Stripes, and if this were it's only feature it would still be very valuable, is its binding framework.

Stripes的主要价值,如果这是它唯一的功能,它仍然是非常有价值的,它的绑定框架。

Binding is the process of converting the requests string values in to the actions values. Stripes does this amazingly well. Specifically, Stripes binding does very well on nested and indexed parameters, as well as type conversions. You can easily have a form field named "currentDate" and then have a "Date currentDate" in your Action, and Stripes will "do the right thing".

绑定是将请求字符串值转换为操作值的过程。条纹做得非常好。具体来说,Stripes绑定在嵌套和索引参数以及类型转换方面做得很好。您可以轻松地拥有一个名为“currentDate”的表单字段,然后在Action中使用“Date currentDate”,Stripes将“做正确的事情”。

If you have a form field named "mainMap['bob'].listOfThings[3].customer.birthDate", Stripes will make the map, create the list, create the customer, convert the string to a date, populate the birthDate, put the customer in the 3 slot of the list, and put that list in the 'bob' spot of the map. (I do stuff like this all the time.)

如果你有一个名为“mainMap ['bob']。listOfThings [3] .customer.birthDate”的表单字段,Stripes将制作地图,创建列表,创建客户,将字符串转换为日期,填充birthDate,将客户放在列表的3个插槽中,并将该列表放在地图的“bob”位置。 (我一直这样做。)

The binding of requests to Action variables is just wonderful.

请求与Action变量的绑定非常棒。

On top of that you get, if you use their form tags, you get nice behaviors when, for example, they put "Fred" in your date field. You easily get the form back, with Fred in the field, and a nice error message.

最重要的是,如果你使用他们的表单标签,你会得到很好的行为,例如,他们把“Fred”放在你的日期字段中。你很容易得到表格,Fred在现场,并有一个很好的错误信息。

Finally, I really like their Resolutions as a result from their Actions. For example, a ForwardResolution to forward to a page, RedirectResolution to redirect to a page, StreamingResolution if you want to pump data down the socket, etc. It's a very elegant feature.

最后,我真的很喜欢他们的决议,因为他们的行动。例如,一个ForwardResolution转发到一个页面,RedirectResolution转发到一个页面,StreamingResolution如果你想在套接字中抽取数据,等等。这是一个非常优雅的功能。

Stripes has all sorts of power and does all sorts of things, but those 3 pieces are what make it best for me, and what I use 99% of the time.

条纹具有各种各样的力量并且可以做各种各样的事情,但是这3件对我来说是最好的,而且我99%的时间使用它。

Simply, it really stays out of the way and readily handles the "plumbing" without completely obscuring the HTTP request nature of the system.

简单地说,它确实不受影响,并且很容易处理“管道”而不会完全模糊系统的HTTP请求性质。

For someone who is content with JSP/Servlets, Stripes I think is an excellent step up as it adds good, solid value with very little cost (it's simple to set up) and without having to toss out everything you already know, since it works just great with JSPs and JSTL. Learn the simple mechanism it uses to map Actions to URLs, and how simple it is to map requests to your actions, and you'll be flying in no time.

对于那些满足于JSP / Servlets的人来说,Stripes我认为这是一个很好的进步,因为它以极低的成本(设置简单)增加了良好,可靠的价值,并且不必丢弃你已经知道的所有内容,因为它有效使用JSP和JSTL非常棒。了解它用于将操作映射到URL的简单机制,以及将请求映射到您的操作的简单方法,您将立即飞行。

Works great with Ajax and the like as well.

适用于Ajax等。

#3


The question illustrates some confusion. I think the definitive answer is "no, it is not possible to use a web framework but not be dependent on it".

这个问题说明了一些困惑。我认为最终的答案是“不,不可能使用Web框架但不依赖于它”。

But your instinct is good. You want to maximize the general benefit a framework provides by helping to properly layer and modularize your code and minimize its invasiveness.

但你的直觉很好。您希望通过帮助正确分层和模块化代码并最小化其侵入性来最大化框架提供的一般好处。

With that said, I think Spring is the winner on both counts.

话虽如此,我认为Spring在这两方面都是胜利者。

If you follow the Spring idiom, the structure of your code will be better by the use of interfaces, layering, and aspects. Some of the attention they pay to design is bound to rub off on you. That's as helpful as the good plumbing code they provide.

如果你遵循Spring惯用法,那么通过使用接口,分层和方面,代码的结构会更好。他们为设计付出的一些注意力必然会影响到你。这与他们提供的良好管道代码一样有用。

Your code base does not have to be 100% Spring. I've seen Spring used in enhancements to legacy Java apps that weren't rewritten from front to back.

您的代码库不必是100%Spring。我已经看到Spring用于对旧版Java应用程序的增强,这些应用程序不是从前到后重写的。

Struts tends to not be a good choice because it is JUST a web framework. It encourages you to put all your processing in Action subclasses, never to come out. Spring injects the idea of a service interface that decouples the web tier from the back end. It's easier to swap out web tiers and expose the service as SOAP, RMI, EJB, or remote HTTP call.

Struts往往不是一个好的选择,因为它只是一个Web框架。它鼓励您将所有处理放在Action子类中,永远不要出来。 Spring注入了一个服务接口的概念,它将Web层与后端分离。更换Web层并将服务公开为SOAP,RMI,EJB或远程HTTP调用更容易。

Hibernate is far more complex than Struts. If you choose Spring, use persistence interfaces and start with Spring JDBC. When you're ready for Hibernate, you can always write a new implementation and simply inject it into the place where your JDBC version used to be.

Hibernate比Struts复杂得多。如果选择Spring,请使用持久性接口并从Spring JDBC开始。当你准备好Hibernate时,你总是可以编写一个新的实现,只需将它注入到你的JDBC版本所在的位置即可。

#1


"Spring seems like it could be a nice fit, but I am very wary of frameworks which attempt to do everything. I would love to use spring-MVC, but can I swap in other components? Can I for example use Guice as my DI engine while using Spring-MVC as my framework?"

“Spring似乎很合适,但我非常担心尝试做所有事情的框架。我很想使用spring-MVC,但是我可以交换其他组件吗?我可以使用Guice作为我的DI使用Spring-MVC作为我的框架引擎?“

Agreed Spring provides a lot of stuff, but it's totally modular. You can use DI with or without AOP and so forth. And yes you can use Spring MVC and Guice for DI together.

同意Spring提供了很多东西,但它完全是模块化的。您可以在有或没有AOP的情况下使用DI,依此类推。是的,你可以一起使用Spring MVC和Guice进行DI。

"I have briefly looked at Struts but it seems overly complex for my needs and again seems to be a complete package."

“我简要地看了一下Struts,但它似乎对我的需求过于复杂,而且似乎又是一个完整的包。”

I have used Struts for quite some time now, but even when I started using it, I found it easy as a breeze. The controller might seem overwhelming at first, but you will have real fun when you get the hang of it. The best way would be taking a look at some real world examples using Struts.

我已经使用Struts很长一段时间了,但即使我开始使用它,我发现它很容易轻松。一开始控制器可能看起来势不可挡,但是当你掌握它时,你将获得真正的乐趣。最好的方法是使用Struts查看一些真实世界的例子。

"I've never used Hibernate, but it seems to be the standard for ORM and if it's anything like ActiveRecord (which I have only been exposed to a little bit) I am sure it fits my needs."

“我从未使用过Hibernate,但它似乎是ORM的标准,如果它像ActiveRecord一样(我只是暴露了一点点),我相信它符合我的需求。”

Oh then if you found Struts to tough, Hibernate is huge. It requires a big learning curve. It pays at the end, but if you know ActiveRecord, I will suggest you to stick to it before you get a good amount of knowledge of Hibernate.

哦,如果你发现Struts很难,Hibernate是巨大的。它需要一个很大的学习曲线。它最终付出,但如果你知道ActiveRecord,我会建议你在获得Hibernate的大量知识之前坚持下去。

"I am pretty sure I would like to use Hibernate for ORM, but I hear it can be pretty complex."

“我很确定我想将Hibernate用于ORM,但我听说它可能非常复杂。”

IMHO, very true...at least for beginners. (Anyone suggesting a change here?)

恕我直言,非常真实......至少对于初学者来说。 (有人建议在这里改变吗?)

"If I can make an MVC app using Servlets and JSP's is it worth it to incorporate Spring?"

“如果我可以使用Servlets和JSP创建一个MVC应用程序,那么合并Spring是否值得?”

You mean without Struts or any other framework? How?

你的意思是没有Struts或任何其他框架?怎么样?

Seems like you are trying to take on too much too fast. Try considering one thing at a time. DI itself is a tricky thing to implement in real world. Oh yes conceptually it's great, but what I mean is you need to first get a hang of things one by one.

好像你试图承担得太快太多。尝试一次考虑一件事。 DI本身在现实世界中实施起来很棘手。哦,是的,概念上它很棒,但我的意思是你需要先逐一掌握一切。

#2


Very simply, if you are comfortable with JSPs and Servlets, then if you want to save some of the drudgery of web programming, I would look at Stripes or Struts 2.

很简单,如果你对JSP和Servlet感到满意,那么如果你想节省一些网络编程的苦差事,我会看看Stripes或Struts 2。

I am very familiar with Stripes, and only am aware that Struts 2 is similar, so I will focus this entry on Stripes.

我对Stripes非常熟悉,只知道Struts 2是类似的,所以我将把这个条目集中在Stripes上。

As an aside, Struts 1 is worthless. It offers no value (frankly).

另外,Struts 1毫无价值。它没有价值(坦率地说)。

Stripes has several features, but I will focus on only a few.

Stripes有几个功能,但我只关注几个。

The primary value of Stripes, and if this were it's only feature it would still be very valuable, is its binding framework.

Stripes的主要价值,如果这是它唯一的功能,它仍然是非常有价值的,它的绑定框架。

Binding is the process of converting the requests string values in to the actions values. Stripes does this amazingly well. Specifically, Stripes binding does very well on nested and indexed parameters, as well as type conversions. You can easily have a form field named "currentDate" and then have a "Date currentDate" in your Action, and Stripes will "do the right thing".

绑定是将请求字符串值转换为操作值的过程。条纹做得非常好。具体来说,Stripes绑定在嵌套和索引参数以及类型转换方面做得很好。您可以轻松地拥有一个名为“currentDate”的表单字段,然后在Action中使用“Date currentDate”,Stripes将“做正确的事情”。

If you have a form field named "mainMap['bob'].listOfThings[3].customer.birthDate", Stripes will make the map, create the list, create the customer, convert the string to a date, populate the birthDate, put the customer in the 3 slot of the list, and put that list in the 'bob' spot of the map. (I do stuff like this all the time.)

如果你有一个名为“mainMap ['bob']。listOfThings [3] .customer.birthDate”的表单字段,Stripes将制作地图,创建列表,创建客户,将字符串转换为日期,填充birthDate,将客户放在列表的3个插槽中,并将该列表放在地图的“bob”位置。 (我一直这样做。)

The binding of requests to Action variables is just wonderful.

请求与Action变量的绑定非常棒。

On top of that you get, if you use their form tags, you get nice behaviors when, for example, they put "Fred" in your date field. You easily get the form back, with Fred in the field, and a nice error message.

最重要的是,如果你使用他们的表单标签,你会得到很好的行为,例如,他们把“Fred”放在你的日期字段中。你很容易得到表格,Fred在现场,并有一个很好的错误信息。

Finally, I really like their Resolutions as a result from their Actions. For example, a ForwardResolution to forward to a page, RedirectResolution to redirect to a page, StreamingResolution if you want to pump data down the socket, etc. It's a very elegant feature.

最后,我真的很喜欢他们的决议,因为他们的行动。例如,一个ForwardResolution转发到一个页面,RedirectResolution转发到一个页面,StreamingResolution如果你想在套接字中抽取数据,等等。这是一个非常优雅的功能。

Stripes has all sorts of power and does all sorts of things, but those 3 pieces are what make it best for me, and what I use 99% of the time.

条纹具有各种各样的力量并且可以做各种各样的事情,但是这3件对我来说是最好的,而且我99%的时间使用它。

Simply, it really stays out of the way and readily handles the "plumbing" without completely obscuring the HTTP request nature of the system.

简单地说,它确实不受影响,并且很容易处理“管道”而不会完全模糊系统的HTTP请求性质。

For someone who is content with JSP/Servlets, Stripes I think is an excellent step up as it adds good, solid value with very little cost (it's simple to set up) and without having to toss out everything you already know, since it works just great with JSPs and JSTL. Learn the simple mechanism it uses to map Actions to URLs, and how simple it is to map requests to your actions, and you'll be flying in no time.

对于那些满足于JSP / Servlets的人来说,Stripes我认为这是一个很好的进步,因为它以极低的成本(设置简单)增加了良好,可靠的价值,并且不必丢弃你已经知道的所有内容,因为它有效使用JSP和JSTL非常棒。了解它用于将操作映射到URL的简单机制,以及将请求映射到您的操作的简单方法,您将立即飞行。

Works great with Ajax and the like as well.

适用于Ajax等。

#3


The question illustrates some confusion. I think the definitive answer is "no, it is not possible to use a web framework but not be dependent on it".

这个问题说明了一些困惑。我认为最终的答案是“不,不可能使用Web框架但不依赖于它”。

But your instinct is good. You want to maximize the general benefit a framework provides by helping to properly layer and modularize your code and minimize its invasiveness.

但你的直觉很好。您希望通过帮助正确分层和模块化代码并最小化其侵入性来最大化框架提供的一般好处。

With that said, I think Spring is the winner on both counts.

话虽如此,我认为Spring在这两方面都是胜利者。

If you follow the Spring idiom, the structure of your code will be better by the use of interfaces, layering, and aspects. Some of the attention they pay to design is bound to rub off on you. That's as helpful as the good plumbing code they provide.

如果你遵循Spring惯用法,那么通过使用接口,分层和方面,代码的结构会更好。他们为设计付出的一些注意力必然会影响到你。这与他们提供的良好管道代码一样有用。

Your code base does not have to be 100% Spring. I've seen Spring used in enhancements to legacy Java apps that weren't rewritten from front to back.

您的代码库不必是100%Spring。我已经看到Spring用于对旧版Java应用程序的增强,这些应用程序不是从前到后重写的。

Struts tends to not be a good choice because it is JUST a web framework. It encourages you to put all your processing in Action subclasses, never to come out. Spring injects the idea of a service interface that decouples the web tier from the back end. It's easier to swap out web tiers and expose the service as SOAP, RMI, EJB, or remote HTTP call.

Struts往往不是一个好的选择,因为它只是一个Web框架。它鼓励您将所有处理放在Action子类中,永远不要出来。 Spring注入了一个服务接口的概念,它将Web层与后端分离。更换Web层并将服务公开为SOAP,RMI,EJB或远程HTTP调用更容易。

Hibernate is far more complex than Struts. If you choose Spring, use persistence interfaces and start with Spring JDBC. When you're ready for Hibernate, you can always write a new implementation and simply inject it into the place where your JDBC version used to be.

Hibernate比Struts复杂得多。如果选择Spring,请使用持久性接口并从Spring JDBC开始。当你准备好Hibernate时,你总是可以编写一个新的实现,只需将它注入到你的JDBC版本所在的位置即可。