从零打造在线网盘系统之Struts2框架起步

欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重地阐述SSH三个框架的基础知识,第四部分将会进入项目实战,如果您已经对SSH框架有所掌握,那么可以直接浏览第四章,源码均提供在GitHub/ssh-network-hard-disk上供大家参阅

本篇目标

  • 掌握Struts2工作流程
  • 掌握Struts2控制器
  • 掌握Struts2XML配置
  • 掌握Struts2注解配置
  • 了解Struts2的浏览器插件

Struts2概述

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Strus2作为控制器(Controller)来建立模型与视图的数据交互。Struts2使用了大量的拦截器来处理用户请求,从而将业务逻辑控制器和ServletAPI分离

Struts2工作流程

  1. 客户端发起请求
  2. 核心控制器FilterDispatcher拦截请求调用Action
  3. 调用Action的execute方法前调用一系列的拦截器
  4. 调用execute执行业务逻辑
  5. 返回结果

控制器是What?

包含execute方法的POJO既可以作为控制器,即一个简单的JAVA类包含一个execute()方法就可以作为控制器,同时控制器具有封装客户端请求参数的能力.

public class TestAction {

    public String execute() throws Exception {
        return "test";
    }
}

Struts2XML配置

XML配置完整工程示例源码下载
导入struts依赖jar

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.18</version>
        </dependency>

web.xml配置Struts2拦截器

    <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>

编写struts.xml

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

    <package name="default" extends="struts-default">
        <action name="hello" class="com.jimisun.action.TestAction">
            <result name="hello">/WEB-INF/jsp/hello.jsp</result>
        </action>
    </package>
</struts>

编写控制器

public class TestAction {

    public String execute() throws Exception {
        return "hello";
    }
}

配置好以上步骤即可访问路径http://localhost:8080/hello.action

Struts2注解配置

注解配置Struts2完整示例源码下载
如果需要使用注解开发,则需要增加struts2-convention-plugin的Jar

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.5.18</version>
        </dependency>

那么在你的Action中就可以这样编写Action,不需要再到struts.xml中进行配置

@ParentPackage("struts-default")
@Namespace("/test")
public class TestAction {

    @Action(value = "hello", results = {
            @Result(name = "hello", location = "/WEB-INF/jsp/hello.jsp")
    })
    public String hello() throws Exception {
        return "hello";
    }
}

Struts2的浏览器插件

在进行Struts2开发的时候随着项目的增大,你所需要处理的路径和方法映射越多,有时候会让你手忙脚乱,而struts2-config-browser-plugin插件很好的帮你解决了这个问题,只需要Jar包依赖即可

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-config-browser-plugin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-config-browser-plugin</artifactId>
            <version>2.5.18</version>
        </dependency>

本章总结

Struts2的起始配置比较简单,但是Struts2其他相关配置就比较繁琐了,不可掉以轻心

posted @ 2018-11-12 13:35  jimisun  阅读(1095)  评论(0编辑  收藏  举报
豫ICP备17005230号-3