刚学struts2,刚写了个用struts2框架实现的程序,一直报如下错误HTTP Status 404 - No result defined for action com.test.action.LoginAction and result success--------------------------------------------------------------------------------type Status reportmessage No result defined for action com.test.action.LoginAction and result successdescription The requested resource (No result defined for action com.test.action.LoginAction and result success) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.33
struts.xml文件(位于src目录下)如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/hello.jsp</result>
</action>
</package></struts>web.xml文件(位于WEB-INF目录下)如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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></web-app>
--------------------------------------------------------------------------------Apache Tomcat/6.0.33
struts.xml文件(位于src目录下)如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/hello.jsp</result>
</action>
</package></struts>web.xml文件(位于WEB-INF目录下)如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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></web-app>
/**
*
*//**
* @author windson
*
*/
public class LoginAction { private String name;
private String password;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception{
System.out.println("hahahhahah");
System.out.println("hahahhahah");
System.out.println("hahahhahah");
System.out.println("hahahhahah");
System.out.println("hahahhahah");
String str = new String("success");
return str;
}
}
return "SUCCESS"或return "success"试试
<result name="success">/hello.jsp</result>
哦,应该是hello.jsp路径不对了,检查一下.
然后直接return SUCCESS 大写 没引号。
调试没问题,那估计就是jsp的路径错了,你把namespace加上看看
能给我解释下吗
message No result defined for action com.test.action.LoginAction and result success这里的 result successaction中 return SUCCESS配置文件:<result name="success">/页面.jsp</result>试试看............
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>改成
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>还有,最后返回的时候
<package name="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/hello.jsp</result>
</action>
name = "success"多此一举了不需要。。默认就是success,action中return SUCCESS就行了。。
,必须确保你的struts2版本至少在 2.1.6、2.1.8吧