【Struts2文件下载】struts.xml文件中配置action,访问路径问题

struts.xml文件中配置action,访问路径问题。

struts.xml访问路径问题一:

注意(重点):namespace可以写为/,或者/a,或者/a/b,对应的action访问路径为/download.action,/a/download.action,或者/a/b/download.action
名称空间=/a/b/c/d     
        名称空间=/a/b/c
        名称空间=/a/b
        名称空间=/a
        名称空间=/(默认)

一):namespace决定了action的访问路径,默认为"",可以接受所有路径的action
第一种方式配置struts.xml文件如下:
<struts>
	<!--没有配置namespace属性,默认可以接受所有的action的访问-->
	<package name="download" extends="struts-default">
		<!--指定action的名字-->
		<action name="download" class="cn.lsh.web.struts2.download.DownloadAction" method="downloadMethod">
			<result name="success" type="dispatcher">/WEB-INF/message.jsp</result>
			
		</action>		
	</package>	
</struts>

例如:我新建的web工程名称为struts2,
在cn.lsh.web.struts2.download.DownloadAction包下:新建的action为DownloadAction,
那么当你部署到tomcat服务器上时,在浏览器地址栏中访问的路径:
http://localhost:8080/struts/download ---->ok
http://localhost:8080/struts/a/download ---->ok 
http://localhost:8080/struts/a/b/download ---->ok
http://localhost:8080/struts/a/b/c/download ---->ok


第二种方式配置struts.xml文件如下:
<struts>
	<!--没有配置namespace属性为naspace="/",同样和第一种方式一样都可以接受所有的action的访问-->
	<package name="download" naspace="/" extends="struts-default">
		<!--指定action的名字-->
		<action name="download" class="cn.lsh.web.struts2.download.DownloadAction" method="downloadMethod">
			<result name="success" type="dispatcher">/WEB-INF/message.jsp</result>
			
		</action>		
	</package>	
</struts>

那么当你部署到tomcat服务器上时,在浏览器地址栏中访问的路径:
http://localhost:8080/struts/download ---->ok(测试通过没问题)
http://localhost:8080/struts/a/download ---->ok(测试通过没问题)
http://localhost:8080/struts/a/b/download ---->ok(测试通过没问题)
http://localhost:8080/struts/a/b/c/download ---->ok(测试通过没问题)

二)struts.xml文件如下:
<struts>
	<!--指定了配置namespace属性为:namespace="/aa"-->
	<package name="download" namespace="/aa" extends="struts-default">
		<!--指定action的名字-->
		<action name="download" class="cn.lsh.web.struts2.download.DownloadAction" method="downloadMethod">
			<result name="success" type="dispatcher">/WEB-INF/message.jsp</result>
			
		</action>		
	</package>	
</struts>

例如:我新建的web工程名称为struts2,
在cn.lsh.web.struts2.download.DownloadAction包下:新建的action为DownloadAction,
那么当你部署到tomcat服务器上时,在浏览器地址栏中访问的路径:
http://localhost:8080/struts/download ---->ok(测试通过没问题)
http://localhost:8080/struts/aa/download ---->ok (测试通过没问题)


struts.xml访问路径问题二:
1):例如我在struts.xml中配置了<constant name="struts.action.extension" value="action"/>,
那么在浏览器地址栏中访问的路径必须是:http://localhost:8080/struts/download.action.
如果在浏览器地址栏中访问的路径是:http://localhost:8080/struts/download 那么则会报404错误。

2):如果没有在struts.xml中配置<constant name="struts.action.extension" value="action"/>,
那么(.action)是可以默认不写,在浏览器地址栏中访问的路径可以是:http://localhost:8080/struts/download , 也可以是http://localhost:8080/struts/download.action.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值