您的位置:首页 > 编程语言 > Java开发

JSP页面加载时同时访问action获取数据( struts标签s:action方法)

2017-03-14 22:32 1156 查看
有不少人想访问主页(JSP页面)时,就加载action的数据,网上许多方法都能实现,这里就说一种比较简单的使用struts2标签的<s:action>方法

以下是action标签的属性

idDeprecated. Use 'var' instead
nameName of the action to be executed (without the extension suffix eg. .action)
namespaceNamespace for action to call
flushWhether the writer should be flush upon end of action component tag, default to true
varName used to reference the value pushed into the Value Stack
executeResultWhether the result of this action (probably a view) should be executed/rendered
ignoreContextParamsWhether the request parameters are to be included when the action is invoked
rethrowExceptionWhether 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的好很多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp action struts