Struts2基础02

TOC

结果页面配置

全局结果页面

result标签配置action 方法的返回值到不同路径的页面中

创建两个 action,执行默认的方法 execute方法,让两个 action的方法都返回 success,返 回 success之后,配置到同一个页面里面
(1)如果多个 action,方法里面返回值相同的,到页面也是相同的,这个时候可以使用全 局结果页面配置

<global-results>
     <result name = "success" >/Hello.jsp</result>
</global-results>

在package标签页中进行配置

局部结果页面

同时配置了全局和局部结果页面,以局部结果页面为准

result标签中的type属性

1.type属性:如何到路径中(转发、重定向)
2.type属性值:

  • 默认值:转发 dispatcher
  • 重定向: redirect

3.dispatcher\redirect是针对到页面的值,对于到其他action采用

  • chain:转发到其他action
  • redirectAction:重定向到其他acction
    <package name="" extends="struts-default" strict-method-invocation="false">
          <global-results>
              <result name="success">/Hello.jsp</result>
          </global-results>
    
          <action name="stu_*" class="per.wzy.Action.Hello" method="{1}">
              <result name="read" type="redirectAction">read</result>
          </action>
          <action name="read" class="per.wzy.Action.read">
    
          </action>
      </package>

    该示例中,先访问stu_add.action,根据配置重定向到read.action。read中execute()方法返回值为success,默认转发到全局结果页面。

Action获取表单提交数据

1.使用ActionContext类

  • 因为该方法不是静态方法,所以需要创建对象调用
  • ActionContext不是通过new来创建对象
  • 通过ActionContext.getContext()来获取对象
@Override
    public String execute() throws Exception {
        ActionContext context = ActionContext.getContext();
        //获取表单数据 string - 属性名   Parameter - 属性对应的值
        Map<String, Parameter> map = context.getParameters();
        //遍历表单数据
        for (String key:map.keySet()) {
            System.out.println("key: "+key+" value: "+map.get(key).getValue());
        }

        return Action.NONE;
    }

2.使用ServeltActionContext类

  • 通过ServletActionContext.getRequest()方法调用
    //获取request对象
          HttpServletRequest request = ServletActionContext.getRequest();
          //调用get
          String name = request.getParameter("name");
          String pwd = request.getParameter("pwd");

3.使用接口注入方式(了解)

  • 实现ServletRequestAware接口获取request对象




posted @ 2019-07-23 10:26  local-  阅读(119)  评论(0)    收藏  举报