6

我有一个 .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;
    }

}

当我单击提交按钮时,它会执行很好的方法。但是当我再次单击添加按钮或任何其他按钮时,它会被重定向到执行方法而不是添加方法。我究竟做错了什么?期待您的回答。提前致谢

4

2 回答 2

7

假设您使用的是 Struts22.3.15.3或更高版本,您需要将struts.mapper.action.prefix.enabled常量设置为 true 以启用对action:前缀的支持。

把它放在你的struts.xml文件中:

<constant name="struts.mapper.action.prefix.enabled" value="true" />
于 2013-11-07T09:51:46.183 回答
1

你也可以在属性文件中指定它:struts.properties

struts.mapper.action.prefix.enabled=true

更多信息:http ://struts.apache.org/development/2.x/docs/constant-configuration.html

于 2014-03-09T10:37:28.777 回答