当前位置 : 主页 > 编程语言 > java >

jsp – Struts 2中的DispatchAction功能?

来源:互联网 收集:自由互联 发布时间:2021-06-25
我有一个.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" "h
我有一个.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;
    }

}

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

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

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

<constant name="struts.mapper.action.prefix.enabled" value="true" />
网友评论