前排提示:完整代码都放在GitHub上面哦,戳这里:https://github.com/MarsOu1998/Struts2ErrorTag
package Test;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class test2 extends ActionSupport {
@Override
public String execute() throws Exception {
this.addActionError("ActionError错误1");
this.addActionError("ActionError错误2");
this.addActionMessage("ActionMessage1");
this.addActionMessage("ActionMessage2");
this.addFieldError("filederror1","字段错误1");
this.addFieldError("filederror2","字段错误2");
return "success";
}
}
在struts.xml里面配置success跳转的页面
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: oushile
Date: 2019/9/7
Time: 09:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:actionerror></s:actionerror>
<s:actionmessage></s:actionmessage>
<s:fielderror value="filederror1"></s:fielderror>
</body>
</html>
值得一提的事,这些错误标签会把里面所有的内容都显示出来,即使使用filederror,并制定了value,但是还是没什么用。