Struts2错误:There is no Action mapped for namespace... 解决方法

今天在使用 Struts2.1 做注解操作的时候,一直出现这个问题。花了半天时间才解决,错误原因真是让人蛋疼!

错误提示:


项目结构:


使用注解的位置:

package com.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.event.SaveOrUpdateEvent;

import com.biz.GoodsBIZ;
import com.dao.GoodsDAO;
import com.domain.Goods;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * GoodsAction.java
 *
 * @author Techzero
 * @Email techzero@163.com
 * @Time 2015年1月5日 上午9:05:11
 */
@ParentPackage("struts-default")
@Namespace("/goods")
public class GoodsAction extends ActionSupport {
	private Goods goods;
	
	public Goods getGoods() {
		return goods;
	}
	
	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	@Action(value="findall", results={
			@Result(location="/admin/goods/show.jsp")
	})
	public String findAll() throws Exception {
		GoodsBIZ goodsBIZ = new GoodsBIZ();
		GoodsDAO goodsDAO = new GoodsDAO();
		goodsBIZ.setGoodsDAO(goodsDAO);
		
		List<Goods> goodsList = goodsBIZ.findAll();
		
		Map<String, Object> requestMap = (Map<String, Object>) ActionContext.getContext().get("request");
		requestMap.put("goodsList", goodsList);
		
		return ActionSupport.SUCCESS;
	}
}

解决方法:

看上去没有任何问题,可是只要一运行就报错!百度了半天没找到,最后发现我这个包名跟别人的不一样,问题就出在这里!将service换成action就可以了,以下是Struts2的原文文档:

First the Convention plugin finds packages named struts, struts2, action or actions. Any packages that match those names are considered the root packages for the Convention plugin. Next, the plugin looks at all of the classes in those packages as well as sub-packages and determines if the classes implementcom.opensymphony.xwork2.Action or if their name ends with Action (i.e. FooAction).

大概意思是,如果要用Struts2的注解,必须要将action处理类放在struts, struts2, action, actions包或者其子包中。这个规定真心有点坑。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值