【问题标题】:There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts]没有为命名空间 [/] 和与上下文路径 [/struts] 关联的操作名称 [home] 映射的操作
【发布时间】:2016-07-06 07:25:07
【问题描述】:

希望有人能提示在哪里寻找问题的根源。

班级登录操作

enter code here

@Namespace("/")
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;

@Override
@Action(value = "/welcome", results = { @Result(name = SUCCESS, location =  "/WEB-INF/content/welcome.jsp") })

When I exectute my index.jsp to execute welcome.jsp didn't work.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"      "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>Login Page</title>
</head>
<body>
    <h3>Welcome to Struts 2</h3>
<s:form action="home">
    <s:textfield name="username" label="User Name"></s:textfield>
    <s:textfield name="password" label="Password" type="password">       </s:textfield>
    <s:submit value="Login"></s:submit>
</s:form>
</body>
</html>

welcome.jsp

enter code here

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=IUTF-8">
<title>Welcome To Struts 2</title>
</head>
<body>
   <h3>
        Welcome
       <s:property value="username"></s:property>
     !
   </h3>
</body>

结果 Struts 问题报告

Struts 检测到一个未处理的异常:

消息:

There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].

【问题讨论】:

  • 通过编辑问题而不是通过评论发布代码。此外,代码有不同的错误,而不是图像上的错误。你应该澄清这个问题。我确定问题的根源在于动作映射。
  • 请发布文字,而不是图片。
  • 还不行,sclv。谢谢。
  • 没有为命名空间 [/] 和动作名称 [home] 映射的动作

标签: java jsp configuration struts2 http-status-code-404


【解决方案1】:

在 Struts2 中,您使用 s:form 标签的 actionnamespace 属性将表单映射到操作配置。

通常,action 属性接受操作名称,但它可以包含 URL。 form 标记由模板呈现为 HTML form,其中 action 属性必须是 URL。

所以 Struts 正在获取动作名称并生成 URL。您可以在浏览器中查看源代码。当发出请求时,Struts2 使用请求 URI 来查找操作映射,如 here 所述。

通常 404 错误会导致服务器上缺少与请求的 URI 对应的操作配置。当您提交表单时,会发出请求并请求资源,但找不到操作配置,因此 Struts2 返回 404 错误。

如果您使用约定或其他插件,则不需要使用注释,但注释始终允许手动覆盖默认值。

默认结果路径为/WEB-INF/contents/,动作名称不带斜线,所以可以使用注解:

@Action(value = "welcome", results = @Result(location = "welcome.jsp") )

您可以从this 答案中找到对在线资源的引用。

教程read this tutorial中的那个。

【讨论】:

    【解决方案2】:

    解决方案。

    在我的 index.jsp 中我在标签 action 中写了“home”,form action="home" 正确的是 Action 处的同名。

    <html>
        <head>
             <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
         <title>Login Page</title>
        </head>
       <body>
           <h3>Welcome to Struts 2</h3>
           <s:form action="home"> **The correct is "welcome"**
           <s:textfield name="username" label="User Name"></s:textfield>
           <s:textfield name="password" label="Password" type="password">    </s:textfield>
           <s:submit value="Login"></s:submit>
         </s:form>
       </body>
    </html>
    

    在我的 LoginAction 中,我使用了 value ="/welcome"。

    package web.actions;
    
    @Action(value = "/welcome", results = { @Result(name = SUCCESS, location = "/WEB-INF/content/welcome.jsp") })
    
    public String execute() throws Exception {
        return SUCCESS;
    }
    

    信息很清楚。
    没有为与上下文路径 [/struts] 关联的命名空间 [/] 和动作名称 [home] 映射的动作。

    但我只是做了很多例子就看到了错误。

    【讨论】:

    • 通常在 SO 上,问题的代码错误,而答案包含正确的代码。但是我认为即使在您声明其正确性的 cmets 中,您也没有写出正确的。
    • 这个答案看起来像是对问题的编辑。请区分答案和为作者提供信用的问题。
    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多