映射中的冲突导致struts.xml

时间:2018-02-12 06:25:52

标签: java struts2

我在 MyBdbSearch 类中定义了3个方法,并且已经在struts.xml中映射了相同的方法,如下所示。

struts.xml中

    <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="struts-default.xml"/>
    <package name="default" extends="struts-default">    

        <action name="bdbSearch" 
                class="com.bdb.MyBdbSearchAction"
                method="showSearchPage">
              <result name="okBdbSearchPage">/jsp/search.jsp</result>
        </action>

         <action name="bdbSearch" 
                class="com.bdb.MyBdbSearchAction"
                method="search">
              <result name="okBdbSearch">/jsp/search.jsp</result>
        </action>

        <action name="bdbSearch" 
                class="com.bdb.MyBdbSearchAction"
                method="getNextResults">
              <result name="okBdbSearch">/jsp/search.jsp</result>
        </action>
     </package>
</struts>

MyBdbSearchAction.java

public class MyBdbSearchAction extends ActionSupport implements ServletRequestAware{    

@Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request = request;
    }



    public String showSearchPage(){

        //some initialization code
        return "okBdbSearchPage";
    }

public String search(){

        //some code
        return "okBdbSearch";
    }

public String getNextResults(){

    //some code
    return "okBdbSearch";
}
}

的index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
    <s:url id="searchUrl" action="/bdbSearch" method="showSearchPage"></s:url>
    <s:a href="%{searchUrl}">
        <h4>search</h4>
    </s:a>
</body>
</html>

当在xml文件中只配置一个方法(showSearchPage)时,app工作正常。但如果所有3种方法都配置如上,则会抛出错误。

没有为动作定义com.bdb.MyBdbSearchAction和结果okBdbSearchPage

为什么会出现这种奇怪的行为?

1 个答案:

答案 0 :(得分:0)

你必须改变.xml文件,如下所示:

<struts>
<include file="struts-default.xml"/>
<package name="default" extends="struts-default">    

    <action name="bdbSearch" class="com.bdb.MyBdbSearchAction">
          <result name="okBdbSearchPage">/jsp/search.jsp</result>
          <result name="okBdbSearch">/jsp/search.jsp</result>
          <result name="okBdbSearch">/jsp/search.jsp</result>
    </action>
 </package>