Struts to Spring MVC using Sping Boot



我需要使用Spring Boot将Struts应用程序转换为Spring MVC。

他想要一个可以从两个不同前端调用的应用程序Core,一个是Angular,另一个是JSP。

我的想法是构建一个从Soap服务或具有中央控制器的数据库中检索数据的应用程序(公开方法的普通java类(,在两个不同的应用程序中导入该应用程序的Jar,一个具有与Angular通信的@RestController,另一个具有可与JSP工作的@Controller

现在我知道了如何使用rest服务和Angular构建一个springboot应用程序,所以这不会困扰我。我担心将springboot与JSP一起使用。我不知道Struts,我看到它有一个创建JSP的结构,几乎可以将JSP作为"Header""Footer"和ecc等布局。。现在在Angular中,我对组件做了同样的事情,而且效率更高,但JSP很旧,我发现这很有趣。

我现在的问题是,以非常有效的方式处理另一个前端(带有JSP的前端(的最佳方式是什么?我从未在Spring中使用过JSP,我已经了解了使用一些简单的"HelloWorld"应用程序进行一些练习的基本配置。

问题来了,使用Spring还是Struts?

Struts,正如我所理解的,是一个纯粹的MVC框架,而SpringMVC只是Spring模块之一。我还听说他们使用它们的组合。我必须马上说Struts1.2已经很久没有开发了,并且已经从Apache站点中删除了。它的继任者是新名称中的WebWork或Struts2。据我所知,如果名称发生了变化,那么体系结构也发生了变化。这对两个框架来说都是非常不同的。

Spring MVC是一个web框架,是Spring模块之一。它使用Struts1中的MVC1实现。这个框架的最大好处是,它可以轻松地与开发项目所需的其他Spring模块集成。

Struts2是一个基于servlet的web框架,用于实现MVC2。它可以通过插件进行扩展,并集成到其他框架中,包括Spring。

区别是什么,如果不是在建筑方面,那么在我看来,第一个更简单,需要更少的时间来学习。第二个更大更复杂,需要更多的时间来学习。很明显,花在学习上的时间是有回报的。至于发展速度,我认为第二个发展得更快。

Apache Struts 2.5.30 GA已经发布2022年4月4日。

这两个框架都可以在前端和后端使用。如果你有问题要用什么,或者用什么更好,那么我会马上告诉你,这是你的选择,而不仅仅是你的。许多人使用第一个框架是因为它是Spring的一部分。如果你想快速完成你的简单项目,那么第一个更适合你,但如果你有更多的时间和机会学习第二个框架,那么这可能适合你。如果不融入Spring,我通常无法想象第二个的实际用途,但这也是我个人的看法。

要处理struts-jsp,您需要将所有struts标记转换为兼容的jstl或spring标记,这样您就可以将其用作springmvc上的视图。

请参阅这篇文章,并检查github代码:https://www.shorterpost.com/2021/03/how-to-convert-struts-tags-to-jstl-spring.html

附件摘录自短文:

Here is an example to convert struts tags to jstl and spring tags.Here is an list of struts tags and equivalent tags in JSTL and Spring.
1.Tags:
1.html
html:select     form:select
html:option     form:option
html:options    form:options
html:hidden     form:hidden
html:checkbox   form:checkbox
html:multibox   form:checkboxes
html:text       form:input
html:textarea   form:textarea
html:form       form:form
html:errors     form:errors
html:radio      form:radiobutton
2.bean
bean:write      c:out
bean:define     c:set
bean:message    spring:message
3.logic
logic:present       c:if
logic:notPresent    c:if
logic:iterate       c:forEach
logic:equal         c:if (test eq)
logic:notEqual      c:if (test ne)
logic:empty         c:if (test empty)
logic:notEmpty      c:if (test not empty)
But the problem is we have to change manually in JSP files, so i created an tool which uses jsoup and Apache commons to automatically process and overwrite tags.This tool reads all tags in JSP files and process overwriting to JSTL or Spring tags.Below is an sample code to process using java.

private static void processHtmlHiddenTag(Element element)
{
String name = element.attr("name");
String property = element.attr("property");
if(!isEmptyOrNull(name) || isEmptyOrNull(property))
return;
element.tagName("form:hidden");
element.attr("path",property);
element.removeAttr("property");
}

查看GitHub上的完整代码点击这里。。。

最新更新