【问题标题】:Why method prefix not working in Struts 2.3.32为什么方法前缀在 Struts 2.3.32 中不起作用
【发布时间】:2017-11-28 23:24:51
【问题描述】:

我使用的是 Struts 2.3.32,这是代码。

methodPrefix.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <s:form action="methodPrefix" theme="simple">
        name: <br/>
        <s:textfield name="name"/>
        <br><br>
        <s:submit value="Create Person"/>
        <s:submit name="method:cancel" value="Cancel"/>
    </s:form>
</body>
</html>

MethodPrefixAction.java

   public class MethodPrefixAction 
    {
        private String name;
        private String message;
        
        public String execute() throws Exception
        {
            message = name + " create";
            return "success";
        }
        public String cancel() throws Exception
        {
            message = "cancel";
            return "success";
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
    }

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.mapper.action.prefix.enabled" value="true" />
     
    <package name="default" extends="struts-default" namespace="">
        <action name="goMethodPrefixPage">
            <result>/chapter4/methodPrefix.jsp</result>
        </action>
        <action name="methodPrefix" class="example.chapter4.MethodPrefixAction">
            <result>/chapter4/methodPrefixResult.jsp</result>
        </action>
    </package>
</struts>

当我按下Cancel按钮时,它不调用cancel()方法,只调用execute()方法。

我不知道为什么method: 前缀不起作用。

我搜索了很多,知道method:前缀配置在Struts 2.3.32中默认为false,所以我用了一个常量.....但是没有用

【问题讨论】:

  • 尝试使用&lt;s:cancel 标签而不是method:cancel。在struts.xml中添加cancellable="true"&lt;action name="methodPrefix"
  • 它不起作用。刚刚发生错误 并且双方都可以取消。

标签: forms jsp struts2 action-mapping dmi


【解决方案1】:

This tutorial 解释它,this tutorial 显示如何进行验证

方法MethodPrefixAction.execute 应该返回successcancel 字符串。

struts.xml 中,您可以根据返回字符串将用户重定向到不同的页面:

<action name="methodPrefix" class="example.chapter4.MethodPrefixAction">
            <result name="success">/chapter4/methodPrefixResult.jsp</result>
            <result name="cancel">/chapter4/methodPrefix.jsp</result>
        </action>

【讨论】:

    【解决方案2】:

    这个常数

    <constant name="struts.mapper.action.prefix.enabled" value="true" />
    

    仅适用于 action: 前缀,不适用于 method: 前缀。所以你应该使用action属性来提交标签。

    <s:submit action="cancel" value="Cancel"/>
    

    注意:如果您关闭了DMIaction: 前缀仍然可用。

    【讨论】:

    • 那我该如何使用方法前缀呢?
    • 对于方法前缀,您应该启用 DMI,为了安全起见默认禁用它,如果您想启用它,请自行承担风险,请参阅this 答案
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2014-08-23
    • 2023-03-03
    • 1970-01-01
    • 2019-07-18
    • 2021-12-31
    • 2018-06-30
    相关资源
    最近更新 更多