我遇到以下代码的问题,有人可以解释为什么这个方法可能没有在jobListAction上被触发?提交表格后,“设置”被调用两次。简而言之,我似乎无法通过struts按钮调用多个方法。有什么指针/要检查的东西吗?
public class JobListAction {
public String execute() {
System.out.println("setup");
}
public String deactivate() {
System.out.println("called");
}
public String callonme()
{
}
}
JSP:
<s:form id="recordsListForm" method="post" action="jobList">
<s:submit type="button" action="deactivate" value="Deactivate Selected Jobs" method="deactivate" />
<s:submit type="button" action="callonme" value="CallonMe" method="callonme" />
</s:form>
struts.xml中
<!-- Job List -->
<action name="jobList" class="JobListAction">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
<!-- Job List - Deactivate Job -->
<action name="deactivate" class="JobListAction" method="deactivate">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
<action name="callonme" class="JobListAction" method="callonme">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
答案 0 :(得分:2)
我想在struts 2中你需要告诉Struts.xml文件中的方法名称,尝试一下,希望它有效......
<action name="jobList" class="JobListAction" method = "deactivate">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
答案 1 :(得分:2)
如果您想要一个可以在同一个动作类中调用多个方法的动作声明,请查看使用wilcard mappings:
查看强>
<s:form id="recordsListForm" method="post" action="jobList">
<s:submit type="button" action="jobList_deactivate" value="Deactivate Jobs" />
<s:submit type="button" action="jobList_callonme" value="CallonMe" />
</s:form>
<强> struts.xml中强>
<!-- Job List -->
<action name="jobList_*" method="{1}" class="JobListAction">
<result name="input">/jsp/admin/jobList.jsp</result>
<result name="success">/jsp/admin/jobList.jsp</result>
</action>
上述映射将匹配以action
开头的任何jobList_
,然后使用匹配的其余部分作为JobListAction
类中的调用方法。
答案 2 :(得分:1)
适合我;什么版本?是否启用了动态方法调用(默认情况下是这样)?
“call 多个方法是什么意思?”每个请求只能调用一个方法。
我的标准:
setup // On initial form display
called // Clicking submit
逐字(或多或少)剪切并粘贴您的代码。