小编典典

Struts 2中的DispatchAction功能?

jsp

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

}

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


阅读 340

收藏
2020-06-08

共1个答案

小编典典

假设您使用的是Struts2版本2.3.15.3或更高版本,则需要将struts.mapper.action.prefix.enabled常数设置为true才能启用对action:前缀的支持。

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

<constant name="struts.mapper.action.prefix.enabled" value="true" />
2020-06-08