有不少人想访问主页(JSP页面)时,就加载action的数据,网上许多方法都能实现,这里就说一种比较简单的使用struts2标签的<s:action>方法
以下是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的好很多