个人初学struts2,在使用struts2 2.5版本部署自己的webapp时,tomcat正常启动且index.jsp正常加载,但是在页面跳转过程中,报这样一个错误:
RROR Dispatcher Could not find action or result: /struts2_1100_SimpleDataValiation/user!add?name=admin
Method add for action user is not allowed! - [unknown location]
我刚开始使用到的文件如下:
1.struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="user" extends="struts-default" namespace="/">
<action name="user" class="struts2_1100_SimpleDataValiation.UserAction">
<result>/user_add_success.jsp</result>
<result name="error">/user_add_error.jsp</result
</action>
</package>
</struts>
2.UserAction.java文件
package struts2_1100_SimpleDataValiation;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String name;
public String add() {
if(name == null || !name.equals("admin")) {
this.addFieldError("name", "name is error");
this.addFieldError("name", "name is too long");
return ERROR;
}
return SUCCESS;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.index.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<base href="<%=basePath %>"/>
<title>index</title>
</head>
<body>
使用addFieldError方法和s:fieldError标签简单处理数据校验
<a href="user!add?name=a" >添加用户</a>
</body>
</html>
4.user_add_error.jsp文件
<?xml version="1.0" encoding="UTF-8"?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Add Error</title>
</head>
<body>
User Add Error!
<s:fielderror fieldName="name" theme="simple"/>
<br />
<s:property value="errors.name[0]"/>
<s:debug></s:debug>
</body>
</html>
5.user_add_success.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>User Add Success</title>
</head>
<html>
<head>
<body>
User Add Success!
</body>
</html>
解决办法:
参考csdn大神的解决方法:
https://blog.csdn.net/qq_29407009/article/details/51474118
原来是allowedMethods.isAllowed(method)这个方法的返回值为false导致的。
<global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index</global-allowed-methods>
public boolean isAllowed(String method) {
for (AllowedMethod allowedMethod : allowedMethods) {
if (allowedMethod.isAllowed(method)) {
return true;
}
}
return false;
}
在struts.xml加入如下两条语句:
<!--动态调用方法设置为true-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!--add为UserAction.java中自己定义的处理方法-->
<global-allowed-methods>add</global-allowed-methods>
如下图示:
启动运行:
首先设置index.jsp文件下的name=admin,点击添加用户,可以看到成功运行User_Add_Success.jsp文件。
<a href="user!add?name=admin" >添加用户</a>
再首先设置index.jsp文件下的name=a(任意改,只要不是admin),点击添加用户,可以看到成功运行User_Add_Error.jsp文件。
<a href="user!add?name=a" >添加用户</a>
然后就可以点击Debug查看错误信息了。
解决了这个问题,又可以开心的玩耍了。
特此分享一下,感谢csdn的诸位大神!!