我尝试编写以下简单的struts但在运行时遇到此错误。
[org.apache.struts2.dispatcher.Dispatcher] Could not find action or result:
No result defined for action com.peter.action.LoginAction and result success
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>Hello Struts</h2>
<s:form action="login" >
<s:textfield name="username" label="Username:" />
<s:password name="password" label="Password:"/>
<s:submit />
</s:form>
</body>
</html>
LoginAction.java
package com.peter.action;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Action;
import com.opensymphony.xwork2.ActionSupport;
@ResultPath("/")
public class LoginAction extends ActionSupport {
private String username;
private String password;
@Override
@Action(value = "login", results =
{@Result(name="success", location="welcome.jsp")})
public String execute() {
return SUCCESS;
}
// Getters and setters for username, password
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Struts</display-name>
<filter>
<filter-name>Struts_Filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.peter.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Struts_Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
除运行时错误外,还存在部署错误
错误[com.opensymphony.xwork2.util.finder.ClassFinder](MSC服务 线程1-2)无法阅读课程 [WEB-INF.classes.com.peter.action.LoginAction]:无法加载 WEB-INF / classes / com / peter / action / LoginAction.class - [未知 位置] com.opensymphony.xwork2.util.finder.ClassFinder.readClassDef(ClassFinder.java:785) [XWork的核 - 2.3.1.2.jar:2.3.1.2]
AFAIK,struts的扫描方法将扫描名为struts2的默认包,用于任何带注释的类,但我已指示struts2使用init-param在com.peter.action中进行扫描但仍无法找到该类。这很奇怪。
答案 0 :(得分:0)
升级到Struts 2.3.8时遇到了同样的问题,除了以WEB-INF.*
开头的错误类包,它以WAR文件的名称开头(表示为WarFileName
)。我通过将以下行添加到struts.xml
来解决它:
<constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*,WarFileName.*"/>
除WarFileName.*
之外的所有内容都是默认设置,因此我也包含了这些内容。我希望这有助于某人。
答案 1 :(得分:-1)
我通过更改为xml映射样式解决了问题。