JSP页面加载时同时访问action获取数据( struts标签s:action方法)
2017-03-14 22:32
1156 查看
有不少人想访问主页(JSP页面)时,就加载action的数据,网上许多方法都能实现,这里就说一种比较简单的使用struts2标签的<s:action>方法
以下是action标签的属性
实例:
<s:action name="xxx" executeResult="true" namespace="/">
注意name直接是action名,不是xxxx.action
直接写在jsp页面挨着body后面
<html>
<head>
.......
</head>
<body>
<s:action name="test" namespace="/" executeResult="true" />
......
</body>
</html>
然而在实际操作中可能会存在死循环,我猜测,这是因为struts在action返回success后又跳回这个页面,然后这个页面又调用这个语句。
就像这样
解决这种死循环,可以Struts2标签自己的s:if,判断获取到的数据是否为空,若为空(第一次,非后台问题),则执行s:action;不是空(得到数据),则不执行这条语句。
另一种方法,也是我的方法,和上面差不多,不同的是我使用jstl的if标签。
<body>
<c:if test="${empty list}"><s:action name="xxx" namespace="/" executeResult="true" /></c:if>
<c:if test="${not empty list}">
......
</c:if>
</body>
这是我自己测试代码的截图
运行结果
貌似没有死循环了
PS:为啥我没用struts标签自己if的方法,这是因为我嫌烦!!!我任性!!其实用struts应该比jstl的好很多
以下是action标签的属性
id | Deprecated. Use 'var' instead |
name | Name of the action to be executed (without the extension suffix eg. .action) |
namespace | Namespace for action to call |
flush | Whether the writer should be flush upon end of action component tag, default to true |
var | Name used to reference the value pushed into the Value Stack |
executeResult | Whether the result of this action (probably a view) should be executed/rendered |
ignoreContextParams | Whether the request parameters are to be included when the action is invoked |
rethrowException | Whether an exception should be rethrown, if the target action throws an exception |
<s:action name="xxx" executeResult="true" namespace="/">
注意name直接是action名,不是xxxx.action
直接写在jsp页面挨着body后面
<html>
<head>
.......
</head>
<body>
<s:action name="test" namespace="/" executeResult="true" />
......
</body>
</html>
然而在实际操作中可能会存在死循环,我猜测,这是因为struts在action返回success后又跳回这个页面,然后这个页面又调用这个语句。
就像这样
解决这种死循环,可以Struts2标签自己的s:if,判断获取到的数据是否为空,若为空(第一次,非后台问题),则执行s:action;不是空(得到数据),则不执行这条语句。
另一种方法,也是我的方法,和上面差不多,不同的是我使用jstl的if标签。
<body>
<c:if test="${empty list}"><s:action name="xxx" namespace="/" executeResult="true" /></c:if>
<c:if test="${not empty list}">
......
</c:if>
</body>
这是我自己测试代码的截图
运行结果
貌似没有死循环了
PS:为啥我没用struts标签自己if的方法,这是因为我嫌烦!!!我任性!!其实用struts应该比jstl的好很多
相关文章推荐
- Struts2 访问Action和Jsp页面出现的路径问题解决方案之 ---- 为jsp、图片、js、css等获取Web工程的绝对路径的方法
- 在Action中查询出SQL SERVER表的数据,返回一个list。怎么在JSP页面中使用Struts标签,显示出表中的内容?(转)
- struts2中 action获取jsp页面的参数的方法
- [android]webview访问jsp页面,能否从jsp页面获取数据
- 首次进入页面刷新1次|JSP获取上一个访问页面URL的方法
- jsp 页面获取action 属性的方法
- WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据
- struts中Action跳转jsp页面图片.css乱----解决方法
- Strut2 的 Action获取JSP 页面参数的方法
- jsp页面<input value=<%= %>标签获取数据库值的问题
- jsp页面动态加载form标签的action地址和里面的submit按钮事件
- struts2 action中获取jsp页面的参数的方法
- 打开jsp页面自动加载多个Action的实现方法
- JSP获取上一个访问页面URL的方法
- jsp页面访问Struts1的Action路径问题
- Struts2.1.8 JSP提交Action后,获取中文乱码解决方法
- Struts2框架中action无法获取到jsp页面传递的表单数据的解决办法
- 打开jsp页面自动加载多个Action的实现方法
- Struts2 前端JSP页面获取后台Action属性值的几种方法