Struts2.5的配置的一些注意事项
- 所需jar包
注意事项:
以上jar包是根据官方的示例添加的,但官方还多加了一个struts2-rest-plugin-2.5.14.1.jar,笔者加进去后会出现错误,导致项目无法运行action。
当出现Wrong method was defined as an action method: index - Class: java.lang.Class File: Class.java Method: getMethod Line: 1786 - java/lang/Class.java:1786:-1
这个错误并且action的配置没有错误时,极有可能是jar包的问题 - 配置文件
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.i18n.encoding" value="UTF-8"></constant>
<!-- 指定需要Struts2处理的请求后缀 -->
<constant name="struts.action.extension" value="do,action"></constant>
<!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->
<constant name="struts.serve.static.browserCache" value="false"></constant>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- action全部用注解进行配置 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 是否开启动态方法调用 -->
<!-- 示例-->
<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="error">/JSP/error.jsp</result>
</global-results>
<global-allowed-methods>login</global-allowed-methods>
<action name="login" class="com.mdy.action.Login" method="login">
<result name="success">/JSP/add_student.jsp</result>
<result name="login_error">/JSP/login.jsp</result>
</action>
</package>
struts.xml在2.5版本需要在调用action的时候对action的方法配置
<global-allowed-methods>login</global-allowed-methods>
web.xml则需要配置struts2的过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
与之前版本有所不同的是它的包名,如果是使用eclipse的话,可以通过按住ctrl将鼠标移到filter-class标签里面的包名,若出现横线,则说明包名正确