我有一个.jsp页面,其中有四个按钮,分别是提交、添加、更新和删除:
<%@ 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>
最后,对应的Action类方法如下:
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;
}
}
当我点击提交按钮时,它会执行方法,这很好。但是当我再次点击添加按钮或任何其他按钮时,它会重定向到执行方法而不是添加方法。我做错了什么吗? 期待您的回答。提前致谢。