最近在学习struts2,写了个小demo,web.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>samples</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" namespace="" extends="struts-default">
<global-results>
<result name="login" type="redirect">/login.jsp</result>
</global-results>
<action name="userLogin" class="hs.homs.action.UserLoginAction">
<result name="success" type="redirect">/entrust.action</result>
<result name="error">/login.jsp</result>
</action>
<action name="entrust" class="hs.homs.action.EntrustAction">
<result name="success">main.jsp</result>
<result name="add">main.jsp</result>
</action>
<action name="entrust_add" method="add" class="hs.homs.action.EntrustAction">
<result name="success">main.jsp</result>
</action>
</package>
</struts>
代码在eclipse上运行正常,运行后url是:http://localhost/demo2/login.jsp,一个简单的登录界面,输入用户名密码能正常登录到内页,url都是http://localhost/demo2/ 开头。
问题出在部署后,部署目录结构:

其中,web.xml放在WEB-INF目录,struts.xml放在build/classes目录
运行后访问的url是http://localhost/login.jsp,能正常打开,点提交时报错:
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [userLogin] associated with context path [].
发布后的server.xml文件(host节点):
<Host name="localhost" appBase="E:/JavaProjects/demo2_publish"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
在eclipse上运行,是在 一个 Tomcat v7.0 Server at localhost上运行,我看了下它自己也有个server.xml文件:
<Host appBase="E:/JavaProjects" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="demo2" path="/demo2" reloadable="true" source="org.eclipse.jst.jee.server:demo2"/>
</Host>
运行后我希望打开的地址不带demo2但一直弄不好,求解?
java j2ee struts2
------解决方案--------------------
提交到action的时候命名空间的错误、。
------解决方案--------------------
namespace="/"
------解决方案--------------------
你改成空或者给他一个命名。然后再form标签里的时候加上命名空间。
------解决方案--------------------
<s:form action="action" namespace="/命名">
------解决方案--------------------