我正在尝试开发一个 Struts2 应用程序,其中在单击超链接时调用一个操作,该超链接使用 Struts 操作映射将用户定向到 hello.jsp。我收到以下错误:
HTTP Status 404 - No result defined for action com.manaar.action.HelloAction and result success
我的文件如下。我的映射看起来是有序的。我还检查了这里的其他帖子,但似乎找不到此问题的原因或解决方案。非常感谢任何建议。非常感谢,J
index.jsp
:
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title><s:text name="app.title" /></title>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
</head>
<body>
<center>
<h2>
Struts 2 Actions
</h2>
<br>
<br>
Welcome
<s:property value="#session.user" default="Guest" />!
<s:if test="#session.user!=null">
<s:url id="logout" action="logout" />
| <s:a href="%{logout}">Logout</s:a> |
</s:if>
<br>
<table cellspacing="5" width="180">
<tr bgcolor="#f0edd9" height="25" align="center">
<td>
<s:url id="hello" action="hello"/>
<s:a href="%{hello}">Hello Action</s:a>
</td>
</tr>
<tr bgcolor="#f0edd9" height="25" align="center">
<td>
<s:a href="add_user.jsp">Add User</s:a>
</td>
</tr>
<tr bgcolor="#f0edd9" height="25" align="center">
<td>
<s:a href="user.jsp">View Users</s:a>
</td>
</tr>
<tr bgcolor="#f0edd9" height="25" align="center">
<td>
<s:a href="login.jsp">Login</s:a>
</td>
</tr>
</table>
</center>
</body>
</html>
struts.xml
:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="hello" class="com.manaar.action.HelloAction" method="wateva">
<result name="success">/hello.jsp</result>
</action>
</package>
HelloAction.java
:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.manaar.action;
import com.opensymphony.xwork2.Action;
import static com.opensymphony.xwork2.Action.SUCCESS;
public class HelloAction implements Action {
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
*
* @return
* @throws Exception
*/
@Override
public String execute() throws Exception {
setMessage("Hello From Struts!");
return SUCCESS;
}
}
最佳答案
您可以使用 config-browser plugin 。如果您想查看浏览器中的配置以及操作如何映射到 URL,这非常有用。
实际上,问题的原因是您使用了 convention-plugin 。如果将 struts2-convention-plugin-2.3.x.jar
放入 WEB-INF/lib
中,则会使用它。安装后,它会扫描 struts-plugin.xml
中定义的包,并按照约定创建一个附加的 struts.xml
配置。除了您的操作符合插件使用的规则外,还会为 HelloAction
类创建操作 "hello"
,但不幸的是它没有结果 "success “
。要将此结果添加到操作中,您应该在类上使用 @Result
注释,或使用 @ResultPath
注释来指定结果的路径(而不是默认路径) WEB-INF/内容
。如果您应用 struts.convention.result.path
配置设置,也可以完成相同的操作。
@Result(name = SUCCESS, location = "/hello.jsp")
另请注意,您在 struts.xml
中为操作 "hello"
定义的映射意义不大,除非它映射到指定的方法。 JSP 的名称可能是 index.jsp
的拼写错误。
关于java - Struts 2 - HTTP 状态 404 - 没有为操作定义结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757947/