我收到了这个错误:
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [loginForm] associated with context path
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</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="default" extends="struts-default">
<action name="loginForm" class="org.nitish.action.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="failure">index.jsp</result>
</action>
</package>
<bean name="loginForm" class="org.nitish.form.LoginForm"></bean>
<constant name="struts.devMode" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />
</struts>
index.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login page | Hello World Struts application in Eclipse</title>
</head>
<body>
<h1>Login</h1>
<s:form action="loginForm">
<s:textfield name="userName" label="Username" />
<s:password name="password" label="Password" />
<s:submit />
</s:form>
</body>
</html>
文件夹结构为:
修改
行动代码:
public class LoginAction extends ActionSupport {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String target = null;
LoginForm loginForm = (LoginForm)form;
if(loginForm.getUserName().equals("admin")
&& loginForm.getPassword().equals("admin123")) {
target = "success";
request.setAttribute("message", loginForm.getPassword());
}
else {
target = "failure";
}
return mapping.findForward(target);
}
}
答案 0 :(得分:0)
namespace="/"
文件
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="loginForm" class="org.nitish.action.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="failure">index.jsp</result>
</action>
</package>
答案 1 :(得分:0)
根据struts dtd,配置文件中元素的顺序很重要。尝试
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
<bean name="loginForm" class="org.nitish.form.LoginForm"></bean>
<package name="default" extends="struts-default">
<action name="loginForm" class="org.nitish.action.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="failure">index.jsp</result>
</action>
</package>
</struts>
选择完全命名空间的选项恢复为默认值,没有理由更改此常量。
并删除
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
struts无法找到操作,因为Web服务器首先提供了欢迎文件。
现在,编辑struts.xml
并在包中添加以下内容
<action name="">
<result type="redirectAction">loginForm</result>
</action>
因此,当您转到Web应用程序根路径时,它将执行默认操作并重定向到操作loginForm
。