Introduction to the

  1. Struts framework provides a Web program development method based on MVC architecture, which has the advantages of modular components, flexibility and reuse.
  2. Make the program structure based on MVC pattern more clear
  3. It also simplifies Web application development.

Simple use of the framework

  1. Download struts 2, website: struts.apache.org/download
  2. Setting up the Struts environment: Import the JAR package

Note: Not every Struts JAR downloaded is useful, and some of these jars contradict each other. I provide some jars here that I use

  1. After importing, add Struts2 MVC framework to the web. XML file to start configuration.
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     </filter-class> 
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/ *</url-pattern>
</filter-mapping>

Copy the code
  1. Create a Java class to test
package p1;
import com.opensymphony.xwork2.ActionSupport;
 public class HelloAction  extends ActionSupport{
 
    @Override
    public String execute(a){
 
        return "ok"; }}Copy the code
  1. Under the source package, create struts.xml

      
<! DOCTYPEstruts PUBLIC "- Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="user" namespace="/" extends="struts-default">
                  
                 <! Execute (); action (name); action (name);
                  
		<action name="Welcome" class="p1.HelloAction">
			<result name="ok">/newjsp.jsp</result>
		</action> 
                
	</package>
</struts> 
Copy the code
  1. Run the test

7. Note: In the struts. XML file, configure the parameters for Struts!!