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