我使用 Struts 2.3.32,这是代码。
方法前缀.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,所以我使用了一个常量.....但它不起作用