struts2已经升级到了2.16了,并使用作为替换替换Codebehind Plugin来实现Struts2的零配置。   我在使用Convention Plugin时却遇到了一个N烦人的问题,小弟在此恳请各位赐教,不知道是我配置的问题还是插件 的本身的bug,我在访问相应action时,竟然不执行action中的execute()默认方法(访问action中其他方法时,居然报没 有相应的action名或result异常),而是直接跳转到相应的结果页面,比如访问HelloAction类,将跳转到hello.jsp,当我 把action类给删除了,还是同样跳转到的hello.jsp页面,所以可以肯定的是没有真正根据action来执行相应的跳转,因为 是即使在没有action存在情况下,也会根据URL规则来找到结果页面。    struts.xml配置文件:
    <?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>
<constant name="struts.convention.default.parent.package" value="crud-default" />
  <constant name="struts.convention.result.path" value="/mycontent"/> 
  
<package name="crud-default" extends="convention-default">
  <!-- 基于paramsPrepareParamsStack,增加store interceptor -->
  <interceptors>
   <interceptor-stack name="crudStack">
    <interceptor-ref name="store">
     <param name="operationMode">AUTOMATIC</param>
    </interceptor-ref>
    <interceptor-ref name="paramsPrepareParamsStack" />
   </interceptor-stack>
  </interceptors>
  <default-interceptor-ref name="crudStack" />
</package></struts>
                我具体的action包是这样的:"org.example.products.web.struts2" ( Convention会从根package中寻找包名含  有struts, struts2, action or actions的任意packages和查找符合条件package下面及其子package中对 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类,所以"org.example.products.web.struts2"该 包下所有Action类应该都将被Convention找到),但是我的action不知怎么没用,就是没有被找到,哎!真是百思不得棋 解!!郁闷了好几天,现在还没找到原因!            我的页面文件目录结构是这样:"webapp/mycontent/hello.jsp"(在struts.xml中配置了“ <constant  name="struts.convention.result.path" value="/mycontent"/>,更改了其默认存在WEB-INF/content下,如果 我想放在根目录下,不知是否是把value="/"就行?请教一下各位,恳请各位赐教!)。           当我访问http://localhost:8080/hello.action时直接跳到hello.jsp页面,HelloAction中的execute方法没反应,直接把HelloAction删除了,访问还是同样。
    
       当访问http://localhost:8080/hello!save.action时,直接抱没有相应的action,但HelloAction中有save方法。
       小弟被此问题已困扰好几天了,这几天觉都睡不着,恳请各位赐教!小弟感激不尽!        

解决方案 »

  1.   

    你这个问题不奇怪。convention 会自动根据路径寻找 jsp 文件的。
    /hello/qq/index.jsp 只要你有这样的文件目录。 就可以 采取 /hello/qq/index.action 【/hello/qq/index】 访问
    跟 HelloAction 存在不存在关系不大 
      

  2.   

    public class HelloAction extends ActionSupport
    {
    /**
     * 
     */
    private static final long serialVersionUID = 1L; @Action(value = "/hello/qq/index", results = { @Result(name = "success", location = "http://localhost", type = "redirect") })
    public String list()
    {
    return null;
    }
    }
    如果有action 存在,那么他优先执行 @Result(name = "success", location = "http://localhost", 
      

  3.   


    不错,如果action不存在convention确实能找到相应目录下的jsp,但如果Action存在的话,它应该要执行Action中的默认方法,再跳转吧。