今天在写代码的时候,想引入Strust写个导出个excel文件,但是在jsp写好后出现了404问题。
明显的action方法写错了,我的jsp是这么写的:
<form action="writeExcel_write" method="post">
<input type="submit" value="导出为xml文件"/>
</form>
代码简单得一批。。。简单到我无法下手。。。
先看看我的Strust的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.action.extension" value="do"/>
<package name="struts2" extends="struts-default" namespace="/">
<action name="*_write" class="com.gxuwz.medical.web.serviceImpl.WriteExcelAction"
method="{1}" />
</package>
</struts>
似乎没什么问题,而且我的web.xml的配置是这样的:
<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>*.do</url-pattern>
</filter-mapping>
发现我<url-pattern>*.do</url-pattern>里面是*.do,
并且strust里定义的是
<constant name="struts.action.extension" value="do"/>这个很重要,一般很多人都会漏掉这个!!!
那么问题就是出在jsp的action 里了
<form action="writeExcel_write" method="post">
<input type="submit" value="导出为xml文件"/>
</form>
这里的 <form action="writeExcel_write" method="post">应该改为, <form action="writeExcel_write.do" method="post">
因为在web.xml里的设置是*.do,所以这里应该对应。
顺带一提的是,web.xml文件里的strust配置有的是*/,有的是*.do,还有的是*.action,个人认为后两个比较规范,第一种不适用于大型的设计,会拦截到不需要的action。
希望以后不要犯这种低级错误了。。。。