Struts2开发步骤

一.导入相关的jar包

1.如果只使用Struts2的基本功能, 只需要导入Struts2的jar包即可:

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
        </dependency>

2.如果需要把Struts2整合到Spring框架中, 需要导入以下的jar包:

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

3.如果需要使用Struts2进行注解开发, 需要导入以下的jar包:

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

4.如果需要把结果以json的方式返回, 需要添加struts2中的json插件:

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>
  1. 必须在web.xml文件中配置Struts2的过滤器, 不会写可以从Struts2的示例代码中拷贝
    <!-- struts2 过滤器 -->
    <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>

二.在注解插件包struts2-convention-plugin里面, 有一个struts-plugin.xml配置文件

  1. 里面规定了这个插件只扫描指定的包里的注解
    需要被扫描注解的类必须要在指定的包中, 这些包必须包含指定的关键字:action,actions,struts,struts2
  <constant name="struts.convention.package.locators" value="action,actions,struts,struts2"/>
  1. 被扫描的类必须以指定的关键字结尾: Action
<constant name="struts.convention.action.suffix" value="Action"/>

三.Struts2中常用的注解

1.类上:

@ParentPackage("struts-default")  表示继承了谁, 相当于Struts2配置文件中的 extends="struts-default"
@Namespace("/")  命名空间 , 相当于Struts2配置文件中的  namespace="/"
@Actions   相当于Struts2配置文件中的Actions

2.方法上

@Action 用于配置访问路径, 通过这个路径可以找到这个方法
@Result 用于配置这个请求的结果集

示例:
@Action(value="save",results= {@Result(name="success",type="redirect",location="./pages/success.html"),
            @Result(name = "error", type="redirect", location="./page/error.html") })
public String save(){
  return SUCCESS;
}

3.Struts2与Spring整合用到的注解, 方便Spring来管理Struts2的内容

@Controller  指定为Spring的表现层
@Scope   指定Struts2的Action为多例的

四.基本使用步骤:

1.定义包:

com.gongxm.web.action         //包名包含了指定的关键字:action

2.在包中定义一个类


//1.添加注解
@ParentPackage("struts-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
class SaveAction extends ActionSupport implements ModelDriven<Book>{

//2.继承ActionSupport类

//3.实现ModelDriven接口, 指定这个Action接收的数据是哪个bean的对象

//4.创建这个bean的对象
Book book = new Book();

  //5.重写ModelDriven接口中的方法,返回bean的对象
  @Override
  public Book getModel() {
    return book;
  }

//6.定义请求对应的方法, 使用注解指定请求路径和返回结果
  @Action(value="save",results= {@Result(name="success",type="redirect",location="./pages/success.html"),
            @Result(name = "error",type="redirect",location="./page/error.html") })
  public String save(){
    return SUCCESS;
  }
}

五.使用struts2中的json插件步骤:

1.在定义Action类时, 必须要写:

@ParentPackage("json-default")  // 它其实是继承了"struts-default"的, 所以,其他代码也可以正常运行

2.方法上,指定返回值类型为json:

    @Action(value="book_page_query",results= {@Result(name="success",type="json")})
    public String pageQuery() {
        int page = 1; //当前页码
        int rows = 10;//每页数据记录条数
        Pageable pageable = new PageRequest(page-1, rows); // page是从0开始的, 0代表第一页
        Page<Book> pageData = standardService.findAll(pageable);
        //创建结果
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());
        //使用插件中的功能,将结果转换成json数据,压入栈顶
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,727评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,291评论 6 342
  • 1 新建一个web工程; 2把struts2核心包拷到工程的lib下; 3在web.xml中配置Struts2的前...
    蘋果_283e阅读 332评论 0 0
  • 我们先从手机自带的相机说起,大学时期我用的是ip,后来陆续用过小米,vivo。 不说别的高深技巧,很多人都不会用手...
    机长_阅读 708评论 0 5
  • 01 早上看到一则新闻,说一男的,在单位食堂排队打饭的时候,看见一员工插队,就说了他两句。不料那位员工就是他领导,...
    瓜子向日葵阅读 1,438评论 2 6

友情链接更多精彩内容