1.设计一个领域类

Calculate.java

package com.model;

public class Calculate {
    private double x;
    private double y;

    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double add(){
        return x+y;
    }
    public double sub(){
        return x-y;
    }
    public double mul(){
        return x*y;
    }
    public double div(){
        return x/y;
    }
}

2.设计Action

CalculateAction.java

package com.action;

import com.model.Calculate;
import com.opensymphony.xwork2.ActionSupport;

public class CalculateAction extends ActionSupport {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private Calculate data;
    private String msg;
    private double value;

    public Calculate getData() {
        return data;
    }
    public void setData(Calculate data) {
        this.data = data;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public double getValue() {
        return value;
    }
    public void setValue(double value) {
        this.value = value;
    }
    public String add() throws Exception{
        value=data.add();
        msg="你选择的是求和运算";
        return "show";
    }
    public String sub() throws Exception{
        value=data.sub();
        msg="你选择的是求差运算";
        return "show";
    }
    public String mul() throws Exception{
        value=data.mul();
        msg="你选择的是求积运算";
        return "show";
    }
    public String div() throws Exception{
        value=data.div();
        msg="你选择的是求商运算";
        return "show";
    }
}

3.设计提交数据页面

input.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>提交数据页面</title>
<script type="text/javascript">
function sub(){document.aaa.action="sub"};
function mul(){document.aaa.action="mul"};
function div(){document.aaa.action="div"};
</script>
</head>
<body>
<form action="add" method="post" name="aaa">
请输入两个整数:<br><br>
第一个运算数:<input name="data.x"/><br><br>
第二个运算数:<input name="data.y"/><br><br>
<input type="submit" value="求和"/>
<input type="submit" value="求差" onclick="sub()">
<input type="submit" value="求积" onclick="mul()">
<input type="submit" value="求商" onclick="div()">
</form>
</body>
</html>

4.设置显示运行结果的页面

show.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
${msg}
<br>第一个数为:${data.x}
<br>第二个数为:${data.y}
<br>运算结果为:${value}
</body>
</html>

5.配置struts.xml

5.1.为Action配置method属性

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">

<action name="add" class="com.action.CalculateAction" method="add">
<result name="show">/show.jsp</result>
</action>
<action name="sub" class="com.action.CalculateAction" method="sub">
<result name="show">/show.jsp</result>
</action>
<action name="mul" class="com.action.CalculateAction" method="mul">
<result name="show">/show.jsp</result>
</action>
<action name="div" class="com.action.CalculateAction" method="div">
<result name="show">/show.jsp</result>
</action>

</package>
</struts>

5.2.动态方法调用

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">

<action name="FourOp" class="com.action.CalculateAction" >
<result name="show">/show.jsp</result>
</action>

</package>
</struts>

修改提交页面数据页面

input.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>提交数据页面</title>
<script type="text/javascript">
function sub(){document.aaa.action="FourOp!sub"};
function mul(){document.aaa.action="FourOp!mul"};
function div(){document.aaa.action="FourOp!div"};
</script>
</head>
<body>
<form action="FourOp!add" method="post" name="aaa">
请输入两个整数:<br><br>
第一个运算数:<input name="data.x"/><br><br>
第二个运算数:<input name="data.y"/><br><br>
<input type="submit" value="求和"/>
<input type="submit" value="求差" onclick="sub()">
<input type="submit" value="求积" onclick="mul()">
<input type="submit" value="求商" onclick="div()">
</form>
</body>
</html>

5.3.使用通配符映射方式

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">

<action name="FourOp_*" class="com.action.CalculateAction" method="{1}">
<result name="show">/show.jsp</result>
</action>

</package>
</struts>

修改提交页面数据页面

input.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>提交数据页面</title>
<script type="text/javascript">
function sub(){document.aaa.action="FourOp_sub"};
function mul(){document.aaa.action="FourOp_mul"};
function div(){document.aaa.action="FourOp_div"};
</script>
</head>
<body>
<form action="FourOp_add" method="post" name="aaa">
请输入两个整数:<br><br>
第一个运算数:<input name="data.x"/><br><br>
第二个运算数:<input name="data.y"/><br><br>
<input type="submit" value="求和"/>
<input type="submit" value="求差" onclick="sub()">
<input type="submit" value="求积" onclick="mul()">
<input type="submit" value="求商" onclick="div()">
</form>
</body>
</html>

6.效果图