>希望有人能给出一个提示,在哪里寻找问题的根源。
类登录操作
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.
索引.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>
欢迎.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 检测到未处理的异常:
消息:
There is no Action mapped for namespace [/] and action name [home] associated with context path [/struts].
在 Struts2 中,您可以使用标签的action
和namespace
属性将表单映射到s:form
操作配置。
属性接受操作名称,但它可以包含 URL。表单标记由模板呈现为 HTML form
其中操作属性必须是 URL。
因此,Struts正在获取操作名称并生成URL。您可以在浏览器中查看源代码。发出请求时,Struts2 使用请求 URI 来查找操作映射,如此处所述。
通常,404 错误会导致与请求的 URI 对应的服务器上缺少操作配置。当您提交表单时,将发出请求并请求资源,但找不到操作配置,因此 Struts2 返回 404 错误。
如果您使用的是约定或 rest 插件,则不需要使用注释,但注释始终允许手动覆盖默认值。
默认情况下,结果路径/WEB-INF/contents/
,操作名称不带斜杠,因此您可以使用注释:
@Action(value = "welcome", results = @Result(location = "welcome.jsp") )
您可以从此答案中找到对在线资源的引用。
教程中的那个阅读本教程。
解决方案。
在我的索引中.jsp我在标签操作中写了"home",表单 action="home" 正确的是操作中相同的名称。
<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>
在我的登录操作中,我使用了值 ="/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] 映射的操作。
但我只是看到错误做了很多例子。