JSP 没有为与上下文路径[/struts]关联的命名空间[/]和操作名称[home]Map的操作

5hcedyr0  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(107)

希望有人能给给予一个提示,在哪里寻找问题的根源。

班级登录操作

@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].

utugiqy6

utugiqy61#

在Struts2中,使用s:form标记的actionnamespace属性将表单Map到操作配置。
通常,action属性接受action名称,但它可以包含URL。模板将表单标记呈现为HTML form,其中action属性必须是URL。
因此,Struts获取了action名称并生成了URL。您可以在浏览器中看到源代码。当发出请求时,Struts2使用请求URI来查找actionMap,如here所解释的。
通常,404错误会导致服务器上缺少与请求的URI对应的操作配置。当您提交表单时,会发出请求并请求资源,但找不到操作配置,因此Struts2返回404错误。
如果你正在使用约定或rest插件,你不需要使用注解,但是注解总是允许手动覆盖默认值。
默认情况下,结果路径是/WEB-INF/contents/,动作名称没有斜杠,所以你可以使用annotation:

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

字符串
你可以从this answer找到在线资源的参考。
教程read this tutorial中的一个。

yrdbyhpb

yrdbyhpb2#

解决方案

在我的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]Map的操作。
但我只是看到了错误做了很多例子。

相关问题