Struts2:前后台的数据交互
2017-08-01 14:47
351 查看
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 + " "; 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>
目录(?)[+]
页面获取action传来的值
方法一使用Java代码
方法二使用EL表达式
方法三使用Struts2标签
action向页面传值
方法一放在request里
方法二放在session里面
方法三使用ValueStack
附ActionContext
get and go
页面获取action传来的值:
方法一:使用Java代码
<%= request.getParameter("str")%><%=request.getAttribute("str")%>1
1
[/code]
方法二:使用EL表达式
${str}1
1
[/code]
方法三:使用Struts2标签
从ValueStack取值:<s:property value="name"/> <s:property value="user"/> <s:property value="user.id"/>1
2
3
1
2
3
[/code]
从ActionContext中取值.
取值的时候需要加上一个符号:#
<s:property value="#user"/> <s:property value="#user.id"/>1
2
1
2
[/code]
<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
[/code]
action向页面传值:
方法一:放在request里:
1、通过ActionContext的实例化对象的put:ActionContext context = ActionContext.getContext(); cotenxt.put(key, value);1
2
1
2
[/code]
对应前端取值的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
[/code]
2、通过ActionContext的实例化对象的get:
Map request = (Map)ActionContext.getContext().get("request"); request.put(key,value);1
2
1
2
[/code]
获取方法同上(不可用S标签)
3、通过ServletActionContext获取request:
HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute(key, value);1
1
[/code]
方法二:放在session里面:
1、通过request获取sessionHttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); session.setAttribute(key, value);1
2
3
1
2
3
[/code]
2、通过ActionContext获取session
HttpSession session =ActionContext.getContext().getSession(); session.setAttribute(key, value);1
2
1
2
[/code]
方法三:使用ValueStack
ValueStack vs = ActionContext.getContext().getValueStack(); User user = new User(); vs.push(user);1
2
3
1
2
3
[/code]
附: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
[/code]
(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
[/code]
get and go!
(function () {('pre.prettyprint code').each(function () {var lines = (this).text().split(′\n′).length;varnumbering = $('').addClass('pre-numbering').hide();
(this).addClass(′has−numbering′).parent().append(numbering);
for (i = 1; i
相关文章推荐
- struts2后台与界面数据交互
- Struts2的后台与前台数据交互处理方式的简单总结
- Struts2:前后台的数据交互
- Struts2:前后台的数据交互
- 微信小程序wx.request后台数据交互
- 【微信小程序】微信小程序掉进的坑之与后台数据交互
- EXTJS系列笔记————grid新增和修改数据与后台的快速交互
- Struts2前后台交互小总结
- Struts2从后台传递数据到前台的主要方法和流程
- AjaxPro组件实现前后台数据无刷新交互
- SSM框架中前后台json数据交互的相关知识
- Android与web后台进行数据交互
- 前后台数据交互
- Extjs与Struts2交互,返回JSON数据的两种实现方式
- WatchOS 2 app 与 Iphone app 数据交互(三1)覆盖式后台传输
- DWR 介绍和实例——前后台数据交互王道
- js前台与后台数据交互-前台调后台
- echarts实例后台交互数据展示
- 封装类DataProtocol 实现后台与页面的数据交互
- 用jquery +ajax 实现与php后台交互json数据