我的母语不是英语,所以如果我犯了一些惯用错误,我深表歉意。我是 Struts2 的新手。 我开发了一个 Web 项目,其中包含 3 个 JSP 页面、一个部署描述符 (web.xml)、一个 struts 配置文件 (struts.xml),其中配置了 2 个 Action (一个名为 index 的 Action 和一个名为 welcome 的 Action )和 1 个实现的类欢迎 Action 的 Action 逻辑。当我尝试 index 操作时,它工作得很好,但是如果我在 URL (welcome.action) 中调用欢迎操作,我会收到以下错误
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
No result defined for action actions.WelcomeAction and result success
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
...
现在我给大家详细介绍一下项目和环境:
开发环境
- 操作系统:Mac OS X 10.10.3
- JRE:1.8.0_20
- IDE:NetBeans 7.4
- 应用服务器:GlassFish 4.0
- 服务器位置:localhost:8080
- 上下文路径:/AppStruts2Example
- URL 项目:http://localhost:8080/AppStruts2Example/
Web 项目开发信息
- Java EE 版本:7
- Struts2 版本:2.3.15
代码描述如下
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="index">
<result>/WelcomeFromIndexAction.jsp</result>
</action>
<action name="welcome" class="actions.WelcomeAction">
<result name="success">/WelcomeFromWelcomeAction.jsp</result>
</action>
</package>
</struts>
actions.WelcomeAction.java
package actions;
import com.opensymphony.xwork2.ActionSupport;
public class WelcomeAction extends ActionSupport{
@Override
public String execute(){
System.out.println("Inside of execute method in WelcomeAction.");
System.out.println("Result: " + SUCCESS);
return SUCCESS;
}
}
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index JSP</title>
</head>
<body>
<h1>Welcome to index.jsp!</h1>
</body>
</html>
WelcomeFromIndexAction.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Action</title>
</head>
<body>
<h1>Welcome From Index Action</h1>
<br />
<h3>Current Page: WelcomeFromIndexAction.jsp</h3>
</body>
</html>
WelcomeFromWelcomeAction.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Action</title>
</head>
<body>
<h1>Welcome From Welcome Action</h1>
<br />
<h3>Current Page: WelcomeFromWelcomeAction.jsp</h3>
</body>
</html>
项目在以下情况下工作正常:
- 我在浏览器上直接写任何 JSP 的 URL
- 我调用索引操作,如下所示:/index.action 或/index -> 都将我重定向到 WelcomeFromIndexAction.jsp
项目失败时:
- 我调用了欢迎操作,如下所示:/welcome.action 或/welcome -> 都向我显示了一个包含 Struts 问题报告的网页,如上所述
我认为这可能是 GlassFish Server 的问题,所以我在虚拟机上安装了 Debian,并在那里安装了 Tomcat 7。我的下一步是使用 NetBeans 的“清理和构建”部署 Web 应用程序。它为我生成了一个部署到 Tomcat 7 中的 WAR 文件,但我遇到了同样的问题。在这令人失望之后,我禁用了 Debian VM (Tomcat 7),然后我回来使用 GlassFish 4.0
正如您在 WelcomeAction.java 中看到的,它在执行方法中有一个 println 语句。当我在浏览器上调用欢迎操作时,我可以看到 Struts 有效地调用了那个方法,这些行被打印在输出上,并且在这个调用之后,Struts2 必须处理结果然后显示错误。这证实了 Struts2 没有找到结果而不是 Action ,但我不知道为什么。
我尝试使用新旧版本的过滤器 (FilterDispatcher/StrutsPrepareAndExecuteFilter) 配置过滤器调度程序。我遇到了同样的问题。 我尝试在 struts.xml 中配置与索引操作完全相同的欢迎操作(实际上是默认设置),但也不起作用。只有索引操作运行良好。 我尝试以多种方式在 struts.xml 中配置欢迎操作,但没有任何效果。
所以,我在这里,需要有人帮助我。感谢您的回答。
注意:Stackoverflow 不允许我发布图片,因为我是新手。所以,我上传了一个重要的打印屏幕到我的保管箱。 https://dl.dropboxusercontent.com/u/101502868/NetBeans%20Struts2%20Error.png 在此打印屏幕上,您可以看到添加的库、执行方法的执行、自定义打印行和 Struts 错误的开始。
谢谢。
最佳答案
在名为 editUser 的操作类中使用自定义方法在 struts.xml 中创建条目,以便控制直接转到该 perticuler 方法并根据您的逻辑处理结果
<action method="editUser" name="editUser" class="com.action.EditUserAction">
<result name="true">/jsp/editUser.jsp</result>
<result name="false">jsp/index.jsp</result>
</action>
并在你的类中添加自定义方法而不是覆盖执行方法
class EditUserAction extendes ActionSupport{
public String editUser(){
String status = "false";
/**
do your buisness logic
*/
return status;
}
}
如果问题仍然存在,请仔细检查您的配置
关于java - Struts 2 找不到从操作返回的成功结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30680433/