简体   繁体   English

struts2操作映射不正确

[英]struts2 action mapping incorrect

I have 2 actionClasses, one is called on startup. 我有2个actionClasses,其中一个在启动时调用。 I'm trying to call an action with a button press (for now) that goes to another page and prints a message in the console saying this action was successful, but when I click on it it isn't called. 我试图通过按下按钮(现在)来调用操作,该操作转到另一页并在控制台中打印一条消息,指出该操作已成功,但是当我单击它时未被调用。 Am I doing something wrong? 难道我做错了什么?

struts.xml 在struts.xml

<struts>
    <package name="default" extends="struts-default">
        <action name="*" class="actionPackage.ActionClass1">
            <result name="homepage">/default.jsp</result>
            <result name="myNextPage">/nextPage.jsp</result>
        </action>
    </package>
</struts>

actionClass1 actionClass1

public String execute(){
    System.out.println("the web application is working properly!");
    return "homepage";
}

actionClass2 actionClass2

public String execute(){
    System.out.println("the button works!");
    return "myNextPage";
}

default.jsp 的Default.jsp

function strutsTestFunc(){document.getElementById("querySubmitter").submit()}

... ...

<form name="searchForm" method="post" action="actionPackage.ActionClass2" id="querySubmiter">

... ...

<input id="filterSubmit" type="submit" value="Search" onclick="strutsTestFunc()">

When I click the button it goes to "/actionPackage.ActionClass2" and gives me a 404 error. 当我单击按钮时,它转到“ /actionPackage.ActionClass2”,并给我一个404错误。 is there something I'm doing wrong here? 我在这里做错什么了吗?

You mappings are wrong. 您的映射是错误的。 Also avoid using * wildcard to map all actions, bypassing in fact the framework mechanisms. 还要避免使用*通配符来映射所有动作,实际上是绕过了框架机制。

<struts>
    <package name="default" extends="struts-default">
        <action name="action1" class="actionPackage.ActionClass1">
            <result name="homepage">/default.jsp</result>
        </action>
        <action name="action2" class="actionPackage.ActionClass2">
            <result name="myNextPage">/nextPage.jsp</result>
        </action>
    </package>
</struts>

in JSP, always point to the mapping of the action, not to the action class itself: 在JSP中,始终指向操作的映射,而不是指向操作类本身:

<form action = "action2" 
        name = "searchForm" 
      method = "post"  
          id = "querySubmiter" >

After it works, start using default results like SUCCESS, and submit with <s:submit /> struts tag, that does not require javascript. 完成后,开始使用默认结果(例如SUCCESS),然后使用不需要JavaScript的<s:submit /> struts标记<s:submit />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM