我試圖編碼以下簡單的struts,但在運行時遇到此錯誤。org.apache.struts2.dispatcher.Dispatcher:無法找到操作或結果錯誤
[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>
除了運行時錯誤,有部署錯誤,是
ERROR [com.opensymphony.xwork2.util.finder.ClassFinder](MSC服務 螺紋1-2)無法讀取類 [WEB-INF.classes.com.peter.action.LoginAction]:可能不加載 WEB-INF/classes/com/peter/action/LoginAction.class - [unknown location] at com.opensymphony.xwork2.util.finder.ClassFinder.readClassDef(ClassFinder.java:785) [xwork- core-2.3.1.2.jar:2.3.1.2]
AFAIK,struts的掃描方法將掃描名爲struts2的默認包,用於任何帶註釋的類,但我有instruc ted struts2使用init-param在com.peter.action中掃描,但仍然無法找到該類。這很奇怪。
它說它無法讀取類 - 是否部署? –
當然要檢查是否有班級。將來添加struts2-config-browser jar,然後轉到/ config-browser/index並查看它告訴你什麼。同樣,只有在struts2-conventions插件存在的情況下(我認爲它應該用於大多數項目和個人所有項目),您的關於包掃描的聲明才適用。 – Quaternion
LoginAction.class位於jboss部署文件夾中。什麼是struts2-config-browser的用法。我已經將struts2-convention-plugin包含到了classpath中。任何人都可以幫忙 – peterwkc