【发布时间】: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,所以我用了一个常量.....但是没有用
【问题讨论】:
-
尝试使用
<s:cancel标签而不是method:cancel。在struts.xml中添加cancellable="true"到<action name="methodPrefix" -
它不起作用。刚刚发生错误
并且双方都可以取消。
标签: forms jsp struts2 action-mapping dmi