struts2 2.5版本ERROR:Method add for action user is not allowed!解决方法

个人初学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的诸位大神!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值