Preliminary understanding of struts2

Struts2 is a Web application framework based on MVC design pattern, which corresponds essentially a servlet, the MVC design pattern, Struts2 as a controller (Controller) to establish interaction model with a data view.

A, Struts2 built environment

1. Struts introduced depends on project pom.xml maven

<dependency>
2             <groupId>org.apache.struts</groupId>
3             <artifactId>struts2-core</artifactId>
4             <version>2.5.13</version>
5      </dependency>

2. Import the required Struts configuration file

struts-base.xml

<? xml Version = "1.0" encoding = "UTF-8"?> 
<! DOCTYPE Struts the PUBLIC
     "- // the Apache Software Foundation // DTD Struts the Configuration 2.5 // EN" 
    "http://struts.apache.org/ DTDs / Struts-2.5.dtd "> 
<Struts> 
    <-! set coding format -> 
    <name = Constant" struts.i18n.encoding "value =" UTF-. 8 "/> 
    <-! enable dynamic method invocation -> 
    <Constant name = "struts.devMode" value = "to true" /> 
    <-! modify the code can take effect immediately without restarting the project -> 
    <Constant name = "struts.configuration.xml. reload "value =" to true "/> 
    <name = Constant" struts.i18n.reload "value =" to true "/> 
    <!- Enable dynamic method invocation -> 
    <Constant name = "struts.enable.DynamicMethodInvocation" value = "to true" /> 

    <-! After struts2.5 add this configuration to enable dynamic method invocation -> 
    < Package name="base" extends="struts-default" abstract="true">
        <global-allowed-methods>regex:.*</global-allowed-methods>
    </package>
</struts>

struts sy.xml

<?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>
    <!-- /user/bookAction?methodName=list 
        /sy/bookAction?methodName=list
    -->
    <package name="sy" extends="base" namespace="/sy">
        <action name="/demo_*" class="com.liuwenwu.web.HelloAction" method="{1}">
            <result name="rs">/rs.jsp</result>
        </action>
    </package>
</struts>

struts.xml

<?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>
    <!-- struts默认的配置文件 -->
    <include file="struts-default.xml"></include>
    <!-- struts基础配置文件 -->
    <include file="struts-base.xml"></include>
    
    <include file="struts-sy.xml"></include>
</struts>

3. The filter core disposed web.xml

<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_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>
      <filter>
          <filter-name>struts</filter-name>
          <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>struts</filter-name>
          <url-pattern>*.action</url-pattern>
      </filter-mapping>
</web-app>

Second, dynamic method invocation

action Code

   public String add() {
        System.out.println("调用add方法");
        return "rs";
        
    }
    
    public String del() {
        System.out.println("调用del方法");
        return "rs";
        
    }

Js page code if the test is successful

<a href="${pageContext.request.contextPath }/sy/demo_add.action">新增</a>

<a href="${pageContext.request.contextPath }/sy/demo_del.action">删除</a>

The console has a corresponding output is a success

Three, struts front desk to pass parameters to the background

three methods:

ModelDrivern 1.implements
      2. class instance attribute name
      3.set / get

Cal first create an entity class

package com.liuwenwu.entity;

public class Cal {
    
    private String num1;
    private String num2;
    public String getNum1() {
        return num1;
    }
    public void setNum1(String num1) {
        this.num1 = num1;
    }
    public String getNum2() {
        return num2;
    }
    public void setNum2(String num2) {
        this.num2 = num2;
    }
    @Override
    public String toString() {
        return "Cal [num1=" + num1 + ", num2=" + num2 + "]";
    }
    

}

1. To achieve modelDrivern

public class HelloAction implements ModelDriven<Cal>,ServletRequestAware{
        private Cal cal1=new Cal();    

    @Override
    public Cal getModel() {
        return cal1;
    }

        /**
     * implements modelDrivern    接收参数值
     * @return
     */
    public String accept1() {
        System.out.println("cal1:"+cal1);

        return "rs";    
    }

}

jsp page code

<a href="${pageContext.request.contextPath }/sy/demo_accept1.action?num1=20&&num2=5">accept1</a>

2. instance of the class attribute name

    Private Cal CAL2; 

    public () {Cal getCal2
         return ; CAL2 
    } 

    public  void setCal2 (Cal CAL2) {
         the this .cal2 = CAL2; 
    } 

    / ** 
     . * Class Instance Attribute name received parameter value 
     * @return 
     * / 
    public String accept2 ( ) { 
        System.out.println ( "CAL2:" + CAL2);
         return "RS" ; 
        
    }

 jsp page code

<a href="${pageContext.request.contextPath }/sy/demo_accept2.action?cal2.num1=20&&cal2.num2=5">accept2</a>

3.set/get

 private String sex;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * set/get    接收参数值
     * @return
     */
    public String accept3() {
        System.out.println(sex);
        return "rs";
    }

jsp page code

<a href="${pageContext.request.contextPath }/sy/demo_accept3.action?sex=nv">accept3</a>

Fourth, the background to the foreground by value 

  set / get property definitions can be received
  req.set .....

  1. The injection method (coupling): interfaces implemented ServletRequestAware

public class HelloAction implements ModelDriven<Cal>,ServletRequestAware{
    //注入耦合
    private HttpServletRequest req;

    /**
     * implements modelDrivern    接收参数值
     * @return
     */
    public String accept1() {
        System.out.println("cal1:"+cal1);
        //注入耦合
        req.setAttribute("cal1", cal1);
        return "rs";    
    }

    @Override
    public void setServletRequest(HttpServletRequest req) {
        this.req=req;
        
    }
}

2. The non-injection (coupling)

  /**
     * implements modelDrivern    接收参数值
     * @return
     */
    public String accept1() {
        System.out.println("cal1:"+cal1);
        //非注入耦合
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("cal1", cal1);
        return "rs";    
    }

 

Guess you like

Origin www.cnblogs.com/xiatian3452/p/11241743.html