java Struts 2 - HTTP 状态 404 - 没有为操作定义结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17757947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Struts 2 - HTTP Status 404 - No result defined for action
提问by jay tai
I'm trying to develop a Struts2 app where an action is invoked upon clicking a hyperlink which directs the user to a hello.jsp using Struts action mapping. I'm getting the following error:
我正在尝试开发一个 Struts2 应用程序,在单击超链接时调用一个操作,该超链接使用 Struts 操作映射将用户定向到 hello.jsp。我收到以下错误:
HTTP Status 404 - No result defined for action com.manaar.action.HelloAction and result success
My files are as follows. My mapping looks like it's in order. I also checked other postings on here but can't seem to find the cause or solution to this problem. Would really appreciate any advice. Many thanks, J
我的文件如下。我的映射看起来是有序的。我还检查了此处的其他帖子,但似乎无法找到此问题的原因或解决方案。真的很感激任何建议。非常感谢,J
index.jsp
:
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
:
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
:
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;
}
}
回答by Roman C
You could use a config-browser plugin. It's useful if you want to see the configuration in the browser and how actions are mapped to the URLs.
您可以使用配置浏览器插件。如果您想查看浏览器中的配置以及操作如何映射到 URL,这将非常有用。
Actually, the cause of the problem that you use a convention-plugin. It's used if you put struts2-convention-plugin-2.3.x.jar
into WEB-INF/lib
. When installed it scans the packages, defined in the struts-plugin.xml
and creates an additional to struts.xml
configuration by conventions. As well as your action is comply the rules used by the plugin the action "hello"
is created for the class HelloAction
but unfortunately it has no a result "success"
. To add this result to the action you should use @Result
annotation on the class, or use @ResultPath
annotation to specify the path to results where they could be located instead of default WEB-INF/content
. The same could be done if you apply struts.convention.result.path
configuration settings.
实际上,问题的原因是您使用了约定插件。如果你把它使用struts2-convention-plugin-2.3.x.jar
到WEB-INF/lib
。安装后,它会扫描在 中定义的包,struts-plugin.xml
并struts.xml
按照约定创建附加配置。除了您的操作符合插件使用的规则外,该操作"hello"
是为类创建的,HelloAction
但不幸的是它没有结果"success"
。要将这个结果添加到操作中,您应该@Result
在类上使用注释,或者使用@ResultPath
注释来指定结果的路径,而不是 default WEB-INF/content
。如果您应用struts.convention.result.path
配置设置,也可以这样做。
@Result(name = SUCCESS, location = "/hello.jsp")
Also note, that the mapping you defined in the struts.xml
for the action "hello"
has less meaning unless it mapped to the method specified. And name of the JSP supposed a typo for index.jsp
.
另请注意,除非映射到指定的方法,否则您在struts.xml
for 操作中定义的映射"hello"
意义不大。并且 JSP 的名称假定index.jsp
.
回答by Ankur Lathi
I think you missed to write the method "wateva" in HelloAction. So either write it in place of execute or remove it from your struts mapping.
我认为您没有在 HelloAction 中编写方法“wateva”。所以要么写它代替执行,要么从你的 struts 映射中删除它。
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="hello" class="com.manaar.action.HelloAction">
<result name="success">/hello.jsp</result>
</action>
</package>