The controller of Struts2 is a filter. The Action in Struts is equivalent to independent servlets in the basic MVC design pattern, and the Action calls the model layer (JavaBean) to complete specific business functions.
Create an example in struts2
Create a new WEB project, then right-click on the project and select Myeclipse>addscaptsCapabilities, select struts2 in the interface, and then click finish. After that, you will see the configuration file struts.xml of struts under the src file.
Then configure filters, structure struts.xml file and write action classes in the web.xml file.
The action class of Struts2 does not require any interface, as long as it is a normal class (POJO) containing the execute method. When the necessary interceptor is executed, the program will execute the execute method and action class:
public class HelloAction{private String name;private String password;public void setName(String name){this.name=name;}public void setPassword(String password){this.password=password;}public String execute() throws Exception{if("yang".equals(name) && "123456".equals(password)){return "success";} else{return "error";}}}When configuring Struts2, the Struts.xml configuration file created by right-clicking src will be automatically published to the WEB-INFI/classes directory. This is the file that Struts will load by default, and is used to configure the actions that need to be called by Struts2.
<!--Configuration package, you must inherit Struts-default--><struts> <package name="struts2" extends="struts-default"> <!--Configuration action, the name hello is used to access the URL: hello.action--> <action name="hello"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package></struts>
Finally, request http://localhost:10086/ZstrutsDemo/hello.action?name=yang&pass=1234 in the browser
You can see the page jump to the corresponding interface
Summarize
The above is the entire content of this article about the introduction to the initial introduction and code examples of struts2. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Struts2 development process and detailed configuration
Introduction to Struts2 intercepting string code
Detailed explanation of Struts2 OGNL expression example
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!