2017-06-26 25 views
1

我使用struts 2.32,这是代码。struts2.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方法,只是调用execute方法。

我不知道为什么方法前缀不起作用。

我搜索了很多,我知道方法前缀配置是支柱2.32,所以我用恒虚默认.....但不起作用

+0

尝试使用'

+0

它不起作用。只是错误发生在并且双方都可以取消。 – Rikimaru

回答

0

这个常数

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

作品action:前缀,不是method:前缀。所以你应该使用action属性来提交标签。

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

那我该如何使用方法前缀? – Rikimaru

+0

为方法前缀您应该启用DMI,它的默认情况下为了安全而禁用,如果您想自行启用它的风险请参阅[this](https://stackoverflow.com/a/20772170/573032)回答 –

0

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> 
+0

不能再使用方法前缀? – Rikimaru

相关问题