Struts2 访问出错 :http 500
Struts2 访问出错 :http 500
错误信息:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:432)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.
Apache Tomcat/7.0.37
错误原因:
1. 原因:struts2 的默认配置中,struts2会拦截所有的请求,包括各种后缀,因此,当访问一个不存在的目录时(即没有明确给出一个文件及其后缀名),struts2 会将其匹配为一个命名空间,然后在struts.xml 中查找对应的 action。如果你的struts2 的处理后缀没有改动,那么如果该命名空间(也即前面访问的目录)中正好没有配置默认的action,则出现 http 500的错误,而不是 404 错误,因为struts2 工厂找不到 action去构造,出现空指针异常。
解决方法:
1. 方法一:
Strut2 的配置文件 struts.xml 中,加入:
<constant name="struts.action.extension" value="action,do"></constant>
即将struts2 的处理后缀改为 action和do (当然可以改为其他)。这样的话,当访问一个不存在的路径(目录)时,struts2 将不会认为其是一个命名空间,也不会去寻找这个命名空间中的默认 action,那么就不会出现该问题。
问题解决。
2. 方法二:
在struts2的配置文件struts.xml中的默认包下,配置一个默认action:
<!-- 默认action -->
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/index.jsp</result>
</action>
即可。如上,这个 action 其类型为默认的 ActionSupport,跳转到应用首页。