org.apache.struts2.dispatcher.Dispatcher: Could not find action or result Error
- by peterwkc
i tried to code the following simple struts but encounter this error during run time.
[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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts Tutorial</title>
</head>
<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.Namespace;
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;
/**
* @author nicholas_tse
*
*/
//@Namespace("/") To define URL namespace
@ResultPath("/") // To instruct Struts where to search result page(jsp)
public class LoginAction extends ActionSupport {
private String username, password;
/**
*
*/
private static final long serialVersionUID = -8992836566328180883L;
/**
*
*/
public LoginAction() {
// TODO Auto-generated constructor stub
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
@Action(value = "login", results =
{@Result(name="success", location="welcome.jsp")})
public String execute() {
return SUCCESS;
}
}
/* Remove
* struts2-gxp-plugin
* struts2-portlet-plugin
* struts2-jsf-plugin
* struts2-osgi-plugin and its related osgi-plugin
* struts-rest-plugin
*
* Add
* velocity-tools-view
*
*
*/
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>
<!-- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter org.apache.struts2.dispatcher.FilterDispatcher -->
<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>
Besides the runtime error, there is deployment error which is
ERROR [com.opensymphony.xwork2.util.finder.ClassFinder] (MSC service
thread 1-2) Unable to read class
[WEB-INF.classes.com.peter.action.LoginAction]: Could not load
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, the scanning methodology of struts will scan the default packages named struts2 for any annotated class but i have instructed struts2 to scan in com.peter.action using init-param but still unable to find the class. It is pretty weird.
Please help.
Thanks.