关于ssh web 项目出现 No result defined for action comindex.action and result success解决办法

在我学习写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会找不到对应的方法而报错。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值