Struts2:前后台的数据交互

来源:互联网 发布:北京编程培训机构排名 编辑:程序博客网 时间:2024/05/22 05:23

Struts2:前后台的数据交互

    <div class="article_manage clearfix">    <div class="article_r">        <span class="link_postdate">2016-07-18 11:08</span>        <span class="link_view" title="阅读次数">675人阅读</span>        <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>        <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('Struts2%ef%bc%9a%e5%89%8d%e5%90%8e%e5%8f%b0%e7%9a%84%e6%95%b0%e6%8d%ae%e4%ba%a4%e4%ba%92','51940313');return false;" title="收藏" target="_blank">收藏</a></span>         <span class="link_report"> <a href="#report" onclick="javascript:report(51940313,2);return false;" title="举报">举报</a></span>    </div></div>    <style type="text/css">                .embody{            padding:10px 10px 10px;            margin:0 -20px;            border-bottom:solid 1px #ededed;                        }        .embody_b{            margin:0 ;            padding:10px 0;        }        .embody .embody_t,.embody .embody_c{            display: inline-block;            margin-right:10px;        }        .embody_t{            font-size: 12px;            color:#999;        }        .embody_c{            font-size: 12px;        }        .embody_c img,.embody_c em{            display: inline-block;            vertical-align: middle;                       }         .embody_c img{                           width:30px;            height:30px;        }        .embody_c em{            margin: 0 20px 0 10px;            color:#333;            font-style: normal;        }</style><script type="text/javascript">    $(function () {        try        {            var lib = eval("("+$("#lib").attr("value")+")");            var html = "";            if (lib.err == 0) {                $.each(lib.data, function (i) {                    var obj = lib.data[i];                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";                    html += ' <a href="' + obj.url + '" target="_blank">';                    html += ' <img src="' + obj.logo + '">';                    html += ' <em><b>' + obj.name + '</b></em>';                    html += ' </a>';                });                if (html != "") {                    setTimeout(function () {                        $("#lib").html(html);                                              $("#embody").show();                    }, 100);                }            }              } catch (err)        { }    });</script>  <div class="category clearfix">    <div class="category_l">       <img src="http://static.blog.csdn.net/images/category_icon.jpg">        <span>分类:</span>    </div>    <div class="category_r">                <label onclick="GetCategoryArticles('6105801','lk7688535','top','51940313');">                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">跟着项目学javaweb<em>(35)</em></span>                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">                    <div class="subItem">                        <div class="subItem_t"><a href="http://blog.csdn.net/lk7688535/article/category/6105801" target="_blank">作者同类文章</a><i class="J_close">X</i></div>                        <ul class="subItem_l" id="top_6105801">                                                    </ul>                    </div>                </label>                        </div></div>    <div class="bog_copyright">                 <p class="copyright_p">版权声明:本文为博主原创文章,转载时请注明出处URL,谢谢大家~</p>    </div>

目录(?)[+]

  1. 页面获取action传来的值
    1. 方法一使用Java代码
    2. 方法二使用EL表达式
    3. 方法三使用Struts2标签
  2. action向页面传值
    1. 方法一放在request里
    2. 方法二放在session里面
    3. 方法三使用ValueStack
  3. 附ActionContext
  4. get and go

页面获取action传来的值:

方法一:使用Java代码

<%= request.getParameter("str")%><%=request.getAttribute("str")%> 
  • 1
  • 1

方法二:使用EL表达式

${str}

  • 1
  • 1

方法三:使用Struts2标签

从ValueStack取值:

   <s:property value="name"/>   <s:property value="user"/>   <s:property value="user.id"/>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

从ActionContext中取值.
取值的时候需要加上一个符号:#

   <s:property value="#user"/>   <s:property value="#user.id"/>
  • 1
  • 2
  • 1
  • 2
<s:property value="#parameters.name"/><br>   //获得request中key为MyName的值   <s:property value="#request.MyName"/><br>   //获得session中key为MyName的值的值   <s:property value="#session.MyName"/><br>   //获得application中key为MyName的值的值   <s:property value="#application.MyName"/><br>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

action向页面传值:

方法一:放在request里:

1、通过ActionContext的实例化对象的put:

ActionContext context = ActionContext.getContext();cotenxt.put(key, value);
  • 1
  • 2
  • 1
  • 2

对应前端取值的4种方式:

<%=request.getAttribute("str") %>${str} <s:property value="str"/>  <s:property value="#request.str"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、通过ActionContext的实例化对象的get:

Map request = (Map)ActionContext.getContext().get("request");request.put(key,value);
  • 1
  • 2
  • 1
  • 2

获取方法同上(不可用S标签)

3、通过ServletActionContext获取request:

HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute(key, value);
  • 1
  • 1

方法二:放在session里面:

1、通过request获取session

HttpServletRequest request = ServletActionContext.getRequest();    HttpSession session = request.getSession(); session.setAttribute(key, value);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

2、通过ActionContext获取session

  HttpSession session =ActionContext.getContext().getSession();  session.setAttribute(key, value);
  • 1
  • 2
  • 1
  • 2

方法三:使用ValueStack

ValueStack vs = ActionContext.getContext().getValueStack();User user = new User();vs.push(user);
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

附:ActionContext

1、每一次请求都会创建一个ActionContext对象(也会创建一个ValueStack),struts框架默认向这个对象里面存放的对象(数据)很多,包括request、session、application、ValueStack、parameters等。
2、request session application的map类型获取:
(1)手动获取

 ActionContext ac = ActionContext.getContext();       //获得Map类型request       Map<String,Object> request =              (Map<String, Object>) ac.get("request");       //获得Map类型session           Map<String, Object> session = ac.getSession();       //获得Map类型application       Map<String, Object> application = ac.getApplication();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(2)自动获取(通过继承接口)
三个接口依次为:
RequestAware,SessionAware,ApplicationAware

public class WebActionTest extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{            private Map<String, Object> request;            private Map<String, Object> session;            private Map<String, Object> application;            @Override            public String execute() throws Exception {                //页面中用原类型的对象取正常值就可以了                request.put("MyName", "tom");                session.put("YourName", "zhansan");                application.put("HerName", "lily");                return SUCCESS;            }            public void setRequest(Map<String, Object> request) {                this.request = request;            }            public void setSession(Map<String, Object> session) {                this.session = session;            }            public void setApplication(Map<String, Object> application) {                this.application = application;            }        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

get and go!

(function () {('pre.prettyprint code').each(function () { var lines = (this).text().split(\n).length;varnumbering = $('
    ').addClass('pre-numbering').hide(); (this).addClass(hasnumbering).parent().append(numbering); for (i = 1; i
    原创粉丝点击