在我学习写ssh项目联系的时候,出现了:“HTTP Status 404 - No result defined for action com.hry.shop.index.action.IndexAction and result success”的错误提示。
大致意思就是说我没有在Action方法中定义返回值,来让struts做判断跳转页面。下面附上我学习写的代码。
Action中的代码:
package com.hry.shop.index.action;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport {
/**
*
* @return
*/
public String index() {
return "index_ok";
}
}
struts.xml中的代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="shop" extends="struts-default" namespace="/">
<!-- 配置访问首页的Action -->
<action name="index" class="indexAction">
<result name="index_ok">/WEB-INF/jsp/index.jsp</result>
</action>
</package>
</struts>
我明明定义了返回值和一样的返回值名称,为什么还是提示我没有定义呢?这个问题经过好久才得到解决。我对Action中的代码做了一些更改。
package com.hry.shop.index.action;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport {
/**
*
* @return
*/
public String execute() {
return "index_ok";
}
}
然后程序就能够成功执行了。通过搜索发现是这样解释的:
如果你的Action类是继承自ActionSupport的话,确切的说是重写了execute方法,ActionSupport里的默认实现就是返回"success"视图。因此,你可以不实现execute方法,只要你的struts.xml里有"success"对应的result即可。如果你的Action类没有继承ActionSupport,而你又没有在struts.xml中对应<action>标签中用method属性指定你自己的方法的话,默认就要找execute方法,这时是必须要实现execute方法的,否则Struts2会找不到对应的方法而报错。