0

I need to convert a Struts app to Spring MVC using Spring Boot.

He want one application Core that can be called from two different Front Ends, one is with Angular and one with JSPs.

My idea is to build and app that retrieve data from a Soap service or a database with a central Controller (normal java class that expose methods), import the Jar of this App in two different Apps, one with a @RestController that communicate with Angular, and one with a @Controller that works with JSPs.

Now I know how to build a springboot app using rest services and Angular so that doesn't bother me. I'm worried about using Spring Boot with JSPs. I didn't know Struts and I've seen that it has a structure for create JSP pretty much handiling JSPs as layouts like "Header", "Footer" and ecc.. now in Angular I do the same thing with the components and is way more efficient, but JSPs are old and i found that very intresting.

My question is now what is the best way to handle the other Front End (the one with JSPs) in a pretty efficient way? I never used JSPs with Spring and I've understood the basic configuration doing some excercise with some easy "HelloWorld" apps.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • It's not super-clear what exactly you're asking. 1) Why would there need to be two different applications doing the same thing? 2) Presumably the data would be the same in either case--so there'd be business logic "layer(s)" and presentation layers. 3) The REST layer would serialize the business data to JSON (or whatever). 4) The JSP/web layer would expose the data via properties to the JSPs. – Dave Newton Apr 04 '22 at 19:13
  • Hi Dave, thanks for your answer. I haven't done it before. So it's possible to have 2 controller one Rest and one for the JSP in the same app? I thought they may have caused conflicts with Urls and whatever – Pandemonius Apr 04 '22 at 19:51
  • They'll only have conflicting URLs if you don't make them separate URLs. Alternatively the header could be used to determine the render (JSON or JSP) but I don't remember enough about Spring MVC to help there. – Dave Newton Apr 04 '22 at 19:52
  • Yeah, it's super unclear what frontend apps are you using. Whatever framework you doing on the frontend, you get a backend API, so you don't create it themselves. As far as you shouldn't use any MVC framework on the backend then your choice with the framework is yours. Read [this](https://stackoverflow.com/a/27172828/573032) answer to get learn to MVC. – Roman C Apr 05 '22 at 15:43
  • Hi Roman, thanks I know that wasn't that clear. I wasn't clear in my head as well. Actually Dave's answer helped me a lot. Basically now I'm building a central application that retrieve data from a Soap service and exposes this data through a Rest Api. This Rest Api can be called from Angular or from another app that uses SpringBoot with JSPs. My second question was how to handle JSPs structure in Springboot like Struts/Struts2. I have found that you can use Tiles.xml for configure the layouts in Springboot as well so I know how to handle properly everythigng now! Thanks to you al@DaveNewton – Pandemonius Apr 05 '22 at 16:13
  • @Pandemonius If you have to choose MVC framework for your frontend application which returns JSP for the view then you should read [this](https://stackoverflow.com/a/36998697/573032). I have also tiles examples [here](https://stackoverflow.com/a/32358332/573032). – Roman C Apr 06 '22 at 17:21
  • @RomanC Thanks Roman. As you get I've started with Angular, JSPs for me came later so I've still a lot to learn. Which solution do you propose as the most correct? I've learned all the Tiles structure and I find it quite close to what Angular do handling components. – Pandemonius Apr 07 '22 at 13:22
  • @Pandemonius As you started `"I need to convert a Struts app to Spring MVC using Spring Boot"`, I wonder why? If your application doesn't fulfill the customer needs, then it waste of time. If you don't know either Struts or Spring MVC, then you can develop Angular frontend without them. If you switch to the JSP then there's no way to return back, unless you design a wab application as MPA. – Roman C Apr 08 '22 at 16:02
  • @RomanC is more an experiment, of course the app is fine working with struts.. The thing is that the company that I work for wants to start to use newer technologies but with some clients they are chained to JSPs, so my boss asked me to develop a dynamic core app that can work with both Angular and Spring MVC – Pandemonius Apr 09 '22 at 17:36
  • @Pandemonius If the application works fine with Struts, so why do you want to convert it to Spring Boot? I have posted the answer below to make your choice between these frameworks. – Roman C Apr 10 '22 at 17:34

2 Answers2

0

The question arises, what to use Spring or Struts?

Struts, as I understand it, is a pure MVC Framework, and Spring MVC is just one of the Spring modules. I also heard that they use combinations of them. I must say right away that Struts 1.2 has not been developed for a long time and has been removed from the Apache site. Its successor was WebWork or Struts2 in the new name. As I understand it, if the name has changed, then the architecture has changed, which is very different for both frameworks.

Spring MVC is a web framework that is one of the Spring modules. It uses the MVC1 implementation as in Struts 1. The greatest benefit of this framework is that it easily integrates with other Spring modules that you need to develop your project.

Struts2 is a servlet-based web framework that implements MVC2. It can be extended with plugins and integrated into other frameworks, including Spring.

What is the difference, if not in architecture, then the first one, in my opinion, is simpler and requires less time to learn. The second one is much larger and more complex, requiring much more time to learn. It is clear that the time spent on studying then pays off. As for the speed of development, the second one, in my opinion, is developing much faster.

Apache Struts 2.5.30 GA has been released on 04 April 2022.

Both frameworks can be used both on the frontend and on the backend. If you have a question what to use, or what is better to use, then I will tell you right away that this is your choice, and not only yours. Many people use the first framework because it is part of Spring. If you want to get your simple project done quickly, then the first one is more suitable for you, but if you have more time and opportunity to spend on learning the second framework, then this may suit you. I generally can’t imagine the practical use of the second without integrating into Spring, but again, this is my personal opinion.

Roman C
  • 49,761
  • 33
  • 66
  • 176
0

To handle struts jsp, you need to convert all struts tags into compatible jstl or spring tags in that way you will be able to use it as view on your spring mvc.

Please refer to this post and also checkout the github code: https://www.shorterpost.com/2021/03/how-to-convert-struts-tags-to-jstl-spring.html

Attached excerpt from shorterpost:

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");
    }

Check out on GitHub for full code Click Here...

jjcosare
  • 1,463
  • 9
  • 9