我有一个.jsp页面,其中有四个名为submit,add,update和delete的按钮:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="User" >
<s:submit />
<s:submit action="addUser" value="Add" />
<s:submit action="updateUser" value="Update" />
<s:submit action="deleteUser" value="Delete" />
</s:form>
</body>
</html>
在每次提交时,它会被重定向到我的struts.xml文件中提到的操作类:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="User" class="vaannila.UserAction">
<result name="success">/success.jsp</result>
</action>
<action name="addUser" method="add" class="vaannila.UserAction">
<result name="success">/success.jsp</result>
</action>
<action name="updateUser" method="update" class="vaannila.UserAction">
<result name="success">/success.jsp</result>
</action>
<action name="deleteUser" method="delete" class="vaannila.UserAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
最后以动作类的相应方法为:
package vaannila;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
private String message;
public String execute()
{
System.out.println("Inside execute method");
message = "Inside execute method";
return SUCCESS;
}
public String add()
{
System.out.println("Inside add method");
message = "Inside add method";
return SUCCESS;
}
public String update()
{
message = "Inside update method";
return SUCCESS;
}
public String delete()
{
message = "Inside delete method";
return SUCCESS;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
当我点击提交按钮时,它会执行方法,这很好。但是当我再次单击“添加”按钮或任何其他按钮时,它会被重定向到执行方法而不是添加方法。我究竟做错了什么? 期待您的回答。提前致谢
答案 0 :(得分:7)
假设您使用的是Struts2版本2.3.15.3
或更高版本,则需要将struts.mapper.action.prefix.enabled
常量设置为true才能启用对action:
前缀的支持。
将它放在struts.xml
文件中:
<constant name="struts.mapper.action.prefix.enabled" value="true" />
答案 1 :(得分:1)
您也可以在属性文件中指定:struts.properties
struts.mapper.action.prefix.enabled =真
了解更多信息:http://struts.apache.org/development/2.x/docs/constant-configuration.html