• <bdo id='f4o4o'></bdo><ul id='f4o4o'></ul>

    <small id='f4o4o'></small><noframes id='f4o4o'>

    <legend id='f4o4o'><style id='f4o4o'><dir id='f4o4o'><q id='f4o4o'></q></dir></style></legend>

    1. <tfoot id='f4o4o'></tfoot>
        <i id='f4o4o'><tr id='f4o4o'><dt id='f4o4o'><q id='f4o4o'><span id='f4o4o'><b id='f4o4o'><form id='f4o4o'><ins id='f4o4o'></ins><ul id='f4o4o'></ul><sub id='f4o4o'></sub></form><legend id='f4o4o'></legend><bdo id='f4o4o'><pre id='f4o4o'><center id='f4o4o'></center></pre></bdo></b><th id='f4o4o'></th></span></q></dt></tr></i><div id='f4o4o'><tfoot id='f4o4o'></tfoot><dl id='f4o4o'><fieldset id='f4o4o'></fieldset></dl></div>
      1. Struts2:&lt;s:submit&gt;中的方法属性按钮不起作用

        时间:2023-09-25

        • <tfoot id='WxRnU'></tfoot>

            <small id='WxRnU'></small><noframes id='WxRnU'>

            1. <legend id='WxRnU'><style id='WxRnU'><dir id='WxRnU'><q id='WxRnU'></q></dir></style></legend>
                <bdo id='WxRnU'></bdo><ul id='WxRnU'></ul>

                    <tbody id='WxRnU'></tbody>
                  <i id='WxRnU'><tr id='WxRnU'><dt id='WxRnU'><q id='WxRnU'><span id='WxRnU'><b id='WxRnU'><form id='WxRnU'><ins id='WxRnU'></ins><ul id='WxRnU'></ul><sub id='WxRnU'></sub></form><legend id='WxRnU'></legend><bdo id='WxRnU'><pre id='WxRnU'><center id='WxRnU'></center></pre></bdo></b><th id='WxRnU'></th></span></q></dt></tr></i><div id='WxRnU'><tfoot id='WxRnU'></tfoot><dl id='WxRnU'><fieldset id='WxRnU'></fieldset></dl></div>
                  本文介绍了Struts2:&lt;s:submit&gt;中的方法属性按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 jsp 中有一个表单.有两个提交按钮:搜索"和添加新"按钮.我已经为每个按钮设置了自己的方法属性.

                  <s:form name="searchForm" action="employeeAction" method="post"><s:textfield name="id" label="员工ID"/><s:textfield name="name" label="员工姓名"/><s:submit value="Search" method="doSearch"/><s:submit value="新增"方法="doAddNew"/></s:form>

                  在 struts.xml 中

                  <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache 软件基金会//DTD Struts 配置 2.3//EN"http://struts.apache.org/dtds/struts-2.3.dtd"><支柱><常量名=struts.enable.DynamicMethodInvocation"值=假"/><常量名=struts.devMode"值=真"/><package name="default" namespace="/" extends="struts-default"><default-action-ref name="index"/><全局结果><result name="error">/error.jsp</result></全球结果><全局异常映射><异常映射异常="java.lang.Exception" result="error"/></全局异常映射></包><包名=示例"命名空间=/示例"扩展=默认"><action name="employeeAction" class="example.EmployeeAction"><result name="search">/example/search.jsp</result><result name="add">/example/add.jsp</result></动作></包></struts>

                  在 EmployeeAction 类中

                  公共类 EmployeeAction 扩展 ActionSupport {private static final Logger logger = Logger.getLogger(EmployeeAction.class);@覆盖公共字符串执行()抛出异常{logger.info("调用执行!");返回成功;}公共字符串 doSearch() 抛出异常 {logger.info("正在调用 doSearch!");返回搜索";}公共字符串 doAddNew() 抛出异常 {logger.info("正在调用 doAddNew!");返回添加";}}

                  问题是当我点击 "Search""Add New" 按钮时,方法 doSearch()doAddNew() 从未被调用,而是调用方法 execute().我上面的代码有什么问题?

                  我正在使用 struts v2.3.

                  解决方案

                  设置

                  <常量名="struts.enable.DynamicMethodInvocation"值=假"/>

                  <常量名="struts.enable.DynamicMethodInvocation"值=真";/>

                  另一种方法是为同一个Action定义多个映射,比如

                  在 JSP 中:

                  <s:submit value=搜索"行动=员工搜索行动";/><s:submit value=添加新"动作=员工添加新动作"/>

                  在 Struts.xml 中

                  <action name="employeeSearchAction"类=example.EmployeeAction";方法=doSearch"><result>/example/search.jsp</result></动作><动作名称=employeeAddNewAction";类=example.EmployeeAction";方法=doAddNew"><result>/example/add.jsp</result></动作>

                  第三种方法是使用 通配符映射.p>

                  P.S:如果您选择第二个,作为最佳实践,我建议您对必须执行的每个逻辑操作使用一个操作...

                  如果您的两个操作都加载/管理了公共数据,则搜索";和添加新",然后您可以定义一个由employeeSearchAction 和employeeAddNewAction 扩展的employeeBaseAction.


                  编辑

                  现在是 2014 年,一致不鼓励使用 DMI(今天比以往任何时候都更),除了非常没用,所以我强烈建议您使用解决方案 n.2.

                  I have a form in jsp. There are two submit buttons: "Search" and "Add New" button. I had set each button with their own method attribute.

                  <s:form name="searchForm" action="employeeAction" method="post">
                      <s:textfield name="id" label="Employee ID"/>
                      <s:textfield name="name" label="Employee Name"/>
                  
                      <s:submit value="Search" method="doSearch"/>
                      <s:submit value="Add New" method="doAddNew"/>
                  </s:form>
                  

                  In struts.xml

                  <?xml version="1.0" encoding="UTF-8" ?>
                  <!DOCTYPE struts PUBLIC
                      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
                      "http://struts.apache.org/dtds/struts-2.3.dtd">
                  
                  <struts>
                  
                      <constant name="struts.enable.DynamicMethodInvocation" value="false" />
                      <constant name="struts.devMode" value="true" />
                  
                      <package name="default" namespace="/" extends="struts-default">
                  
                          <default-action-ref name="index" />
                  
                          <global-results>
                              <result name="error">/error.jsp</result>
                          </global-results>
                  
                          <global-exception-mappings>
                              <exception-mapping exception="java.lang.Exception" result="error"/>
                          </global-exception-mappings>
                  
                      </package>
                  
                      <package name="example" namespace="/example" extends="default">
                  
                          <action name="employeeAction" class="example.EmployeeAction">
                             <result name="search">/example/search.jsp</result>
                             <result name="add">/example/add.jsp</result>
                          </action>
                  
                      </package>
                  </struts>
                  

                  In EmployeeAction class

                  public class EmployeeAction extends ActionSupport {
                  
                      private static final Logger logger = Logger.getLogger(EmployeeAction.class);
                  
                      @Override
                      public String execute() throws Exception {    
                          logger.info("Calling execute!");    
                          return SUCCESS;
                      }
                  
                      public String doSearch() throws Exception {    
                          logger.info("Calling doSearch!");    
                          return "search";
                      }
                  
                      public String doAddNew() throws Exception {    
                          logger.info("Calling doAddNew!");    
                          return "add";
                      }
                  }
                  

                  The problem is when I clicked "Search" or "Add New" button, the method doSearch() or doAddNew() was never called, instead it called method execute(). What is wrong with my code above?

                  I'm using struts v2.3.

                  解决方案

                  Set

                  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
                  

                  to

                  <constant name="struts.enable.DynamicMethodInvocation" value="true" />
                  

                  Another way is to define multiple mappings for the same Action, like

                  in JSP:

                  <s:submit value="Search"  action="employeeSearchAction" />
                  <s:submit value="Add New" action="employeeAddNewAction"/>
                  

                  in Struts.xml

                  <action name="employeeSearchAction" class="example.EmployeeAction" method="doSearch">
                         <result>/example/search.jsp</result>
                  </action>
                  <action name="employeeAddNewAction" class="example.EmployeeAction" method="doAddNew">
                         <result>/example/add.jsp</result>
                  </action>
                  

                  A third way is to use Wildcard Mappings.

                  P.S: If you go for the second one, I'll suggest, as a best practice, to use one Action for every logical action you have to perform...

                  If you have common data loaded / managed by both your actions, "search" and "addNew", then you can define a employeeBaseAction, extended by both employeeSearchAction and employeeAddNewAction.


                  EDIT

                  It's 2014 now, and DMI usage is unanimously discouraged (today more than ever), other than pretty useless, so I strongly suggest you to use solution n.2.

                  这篇关于Struts2:&lt;s:submit&gt;中的方法属性按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:#{} ${} 和 %{} 有什么区别? 下一篇:Maven 3 工件问题

                  相关文章

                  <small id='5PGpe'></small><noframes id='5PGpe'>

                  1. <i id='5PGpe'><tr id='5PGpe'><dt id='5PGpe'><q id='5PGpe'><span id='5PGpe'><b id='5PGpe'><form id='5PGpe'><ins id='5PGpe'></ins><ul id='5PGpe'></ul><sub id='5PGpe'></sub></form><legend id='5PGpe'></legend><bdo id='5PGpe'><pre id='5PGpe'><center id='5PGpe'></center></pre></bdo></b><th id='5PGpe'></th></span></q></dt></tr></i><div id='5PGpe'><tfoot id='5PGpe'></tfoot><dl id='5PGpe'><fieldset id='5PGpe'></fieldset></dl></div>
                    <legend id='5PGpe'><style id='5PGpe'><dir id='5PGpe'><q id='5PGpe'></q></dir></style></legend>
                  2. <tfoot id='5PGpe'></tfoot>

                      • <bdo id='5PGpe'></bdo><ul id='5PGpe'></ul>