引入Strust2后jsp无法调用action报错404的问题

在使用Struts2进行开发时,遇到jsp调用action出现404错误的问题。检查发现,Struts配置正确,但jsp中的form action缺少了.action扩展名。解决方法是将form的action由'writeExcel_write'修改为'writeExcel_write.do',以匹配web.xml中设置的<url-pattern>*.do</url-pattern>。同时讨论了web.xml中不同的url-pattern配置方式对大型项目的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在写代码的时候,想引入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。

希望以后不要犯这种低级错误了。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值