Struts2錯誤:There is no Action mapped for namespace... 解決方案

來源:互聯網
上載者:User

標籤:struts2   exception   註解   

今天在使用 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 [email protected] * @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包或者其子包中。這個規定真心有點坑。。。

Struts2錯誤:There is no Action mapped for namespace... 解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.