解决java struts 2.5 以上不能访问的问题
关于动态方法调用,自从Struts2.3之后DMI的就默认被约束了。只要没有被方法属性(包括通配符)和<allowe-methods>标签明确允许的方法,Struts都会拒绝。也就是说要使用DMI的话,需要将调用的方法加入<allowed-methods>标签中(多个方法用逗号分隔)并且将enable.DynamicMethodInvocation的值设置true"。如果有add和update两个方法则可这样写:
<allowed-methods>add,update</allowed-methods> <constant name="enable.DynamicMethodInvocation" value="true"/>
constant标签放在package标签外,<allowed-methods>放在action标签内
本人解决
1、struts.xml配置为
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="user" extends="struts-default">
<action name="userAction" class="com.struts.UserAction">
<result name="add">/user_add.jsp</result>
<result name="update">/user_update.jsp</result>
</action>
</package>
</struts>
2、web.xml 的配置
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、源文件 com.struts.UserAction.java
package com.struts; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private String info; public String add(){ setInfo("添加"); return "success"; } public String update(){ setInfo("修改"); return "update"; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String execute() { System.out.println("execute demo"); return "success"; }
}
4、在视图层添加 user_add.jsp user_update.jsp 文件
5、 localhost:8080/web/userAction!add userAction!1update 就是访问

浙公网安备 33010602011771号