Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.

Latest commit

 

History

History
234 lines (206 loc) · 6.23 KB

Struts2 的使用.md

File metadata and controls

234 lines (206 loc) · 6.23 KB

Struts2简介

Struts1(MVC)

WebWork2(MVC)--xwork(内核)-->Struts2

Struts2结构

  • springmvc

  • struts2

基本使用

/hello.action-->Filter控制器-->HelloAction-->Result-->/hello.jsp

  1. 创建工程、引入jar包和xml配置文件

    <dependencies>
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-core</artifactId>
          <version>2.5.14.1</version>
        </dependency>
    </dependencies>
    

    在src/main/resources添加struts.xml配置文件

  2. 编写流程中主要组件

    package cn.xdl.action;
    

    public class HelloAction {

    //默认方法名为execute,无参
    public String execute(){
        System.out.println(&quot;进入HelloAction处理&quot;);
        return &quot;success&quot;;//与&lt;result&gt;配置对应
    }
    

    }

  3. 按流程配置组件

    • 配置Filter控制器

      <filter>
          <filter-name>strutsmvc</filter-name>
          <filter-class>
          org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
          </filter-class>
          <!-- 默认找src/struts.xml配置文件 -->
      </filter>
      

      <filter-mapping> <filter-name>strutsmvc</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>

    • 配置HelloAction和Result

      <package name="demo1" extends="struts-default">
      
      &lt;action name=&quot;hello&quot; class=&quot;cn.xdl.action.HelloAction&quot;&gt;
          &lt;result name=&quot;success&quot; type=&quot;dispatcher&quot;&gt;
              /WEB-INF/hello.jsp
          &lt;/result&gt;
      &lt;/action&gt;
      

      </package>

案例:列表显示

/list.action-->Filter控制器-->ListAction-->DeptDao-->Result-->/WEB-INF/list.jsp

案例:修改Action请求扩展名

默认规则

默认只有.action或没有扩展名请求能进入action,如果需要修改,按以下方法

  1. 在web.xml中将Filter配置改为/*

    <filter>
        <filter-name>strutsmvc</filter-name>
        <filter-class>
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
        <!-- 默认请求类型为.action或没有扩展名 -->
        <!-- 默认找src/struts.xml配置文件 -->
    </filter>
    

    <filter-mapping> <filter-name>strutsmvc</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

  2. 在struts.xml中定义

    <constant name="struts.action.extension" value="do">
    </constant>
    

请求命名空间namespace

/day01/hello.do 请求名为hello,namespace为/day01,需要通过< package>元素的namespace属性指定。

<package name="demo1" namespace="/day01" extends="struts-default">
&lt;!--所有action请求都需要加上/day01 --&gt;
&lt;action name=&quot;hello&quot; class=&quot;&quot;&gt;&lt;/action

</package>

案例:登录

显示登录界面

/user/tologin.do-->Filter控制器-->ActionSupport-->Result-->/WEB-INF/login.jsp

  1. 编写login.jsp

  2. 追加action配置

    <!-- class属性默认值为ActionSupport -->
    <action name="tologin">
        <!-- name默认值为success、type默认值为dispatcher -->
        <result>
        /WEB-INF/login.jsp
        </result>
    </action>
    

登录按钮处理

/user/login.do-->Filter控制器-->LoginAction-->Result-->成功ok.jsp/失败login.jsp

  1. 编写LoginAction

    利用同名属性接收请求参数

    public class LoginAction {
    
    private String username;//对应&lt;input type=&quot;text&quot; name=&quot;username&quot;&gt;
    private String password;
    
    public String execute(){
        //检查用户名和密码
        if(&quot;scott&quot;.equalsIgnoreCase(username)
            &amp;&amp;&quot;123456&quot;.equals(password)){
            return &quot;success&quot;;
        }
        return &quot;login&quot;;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    

    }

  2. 配置LoginAction和Result

    <action name="login" class="cn.xdl.action.LoginAction">
        <result name="success" type="dispatcher">/WEB-INF/ok.jsp</result>
        <result name="login" type="dispatcher">/WEB-INF/login.jsp</result>
    </action>
    

request、session、application使用

Struts2底层对request、session、application进行了封装,对应Map类型为RequestMap、SessionMap、ApplicationMap。

  1. 通用方法ActionContext

    • 获取Map类型的

      ActionContext.getSession()

      ActionContext.getApplication()

      ActionContext.get("request")

    • 获取Servlet类型的

      ServletActionContext.getRequest()

      ServletActionContext.getServletContext()

      ServletActionContext.getResponse()

  2. Action专用方法

    Action类实现Aware接口,底层注入对象.

    • 注入Map类型

      RequestAware

      SessionAware

      ApplicationAware

    • 注入Servlet类型

      ServletRequestAware

      ServletResponseAware

      ServletContextAware