Build a Struts2 development environment and complete a HelloWorld instance
Struts2 is a new MVC framework that is merged Based on Struts1 and WebWork technologies. It is very different from Struts1 and cannot be understood as an upgraded version of Struts1. It is based on Xwork, it can be understood as an upgraded version of WebWork.
First, import the jar package is required. For Struts2, at least five core packages need to be imported. The version is selected based on your actual situation:
You can download the jar package from the official website or add group 102992282 to the group file.
Configure the front-end controller filter in web. xml. The Code is as follows:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
Under src, create a configuration file named struts. xml and add the XML version information and DTD. The Code is as follows:
Then write our first Action. The Code is as follows:
Package hxl. insist; public class HelloDemo {public HelloDemo () {System. out. println (instantiate HelloDemo);} public String sayhi () {return hello ;}}
Note: The method is public, the return type is String, and the parameter list is empty.
Configure this Action in the struts. xml configuration file. The Code is as follows:
/WEB-INF/page/hello. jsp
Note: every time we access this URL, we can see that the console will print the instantiated HelloDemo, so we can know through this phenomenon that every time we access the Action, the web Container creates an Action object, so this Action is thread-safe, but may affect the program efficiency.