关于Struts2 UI标签<s:form>namespace配置问题

以下言论纯个人观点,若有不妥敬请指出,谢谢。


在把html的UI标签form换成struts2的标签<s:form>过程中,我加了namespace="/login",action="LoginAction_home",

原来的HTML标签:

<form id="form1" name="form1" action="/login/LoginAction_home.do" target="_parent" method="post">

换成Struts2标签后:

<s:form id="form1" name="form1" namespace="/login" action="LoginAction_home.do" target="_parent" method="post">	

运行后发现,提示无法找到action:There is no Action mapped for namespace [/] and action name [LoginAction_home] associated with context path [/Struts2_StaffManagementF]

哎,没理由啊,我明明web.xml里struts2的filter配置了过滤.do:

<filter>
    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
struts.xml文件里面也配好了namespace和action:
<struts>
	<constant name="struts.action.extension" value="do"></constant>
	<constant name="struts.devMode" value="true"></constant>
	<constant name="struts.ui.theme" value="simple"></constant>
	
	<package name="login" namespace="/login" extends="struts-default">
		<action name="LoginAction_home" class="com.yi.action.LoginAction" method="home">
			<result name="home">/login/home.jsp</result>
			<result name="input">/login/login.jsp</result>
		</action>
	</package>
</struts>
查看login.jsp源代码发现form的action属性里没有namespace:
<form id="form1" name="form1" action="LoginAction_home.do" target="_parent" method="post">	
那么问题来了,我明明加了namespace,为什么在运行后namespace会没有?

经过一顿摸索、实验和百度,总结如下:

当struts2里<s:form>标签里action属性值加了后缀如".do"或者".action"后,struts2将不会自动给action添加namespace值。

将action后缀去掉后:运行成功

<s:form id="form1" name="form1" namespace="/login" action="LoginAction_home" target="_parent" method="post">

总结:

在写form标签时:

如果用的是html的form标签,action属性值里一定要加上namespace对应的值;

如果用的是struts2的s:form标签:

如果加了namespace属性,则必须在action属性值里添加namespace(action属性值有无后缀名都行);

如果加了namespace属性,action属性的值里必须去掉后缀名,如".do",".action",否则页面在提交后会提示找不到该action



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值