struts2 动态方法以及版本之间使用的问题

1.struts2 使用动态方法通用功能测试时(版本2.5),出现了报错。 当时我以为是自己写错了,但是返回看没检查出来错误,然后我就使用(版本2.3)测试。发现在2.3版本中,没有错误。经过官网查询,在版本2.5中default.properties 默认配置,如图

2.开启动态通用配置,默认是false

3.然后这就需要我们在struts.xml中添加配置了

<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

4.满怀心细之情去测试,结果没有预测中的结果,却来了和原来一样的错误。如图

5.为什么我的报错那么多?你的报错却只有几行?原因在struts.xml 中添加

<!-- 启用开发模式配置 -->
<constant name="struts.devMode" value="true"></constant>

6.回归正题,头疼的事情依旧没有解决。没有办法,只好请教度娘了。

果然度娘依旧给力,我搜到了

http://www.cnblogs.com/gsy52300/p/5778754.html

我看过后,心情终于好点了,头疼也神奇的好了。不过我发现这不是我最终想要的效果。

总结看过它之后的配置

struts.xml配置如下

<struts>
   <!-- 使用下面这种方法,这里动态通配可以关闭 -->
   <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
   <!-- 启用开发模式 -->
   <constant name="struts.devMode" value="true"></constant>
   
   <package name="a" extends="struts-default" namespace="/">
   		<action name="us*" class="com.struts2.action.Meths" method="{1}">
			<result name="login">/login.jsp</result>
			 <!-- 这里写自己的方法,用逗号分割开 -->
			<allowed-methods>userAdd,方法1,方法2,方法3</allowed-methods>
   		</action>
   </package>
</struts>

前台页面,伪代码。

<%
	String base = request.getScheme() + "://" +
				    request.getServerName() + ":" +
					request.getServerPort() + 
					request.getContextPath();
%>
<a href="<%=base%>/ususerAdd">测试</a>

测试了下,诶呦,终于成功了。

7.不过不是我要的效果,没办法,只要继续劳驾度娘了。终于皇天不负我真情,找到了最终我想要的效果。原文章地址 http://blog.csdn.net/maobois/article/details/51854607 (博主总结)这里主人也是引荐的  http://ask.csdn.net/questions/260958 (最终答案)

再来看看修改下的配置

<struts>
   <!-- 开启动态方法匹配 -->
   <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
   <constant name="struts.devMode" value="true"></constant>
   
   <package name="a" extends="struts-default" namespace="/">
   		 
   		<!-- 关键地方  struts2.5 为了提升安全性,添加了 allomethod 这么个玩意-->
   		 <global-allowed-methods>regex:.*</global-allowed-methods>
   		 
   		<action name="us*" class="com.struts2.action.Meths" method="{1}">
			<result name="login">/login.jsp</result>
   		</action>
   </package>
</struts>

前台页面不变。到此,终于解决了。

8.对于遇到的问题,解决方法总结下,免得记性不好,以后忘记,也给遇到此问题的小伙伴,一个惊喜。

1.1通过启用动态方法匹配

<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>

1.2与之相对应的在package包的添加

<global-allowed-methods>regex:.*</global-allowed-methods>

完整配置

<?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.enable.DynamicMethodInvocation" value="true"></constant>
   <constant name="struts.devMode" value="true"></constant>
   
   <package name="a" extends="struts-default" namespace="/">
   		 
   		<!-- 关键地方  struts2.5 为了提升安全性,添加了 allomethod 这么个玩意-->
   		 <global-allowed-methods>regex:.*</global-allowed-methods>
   
   		<action name="us*" class="com.struts2.action.Meths" method="{1}">
			<result name="login">/login.jsp</result>
   		</action>
   </package>
</struts>

2.1 不需要开启动态方法匹配配置

<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>

2.2 相关配置

<action name="us*" class="com.struts2.action.Meths" method="{1}">
	<result name="login">/login.jsp</result>
	<allowed-methods>方法1,方法2</allowed-methods>
</action>

2.3 如果你感觉,2.2一一写出方法,太麻烦,也可以这么写

<action name="us*" class="com.struts2.action.Meths" method="{1}">
	<result name="login">/login.jsp</result>
	<allowed-methods>regex:.*</allowed-methods>
 </action>

这里说下自己对这里的见解

什么时候用1.1 和2.1

如果你同一个包中,都需要使用这种动态匹配,显然1.1更适合你。

<package name="">
   	<action name=""></action>
   	<action name=""></action>
   	<action name=""></action>
</package>

反正,如果只是局部某个<action>使用,就选2.1

我擦,人都走完了,我也该进餐去了。

时间: 2024-08-11 01:36:21

struts2 动态方法以及版本之间使用的问题的相关文章

Struts2动态方法调用怎么写配置

Struts2动态方法调用怎么写配置?可以这样: 1.Struts2入门之 HelloWorld  编写不依赖任何三方包的Action类 按照MVC架构,Action执行完后要返回显示的结果视图,所以,Action中的请求处理方法要返回一个逻辑名称来表示它要显示的结果视图,即方法的 返回值类型为代表逻辑名称的字符串. 在 struts.xml配置文件中配置Action类 struts.xml文件中的配置信息使用package进行分类和管理,不同的包对应不同的url访问路径,即在访问路径上就能区分

struts2动态方法无法调用

struts2 大约2.3版本以后,调用动态方法会产生类似下面的错误 :Unable to instantiate Action, top.juson.study.action.Action2!eat, defined for 'action2!eat' in namespace '/'top.juson.study.action.Action2!eat 其中的defined for 'action2!eat' 说明这里没有把eat正常的解析为方法,而是和前面的action一起解析成了方法, 原

Struts2动态方法调用(DMI)

在Struts2中动态方法调用有三种方式,动态方法调用就是为了解决一个Action对应多个请求的处理,以免Action太多 第一种方式:指定method属性这种方式我们前面已经用到过,类似下面的配置就可以实现 <action name="chainAction" class="chapter2.action.Chapter2Action" method="chainAction"> <result name="chai

Struts2动态方法调用

动态方法就是一个Action对应多个请求,减少Action的数量 1.指定method属性 <action name="addAction" method="add" class="com.venn.action.HelloWorldAction"> <result>/jsp/add.jsp</result> </action> 2.感叹号(!)方式(不推荐使用) <action name=&

struts2动态方法的配置

如果想通过aciton!method.action的形式访问,需要做两个设置 1.配置文件中设置动态方法调用为true <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 2.action中配置值允许的方法: <allowed-methods>login,logout</allowed-methods> 原文地址:https://www.cn

Struts2动态方法调用:通配符方式

更改src/struts2.xml的代码: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <

struts2 - 动态方法调用与动态结果调用 小 demo

访问请求: http://localhost:8080/strutsdemo1/user_index.action?username=zhangsan struts.xml 文件配置: TestAction.java 文件:

Struts2学习四----------动态方法调用

? 版权声明:本文为博主原创文章,转载请注明出处 Struts2动态方法调用 - 默认:默认执行方法中的execute方法,若指定类中没有该方法,默认返回success <package name="default" extends="struts-default" namespace="/"> <action name="add" class="org.struts.dynamicmethod.a

struts2.5动态方法调用和默认Action

在动态方法调用中,使用通配符方法出现问题,参考了http://www.cnblogs.com/jasonlixuetao/p/5933671.html 这篇博客,问题解决了. 这个是helloworld.xml: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Co