Struts2中有没有类似addActionMessage这样的方法

Struts2中有没有类似addActionMessage这样的方法能实现弹出确定取消框
if(bool.equals("true")){
addActionMessage("包含敏感词:"+set);//这个发送变成弹出确定取消框来
}

该回复引自免费微信小程序-皆我百晓生:


Struts2中没有直接提供类似addActionMessage方法可以实现弹出确定取消框的功能。但可以通过使用JavaScript等前端技术来实现该功能。

你可以在前端页面中添加一个隐藏的div,当bool等于"true"时,通过JavaScript将包含敏感词的信息设置到div中,并将其显示出来。在div中添加确定和取消按钮,并绑定对应的点击事件。当点击确定按钮时,执行相应的操作;当点击取消按钮时,隐藏该div。

示例代码如下:

在页面中添加一个隐藏的div:

<div id="confirmDialog" style="display: none;">
  包含敏感词: <span id="sensitiveWords"></span>
  <br>
  <button onclick="confirmAction()">确定</button>
  <button onclick="hideConfirmDialog()">取消</button>
</div>

在JavaScript中定义相关函数:

function showDialog(words) {
  document.getElementById("sensitiveWords").innerText = words;
  document.getElementById("confirmDialog").style.display = "block";
}

function confirmAction() {
  // 执行确定操作
  // ...
  hideConfirmDialog();
}

function hideConfirmDialog() {
  document.getElementById("confirmDialog").style.display = "none";
}

在Struts2的Action中,当bool等于"true"时,调用showDialog方法:

if (bool.equals("true")) {
  String sensitiveWords = "包含敏感词: " + set; // 设置包含敏感词的信息
  String script = "showDialog('" + sensitiveWords + "');"; // 准备调用JavaScript的代码
  ServletActionContext.getResponse().getWriter().write("<script>" + script + "</script>"); // 执行JavaScript
}

这样,当bool等于"true"时,就会弹出带有确定和取消按钮的确认框,并显示包含敏感词的信息。当点击确定按钮时,执行相应的操作;当点击取消按钮时,确认框消失。