struts2.5.2 通配符问题_亲测有用

学了一段时间struts2,跟着教程做,但发现struts2的版本不同,很多东西的使用是有差异的。例如之前遇到的创建sessionFactory的方式就跟之前版本有着明显的差异。今天又遇到一个问题,那就是通配符的使用。

问题:若不使用通配符,可以找到相对应的action,而使用通配符就会报错,找不到actionmapping之内的错,找不到action。

问题原因: struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:<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>

<package name="default" namespace="/" extends="struts-default">
   <global-allowed-methods>regex:.*</global-allowed-methods>
 <action name="helloworld" class="com.imooc.action.HelloWorldAction">
      <result>/result.jsp</result>
      <result name="add">/add.jsp</result>
      <result name="update">/update.jsp</result>
 </action>
 
</package>
 
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
 
</struts>

1.首先,注意头部信息,这个应该是用来指定文件中允许使用那些标签。
  <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
2.加上下面这句。
  <global-allowed-methods>regex:.*</global-allowed-methods>
 或者(不加上面这句),在action中加上指定允许调用的方法的语句:
  <allowed-methods>login,logout</allowed-methods>
  
  
原文地址:https://www.cnblogs.com/jasonlixuetao/p/5933671.html