struts DispatchAction的使用

1、JSP页面
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title>
    <script type="text/javascript">
        function compute(param) {
            document.forms[0].elements["method"].value = param;
            document.forms[0].submit();
        }
    </script>
</head>
<body>
<html:form action="/login" method="post">
    <input type="hidden" name="method"/>
    <input type="submit" value="add++" onclick="compute('add')"/>
    <input type="submit" value="sub--" onclick="compute('sub')"/>
</html:form>
</body>
</html>

2、struts-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
    <form-beans>
        <form-bean name="login" type="struts.form.LoginForm"/>
    </form-beans>

    <action-mappings>
        <action path="/login" type="struts.action.LoginAction" parameter="method" name="login">
               <forward name="success" path="/success.jsp"/>
        </action>
    </action-mappings>
</struts-config>

3、Action页面
public class LoginAction extends DispatchAction {
    public ActionForward add(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("add-----------------------");
        return actionMapping.findForward("success");
    }

    public ActionForward sub(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("sub------------------------");
        return actionMapping.findForward("success");
    }

    
}

你可能感兴趣的:(java,apache,jsp,xml,struts)