Struts

前言

1、Struts

  • Apache Struts 2 最初被称为 WebWork 2,它是一个简洁的、可扩展的框架,可用于创建企业级 Java web 应用程序。

  • 设计这个框架是为了从构建、部署、到应用程序维护方面来简化整个开发周期。

1.1 配置

  • 新建 web.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <web-app>
    <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>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    </web-app>
  • 配置 struts.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
    <package name="basicstruts" extends="struts-default">
    <action name="index">
    <result>index.jsp</result>
    </action>
    </package>
    </struts>
  • 基本原理

    • 所有的访问都会被 web.xml 中配置的 Struts 的 Filter 所拦截。
    • 拦截之后,就进入 struts 的工作流程。
    • 访问的地址是 /index,根据 struts 按照 struts.xml 中的配置,服务端跳转到 index.jsp。
    • 显示 index.jsp 的内容。
  • WebSocket 配置

    • 因为 Struts 会把所有的请求都拦截下来,所以需要加一个例外,以保证 webSocket 请求能够被正常捕捉住,不然就被 Struts 搞跑了。

      1
      2
      3
      <struts>
      <constant name="struts.action.excludePattern" value="/ws/bitcoinServer" />
      </struts>

1.2 显示数据到 JSP

  • 把 Model 的数据显示在视图 JSP 上。访问路径 /showProduct 会调用 ProductAction 类的 show 方法,服务端跳转到 show.jsp。

    1
    2
    3
    <action name="showProduct" class="com.how2java.action.ProductAction" method="show">
    <result name="show">show.jsp</result>
    </action>

1.3 提交数据到 Action

  • 提交数据到 Action

    1
    2
    3
    <action name="addProduct" class="com.how2java.action.ProductAction" method="add">
    <result name="show">show.jsp</result>
    </action>

1.4 中文问题

  • Struts 的中文问题,由 3 部分组成

    • jsp 提交数据的时候,必须是 UTF-8 编码的。
    • struts 拿到数据后进行 UTF-8 解码。
    • 服务端跳转到 jsp 进行显示的时候,要指定浏览器使用 UTF-8 进行显示。UTF-8 可以换成 GBK 或者 GB2312,但是必须统一,不能混用。

1.5 Error FilterStart

  • 在 Tomcat 启动 struts web 应用的时候,如果出现了 struts 配置上的错误,可能只能看到一个 Error FilterStart 的提示,而看不到详细的错误原因。

  • 这是因为默认配置下,struts 把日志输出关闭了。为了把日志输出开启便于调试,需要增加 log4j.xml 这个配置文件。

1.6 request response

  • 在 struts 中也可以获取 servlet 包中的 request 和 response 对象。

  • Struts 使用类 StrutsRequestWrapper 对 HttpServletRequest 进行了封装。

    1
    2
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();

1.7 session

  • Struts 中的 session 有两个

    • 一个是传统的 servlet 包下的 HttpSession。
    • 另一个是 Struts 中自己定义的 Session。
  • 传统的 servlet 包下的 session 的获取办法,使用该方法,需要在项目中导入 servlet-api.jar。

    1
    ServletActionContext.getRequest().getSession();
  • 新的 Session 的获取办法,这个 session 以 Map 类的形式出现,其中的值和 HttpSession 中的值是同步的。

    1
    Map m = ActionContext.getContext().getSession();

1.8 上传文件

  • 与通过 Servet 进行上传文件比较起来,Struts 的上传文件就简单很多。

  • 上传一定要为 form 加上 enctype="multipart/form-data",表示提交的数据是二进制的。并且必须是 method="post"

  • 使用 struts 的 s:file 标签标识上传文件框。

    1
    <s:file name="doc" label="upload" />
  • Struts 上传文件的最大值默认是比较小的只有 2M,可以进行设置。

    1
    2
    3
    <struts>
    <constant name="struts.multipart.maxSize" value="10240000"/>
    </struts>

2、标签

2.1 form 标签

  • form 标签:用于提交数据。

    1
    2
    3
    4
    <s:form action="addProduct">
    <s:textfield name="product.name" label="product name" />
    <s:submit value="Submit" />
    </s:form>

2.2 iterator 标签

  • iterator 标签:用于遍历一个集合中的数据。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <s:iterator value="products" var="p" status="st">
    <tr>
    <td>${p.id}</td>
    <td>${p.name}</td>
    <td>${st.index}</td>
    <td>${st.count}</td>
    <td>${st.first}</td>
    <td>${st.last}</td>
    <td>${st.odd}</td>
    <td>${st.even}</td>
    </tr>
    </s:iterator>

2.3 check 标签

  • check 标签:checkbox 多选。

    1
    <s:checkboxlist value="selectedProducts" name="product.id" list="products" listValue="name" listKey="id" />

2.4 radio 标签

  • radio 标签:单选。

    1
    <s:radio name="product.id" value="2" list="products" listValue="name" listKey="id" />

2.5 select 标签

  • select 标签:下拉菜单。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <s:select label="products"
    name="product.id"
    list="products"
    listKey="id"
    listValue="name"
    multiple="true"
    size="3"
    value="selectedProducts"
    />

2.6 多重迭代

  • 多重迭代

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <s:iterator value="categories" var="c">
    <tr>
    <td>${c.id}</td>
    <td>${c.name}</td>
    <td>
    <s:iterator value="#c.products" var="p">
    ${p.name}<br/>
    </s:iterator>
    </td>
    </tr>
    </s:iterator>

3、其他

  • 调试

  • 通配符匹配:add,show, list 分别需要进行配置,通过通配符匹配可以把这 3 个配置整合在一个配置中实现。

  • 拦截器:拦截器可以简单地看成是 Struts 中的 “filter”,拦截器可以拦截指定的 Action,并且对 Action 进行相应的操作。

  • 客户端跳转:Struts 默认跳转方法是服务端跳转。

  • 表单验证

  • Action 是多实例的:在默认设置下,Action 是多实例的,每次访问都会创建新的 Action 实例。

  • 注解方式:Struts 还能够基于注解进行配置。

文章目录
  1. 1. 前言
  2. 2. 1、Struts
    1. 2.1. 1.1 配置
    2. 2.2. 1.2 显示数据到 JSP
    3. 2.3. 1.3 提交数据到 Action
    4. 2.4. 1.4 中文问题
    5. 2.5. 1.5 Error FilterStart
    6. 2.6. 1.6 request response
    7. 2.7. 1.7 session
    8. 2.8. 1.8 上传文件
  3. 3. 2、标签
    1. 3.1. 2.1 form 标签
    2. 3.2. 2.2 iterator 标签
    3. 3.3. 2.3 check 标签
    4. 3.4. 2.4 radio 标签
    5. 3.5. 2.5 select 标签
    6. 3.6. 2.6 多重迭代
  4. 4. 3、其他
隐藏目录