以下言论纯个人观点,若有不妥敬请指出,谢谢。
在把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