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

发布于 2024-07-18 04:36:24 字数 1141 浏览 7 评论 0原文

我正在研究 Web 框架在我的 Java Web 应用程序中的使用。 我的基本要求是非常容易维护、可测试并且不重复。

我尝试过使用某种前端控制器模式和 JSP 视图来编写自己的 MVC 类型应用程序。 这样做的好处是,我可以完全控制我的网络应用程序的各个方面,如果我设计得当,如果我选择的话,将来将其转移到经过更多测试的框架应该不难。 然而,缺点是我必须重新发明轮子。

我听说了有关当前可用的 Web 框架的一些好消息。 我一直在研究的一些技术有 Spring、Wicket、Struts、Guice、Hibernate 和 Tapestry。

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

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

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

我从未使用过 Hibernate,但它似乎是 ORM 的标准,如果它类似于 ActiveRecord(我只接触过一点),我确信它符合我的需求。

我也从未真正使用过 Guice,但人们似乎真的很喜欢它,而且我总体上是 DI 的粉丝,尽管我不确定它在实际应用程序中如何使用。

基本上,我只对编写 Servlet/JSP 真正有信心。 我并不反对学习替代技术,但我正在寻求关于哪些技术真正对我有益的建议。

如果我可以使用 Servlet 和 JSP 制作 MVC 应用程序,那么合并 Spring 是否值得? 或者我应该只使用 Servlet / JSP 并合并像 Guice 这样的 DI 引擎?

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

我感到迷失,正在寻找该领域知识渊博的人的一些指导,对这些问题的任何意见将不胜感激。 谢谢!

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.

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.

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.

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.

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?

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

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.

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.

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.

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?

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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

婴鹅 2024-07-25 04:36:25

很简单,如果您熟悉 JSP 和 Servlet,那么如果您想省去一些 Web 编程的苦差事,我会考虑 Stripes 或 Struts 2。

我对 Stripes 非常熟悉,只知道 Struts 2 是类似,所以我将把这篇文章的重点放在 Stripes 上。

顺便说一句,Struts 1 毫无价值。 它没有提供任何价值(坦率地说)。

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

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

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

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

将请求绑定到 Action 变量真是太棒了。

最重要的是,如果您使用他们的表单标签,例如,当他们在您的日期字段中输入“Fred”时,您会得到很好的行为。 Fred 就在现场,您可以轻松取回表格,并收到一条不错的错误消息。

最后,我真的很喜欢他们的行动所带来的决心。 例如,ForwardResolution 用于转发到页面,RedirectResolution 用于重定向到页面,StreamingResolution 如果您想将数据泵入套接字等。这是一个非常优雅的功能。

Stripes 具有各种各样的功能,可以做各种各样的事情,但是这 3 部分最适合我,也是我 99% 的时间都在使用的东西。

简而言之,它确实不妨碍并轻松处理“管道”,而不会完全掩盖系统的 HTTP 请求性质。

对于那些对 JSP/Servlet 感到满意的人来说,我认为 Stripes 是一个很好的进步,因为它以很少的成本增加了良好的、可靠的价值(设置很简单),而且不必扔掉你已经知道的一切,因为它有效非常适合 JSP 和 JSTL。 了解它用于将操作映射到 URL 的简单机制,以及将请求映射到您的操作是多么简单,您很快就会飞起来。

与 Ajax 等也能很好地配合。

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.

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

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

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

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

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".

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.)

The binding of requests to Action variables is just wonderful.

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.

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.

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.

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

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.

Works great with Ajax and the like as well.

比忠 2024-07-25 04:36:25

这个问题说明了一些混乱。 我认为明确的答案是“不,不可能使用 Web 框架而不依赖它”。

但你的直觉是好的。 您希望通过帮助正确分层和模块化代码并最大程度地减少其侵入性来最大化框架提供的总体优势。

话虽如此,我认为 Spring 在这两方面都是赢家。

如果您遵循 Spring 惯用法,通过使用接口、分层和方面,您的代码结构将会更好。 他们对设计的一些关注肯定会影响到你。 这与他们提供的良好管道代码一样有用。

您的代码库不必是 100% Spring。 我见过 Spring 用于增强遗留 Java 应用程序,但这些应用程序没有从前到后重写。

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

Hibernate 比 Struts 复杂得多。 如果您选择 Spring,请使用持久化接口并从 Spring JDBC 开始。 当您准备好使用 Hibernate 时,您始终可以编写一个新的实现,并将其简单地注入到您的 JDBC 版本原来所在的位置。

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".

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.

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.

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.

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.

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.

怎樣才叫好 2024-07-25 04:36:24

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

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

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

我已经使用 Struts 有一段时间了,但即使当我开始使用它时,我也发现它非常简单。 控制器一开始可能看起来让人不知所措,但当你掌握了它的窍门后,你会真正享受到乐趣。 最好的方法是查看一些使用 Struts 的现实示例。

“我从未使用过 Hibernate,但它似乎是 ORM 的标准,如果它类似于 ActiveRecord(我只接触过一点),我确信它符合我的需求。”

哦,如果您发现 Struts 很难,那么 Hibernate 就很强大了。 它需要很大的学习曲线。 它最终会带来回报,但如果您了解 ActiveRecord,我建议您在获得大量 Hibernate 知识之前坚持使用它。

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

恕我直言,非常正确......至少对于初学者来说。 (有人建议在这里进行更改吗?)

“如果我可以使用 Servlet 和 JSP 制作 MVC 应用程序,那么是否值得合并 Spring?”

您的意思是没有 Struts 或任何其他框架? 如何?

看起来你试图承担太多太快的事情。 尝试一次考虑一件事。 在现实世界中实施 DI 本身是一件棘手的事情。 哦,是的,从概念上讲它很棒,但我的意思是你需要首先一件一件地掌握窍门。

"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?"

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.

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

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.

"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."

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.

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

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?"

You mean without Struts or any other framework? How?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文