zoukankan      html  css  js  c++  java
  • struts2取值

    http://www.cnblogs.com/yangy608/archive/2010/11/23/1885256.html

    struts2取值

    1.标签取值方式一

        通过<s:property value="" />取值

        当Action的valueStack中有该属性的值时,只需直接使用该属性的名字即可;

        当Action的valueStack中没有该属性的值时,比如在session,application范围中的属性值时,需要加#或者#attr.;

        例子:

        假设某Action中有person成员变量,在application中存在company属性

        那么我们可以通过以下方法取值:

        <s:property value="person.name" />

        <s:property value="#person.name" />

        <s:property value="company.name" /> //无法取到,因为company不在action的valueStack中

        <s:property value="#company.name" />

    2.标签取值方式二

    在任意的<s:/>标签内使用%{}来取值

    当Action的valueStack中有该属性的值时,只需直接使用该属性的名字即可;

    当Action的valueStack中没有该属性的值时,比如在session,application范围中的属性值时,需要加#或者#attr.;

    例子:

    假设某Action中有person成员变量,在application中存在company属性

    <s:textfield name="person.name" value="person.name" /> //错误,value会直接显示person.name字样

    <s:textfield name="person.name" value="%{person.name}" />

    <s:textfield name="person.company.name" value="%{#company.name}" />

    <s:textfield name="person.company.name" value="%{#attr.company.name}" />

    3.获取JSP页面的request,session,application中的属性

       在页面中可以这样获取:

    <td>${applicateionScope.counter}</td>

    <td>${sessionScope.counter}</td>

    <td>${requestScope.counter}</td>

    或者直接这样用:${属性} ${userBean.username}。userBean可以是request或session中的对象。

    struts2中的Action代码中的内容为:

    ActionContext ctx = ActionContext.getContext();

    ctx.getApplication.put("counter",new Integer(5));

    ctx.getSession.put("counter",new Integer(5));

    ctx.put("counter",new Integer(5));

    ctx.put就是直接设置request的值。

    也可以使用以下方式获得request:

    HttpServletRequest r = ServletActionContext.getRequest();

    HttpServletResponse resp = ServletActionContext.getResponse();

    struts2还提供了以下接口:

    ServletContextAware:Action实现该接口,可以直接访问ServletContext。

    ServletRequestAware:Action实现该接口,可以直接访问HttpServletRequest。

    ServletResponseAware:Action实现该接口,可以直接访问HttpServletResponse。

  • 相关阅读:
    userdir 希望用户能够以http://X.X.X.X/~username 方式来访问自己的网页
    var_export() 函数的使用
    mb_detect_encoding — 检测字符的编码
    详解PHP fsockopen的使用方法
    jQuery 返回顶部
    Mysql函数
    sql where 1=1和 0=1 的作用
    Numpy基础学习(三)
    Numpy 中的矩阵
    Numpy数组的全通用函数
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648023.html
Copyright © 2011-2022 走看看