首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Struts2命名空间的有关问题

2012-05-29 
Struts2命名空间的问题struts.xmlXML codepackage namestruts2 extendsstruts-default namespace

Struts2命名空间的问题
struts.xml

XML code
<package name="struts2" extends="struts-default" namespace="/user">        <action name="index" class="loginAction">            <result name="input">/index.jsp</result>            <result name="success">/result.jsp</result>            <result name="fail">/index.jsp</result>        </action>    </package>

index.jsp
HTML code
<s:form action="user/index.action" method="post">        <s:textfield name="username" label="username"></s:textfield>        <s:password name="password" label="password"></s:password>        <s:submit></s:submit>    </s:form>


路径:http://localhost:8080/Struts2/index.jsp
当我第一次输入错误的用户名和密码的时候回自动返回到登录界面,这时连接地址为:
http://localhost:8080/Struts2/user/index.action
其中user为我定义的命名空间
这时候我再次用正确的用户名和密码登录的时候,会报如下错误
HTML code
Struts Problem ReportStruts has detected an unhandled exception: Messages: There is no Action mapped for namespace /user/user and action name index.  --------------------------------------------StacktracesThere is no Action mapped for namespace /user/user and action name index. - [unknown location]     com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:475)    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)    java.lang.Thread.run(Thread.java:619)    --------------------------------------------You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set:   struts.devMode=falsein your WEB-INF/classes/struts.properties file.  


这时候的连接地址为:http://localhost:8080/Struts2/user/user/index.action
不明白为啥命名空间会自动添加。
如果在form标签里面单独配置的话又正常了。
<s:form action="index" method="post" namespace="/user">
不明白为啥不能卸载action中。
<s:form action="user/index" method="post">错误
<s:form action="index" method="post" namespace="/user">正确
是不是命名空间不能卸载form的action中?
有做过的朋友,帮忙看看。



[解决办法]
正确的使用方法是 

<s:form action="index" method="post" namespace="/user">

或者

<form action="/user/index" method="post">
[解决办法]
第一种

<form action="index.action" >

.....

</form>

第二种

<form action="/user/index.action" >

.....

</form>

第三种

<s:form action="index" namespace="/user" >

.....

</s:form>

第一种写法可以通过测试,原因是FilterDispatcher会去package的根目录及下面的子目录去搜index这个action

第二种写法是正规写法,可以通过测试,因为FilterDispatcher把form的跳转分解为index.action和nanespace=/user,然后在package下直接命中namespace为"/user"的那个包。

第三种写法,能通过测试,原因因为你已指定了namespace,而且struts2会默认的给你的action加上.action

热点排行