Struts 2的位置

时间:2016-03-19 14:39:10

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

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

班级登录操作

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

2 个答案:

答案 0 :(得分:0)

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

通常,action属性接受操作名称,但它可以包含URL。表单标签由模板呈现为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中的那个。

答案 1 :(得分:-1)

解决方案。

在我的index.jsp中,我在标记操作中写了“home”,表单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]。

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