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

本文介绍了如何在JSP页面加载时利用Struts2的s:action标签从action获取数据,以避免页面加载时产生死循环的问题。通过在s:action标签中结合s:if或jstl的if标签进行条件判断,确保只有在数据为空时才执行action请求。这种方法简化了实现主页加载时获取数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有不少人想访问主页(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的好很多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值