Transition from Struts2 to Spring MVC (Controller)

Prerequisites for migrating from Struts 2 to Spring MVC

The simplest procedure is

Other replacements include Validation, View, and Interceptor, but they can be modules that are independent of Struts2, so they can be replaced sequentially. This time is the Controller edition.

Action class assumptions

The Action class of Struts2 is supposed to be described by annotation using ** Convention plug-in **. If you have not installed the Convention plugin, first apply the Convention plugin and add Struts2 annotations to the Action class.

Struts2 Official: Convention Plugin: https://struts.apache.org/docs/convention-plugin.html Qiita: Write Struts2 Action class based on annotation (2016 Spring Ver.) Http://qiita.com/alpha_pz/items/4a97df916102dad2e2bc

Action class after applying the Convention

The implementation of Action class is as follows

Just keep it.

DisplayListAction.java


// Struts2-Convention
@Namespace("/")        // (1)
@ParentPackage("struts-thymeleaf")
@Results({@Result(name=ActionSupport.SUCCESS,type="thymeleaf-spring",location="list")})        // (3)
// Spring framework
@Controller
@Scope("prototype")
public class DisplayListAction extends ActionSupport {

    @Action("list") // Struts2-Convention        // (2)
    public String execute() throws Exception {
        products = service.search(param);
        return SUCCESS;
    }

    @Autowired // Spring framework
    ProductService service;

    @Setter        // (4)
    private String param;

    @Getter @Setter // Lombok        // (5)
    List<SampleProduct> products;
}

If you narrow down to this point, you can replace it with the Spring MVC Controller.

To Controller of Spring MVC

DisplayListController.java


@Controller
@RequestMapping("/")        // (1)
public class DisplayListController {

    @GetMapping("list")        // (2)
    public ModelAndView index(@RequestParam String param, ModelAndView mnv) {        // (4)
        List<SampleProduct> products = service.search(param);

        mnv.addObject("products", products);        // (5)
        mnv.setViewName("/list");        // (3)

        return mnv;
    }
}

This allows for one-to-one mapping.

# Settings Struts2 Convention Spring MVC
1 path @Namespace @RequestMappings
2 Control path @Action @GetMapping,@Post Mapping etc.
3 View View @Results,@Result Return the view name or View name of ModelAndView alone
4 Parameters to receive Member variable + mutator method(※1) Method arguments
5 Content to respond Member variable + accessor method(※2) Method return value

The important thing to migrate is to bring the Controller-Service relationship to the Action-Service relationship following Spring MVC, and to have the request / response object handled in the Action class field in the Controller method. Is to go.

Recommended Posts

Transition from Struts2 to Spring MVC (Controller)
Try Spring Boot from 0 to 100.
Upgrade spring boot from 1.5 series to 2.0 series
[Java Spring MVC] Controller for development confirmation
Story when moving from Spring Boot 1.5 to 2.1
[Rails] Assigning variables from controller to JavaScript
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
[Spring MVC] How to pass path variables
Major changes related to Spring Framework 5.0 Web MVC
[Reverse lookup] Spring Security (updated from time to time)
Test controller with Mock MVC in Spring Boot
Transition to a view controller with Swift WebKit
Touch all Spring "Guides" (updated from time to time)
Pass Form class from Controller to Thymeleaf by Spring-Boot
I tried to implement file upload with Spring MVC
How to use Struts2 * Spring Framework (Spring plugin) June 2017 Version
To connect from Spring to MySQL on virtual server (unsolved)
Push delivery from Spring application to Firebase Cloud Messaging
1. Start Spring framework from 1
Steps to deploy Struts 2.5.8
From Java to Ruby !!
How to transition from the [Swift5] app to the iPhone settings screen
To receive an empty request with Spring Web MVC @RequestBody