zoukankan      html  css  js  c++  java
  • struts2学习笔记(3)

    1.Result

    (1)Global-result

      全局result,为本package中的所有action共享

    (2)动态result

      可以在一个action中定义一个属性来保存结果,在struts配置中使用${}来读取。$用来读取栈值。

    View Code
    package com.bpf.struts2.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport
    {
        private int type;
        private String r;
        
        public int getType()
        {
            return type;
        }
    
        public void setType(int type)
        {
            this.type = type;
        }
    
        public String getR()
        {
            return r;
        }
    
        public void setR(String r)
        {
            this.r = r;
        }
        @Override
        public String execute() throws Exception
        {
            if(type == 1)
                r = "/user_success.jsp";
            else if(type ==2)
                r = "/user_error.jsp";
            return SUCCESS;
        }
    }

    struts.xml配置:

    View Code
    <action name="user" class="com.bpf.struts2.action.UserAction">
    <result>${r} </result>
    </action>

    2.访问web元素

    常用的有两种方法:

    (1)依赖容器(struts2)

    View Code

     (2)IoC(Inverse of Control, 控制反转)或叫DI(Dependency Injection)

    View Code

    在JSP页面上获得web元素:

    View Code

    3.参数传递

      客户端跳转才需要传递。因为一次request只有一个value stack,一次request涉及到的所有action共用同一个value stack。所有参数都可以从value stack中获得。

    <action name="tranPara" class="com.bpf.struts2.action.UserAction">
          <result type="redirect">/user_success.jsp?t=${type} </result>
    </action>

      跳转到JSP页面,没有action,也就没有value stack,参数保存在stack context中的parameters中,所以获取参数可用:

    <body>
        type的值:<s:property value="#parameters.t"/>
     </body>

       

    <body>
    使用struts2的property标签获得web元素:<br />
        <s:property value="#request.r1"/> <br/>
        <s:property value="#session.s2"/> <br/>
        <s:property value="#application.a3"/> <br/>
    使用JSP脚本获得web元素:<br />    
        <%=request.getAttribute("r1") %> <br/>
        <%=session.getAttribute("s2") %> <br/>
        <%=application.getAttribute("a3") %> <br/>
    </body>
  • 相关阅读:
    一道leetcode题的收获如何比较字符串的大小重写sort中的compare[](string &s,string &t){return s+t>t+s};
    unsigned int表示负数问题
    fork()和printf()几点注意细节
    32位机中数据问题
    C++隐藏机制
    ||,&&,++i解答
    enum忽略知识点
    硬链接与软链接
    20145215实验五 Java网络编程及安全
    证书与keytool
  • 原文地址:https://www.cnblogs.com/paulbai/p/2620127.html
Copyright © 2011-2022 走看看