struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!

本文主要是介绍struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先说 struts2.5 使用感叹号实现动态方法调用的问题,使用通配符的方式出现的问题跟这一样!


在 struts2.3 的版本中,我们通常这么配置 struts.xml 文件:


<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>

<package name="default" namespace="/" extends="struts-default">
      <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>


HelloWorldAction类如下:


package com.imooc.action;


import com.opensymphony.xwork2.ActionSupport;


public class HelloWorldAction extends ActionSupport {

public String add(){

return "add";
}

public String update(){

return "update";
}

@Override
public String execute() throws Exception {

System.out.println("执行Action");

return SUCCESS;
}

}

这样在 struts2.3 上能够成功,但在 struts2.5 上却无论如何定位不到 http://localhost:8888/HelloWorld/helloworld!add.action,会发生如下错误:

HTTP Status 404 - Method add for action helloworld is not allowed!


type Status report

message Method add for action helloworld is not allowed!

description The requested resource is not available.


Apache Tomcat/7.0.70


搜了半天,有说是要添加 <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>,明确打开动态方法调用功能,因为 struts2 默认是关的,但在这里并不能解决问题,因为配置是正确的。


原来是 struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:<global-allowed-methods>regex:.*</global-allowed-methods>

同时要注意,struts.xml 的解析版本要为 2.5,即头部信息应为:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
 

最后,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>

<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>


同样,使用通配符实现动态方法调用时,

<?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_*" method="{1}" class="com.imooc.action.HelloWorldAction">
      <result>/result.jsp</result>
      <result name="add">/{1}.jsp</result>
      <result name="update">/update.jsp</result>
 </action>
 
</package>
 
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
 
</struts>

这篇关于struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/644207

相关文章

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖

resultMap如何处理复杂映射问题

《resultMap如何处理复杂映射问题》:本文主要介绍resultMap如何处理复杂映射问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录resultMap复杂映射问题Ⅰ 多对一查询:学生——老师Ⅱ 一对多查询:老师——学生总结resultMap复杂映射问题

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效