我尝试编写以下简单的 struts 代码,但在运行时遇到此错误。
[org.apache.struts2.dispatcher.Dispatcher] Could not find action or result:
No result defined for action com.peter.action.LoginAction and result success
索引.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>
登录操作.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-core-2.3.1.2.jar:2.3.1.2 ]
AFAIK,struts 的扫描方法将扫描名为 struts2 的默认包以查找任何带注释的类,但我已指示 struts2 使用 init-param 扫描 com.peter.action 但仍然无法找到该类。这很奇怪。