Struts2 basics (1)

Struts2
      1. MVC model:
            Model: Model JavaBean transfer data
            view: View html jsp display data
            Controller: Controller Servlet action Processing request

2.
      Struts2 struts2 is a popular and mature WEB application framework based on MVC design pattern.
      Role:
      replace servlet and reduce web development time

3. Entry case
      1. Create a WEB project
      2. Add dependency
      3. Create controller HelloAction.java
      4. Create struts.xml
      5. Create page hello.jsp
      6. Start struts2 in web.xml

The configuration file in this web mainly includes the configuration of the filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <!--过滤器-->
  <filter>
    <!--取名-->
    <filter-name>struts2</filter-name>
    <!--过滤器的全类限定名-->
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <!--对应的名称-->
    <filter-name>struts2</filter-name>
    <!--要拦截的目标-->
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Struts2 configuration file

<?xml  version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <!--
           package:对action进行分包
            name:给包取名字,区分多个包+
            namespace:action名称的前缀
            extends:继承  struts-default
    -->
    <package name="default" namespace="/" extends="struts-default">
        <!--
            配置action
                name  action的名称,请求的路径
                class:控制器的全类限定名
        -->
        <action name="hello" class="com.whpu.k16035.action.HelloAction">
            <!--
                返回值处理
                    name    匹配控制器的返回值
                    /jsp/hello.jsp  :跳转的路径
            -->
            <result name="success">/jsp/hello.jsp</result>
        </action>
    </package>

</struts>


Insert picture description here
The order of the elements executed in the web.xml of the entire strut2 execution process :
     1. Listener (listener) -->
     2. filter (filter) -->
     3. struts interceptor (not struts2) -->
     4 , Servlet server

Guess you like

Origin blog.csdn.net/qq_43479839/article/details/92990399