在Struts2.x中使用非execute方法的实现及调用

时间:2021-11-21 22:02:44

Action可以采用默认的execute方法,也可以使用自定义的无形式参数的返回类型为String类型的方法,下面以计算器的实现来练习类方法的实现及调用。

调用默认的execute的方法,在前面的任务中已经练习,这里只练习调用非execute方法,对于非execute方法的调用有如下4种方式:

(1)   使用动态方法调用(DMI)方式;

(2)   在struts.xml中通过method属性指定方法名;

(3)   使用struts提交按钮的method属性;

(4)   使用通配置符方式配置Action。

首先添加struts2支持 添加完以后

在Struts2.x中使用非execute方法的实现及调用

接下来配置struts.xml文件的初始内容如下:

<struts>

      <packagename="mypackage"namespace="/"extends="struts-default">

      </package不要忘记!

name 的名字时随意的

添加包名在包名下添加action类

package bzu.edu.action;

import com.opensymphony.xwork2.ActionSupport;

public class CalcAction extends ActionSupport{
private double num1;
private double num2;
private double result;
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
//加法
public String add(){
result=num1+num2;
return "result";
}
//减法
public String sub(){
result=num1-num2;
return "result";
}
//乘法
public String mul(){
result=num1*num2;
return "result";
}
//除法
public String div(){
if(num2!=0){
result=num1/num2;

}
return "result";
}
}


 

添加calc—_result.jsp页面,用以计算结果

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'calc_result.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="" method="post">
<table align="center" border="1">
<tr>
<td>第1个数:</td>
<td><s:property value="num1"/></td>
</tr>
<tr>
<td>第2个数:</td>
<td><s:property value="num2"/></td>
</tr>
<tr>
<td>计算结果:</td>
<td><s:property value="result"/></td>
</tr>

</table>
</form>
</body>
</html>


因为用了四种方法所以要有四个页面,这四个页面分别与Struts.xml中相对应接下来是四个页面

(1)calc.jsp

在WebRoot下添加calc.jsp页面,修改该页面的内容如下:

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'calc.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<!-- 5.1.动态方法调用(DMI)方式 -->
<script type="text/javascript">
function doCalc(op)
{
document.frm.action="calc!"+op;
document.frm.submit();
}

</script>
</head>
<form name="frm" action="" method="post">
<table align="center" border="1">
<tr>
<td>第1个数:</td>
<td><input type="text" name="num1"/></td>
</tr>
<tr>
<td>第2个数:</td>
<td><input type="text" name="num2"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value=" 加 " onclick="doCalc('add');"/>
<input type="button" value=" 减 " onclick="doCalc('sub');"/>
<input type="button" value=" 乘 " onclick="doCalc('mul');"/>
<input type="button" value=" 除 " onclick="doCalc('div');"/>
</td>
</tr>
</table>

</form>
<body>

</body>
</html>

(2 )calc1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'calc2.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<body>
<s:form name="myfrm" action="calc" method="post">
<table align="center" border="1">
<tr><td>第1个数:</td>
<td><input type="text" name="num1"/></td>
</tr>
<tr><td>第2个数:</td>
<td><input type="text" name="num2"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<s:submit value="加" method="add"/>
<s:submit value="减" method="sub"/>
<s:submit value="乘" method="mul"/>
<s:submit value="除" method="div"/>
</td>
</tr>
</table>
</s:form>

</body>
</html>

(2 )calc3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'calc2.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<!-- (3).使用struts标签来完成form -->
<!-- 重点是使用s:form标签,并在按钮中使用method方法。 它在struts中的配置同样是调用方法(1)-->
</head>

<body>
<s:form name="myfrm" action="calc" method="post">
<table align="center" border="1">
<tr><td>第1个数:</td>
<td><input type="text" name="num1"/></td>
</tr>
<tr><td>第2个数:</td>
<td><input type="text" name="num2"/></td>
</tr>
</table>
<tr>
<td align="center">
<s:submit value="加" method="add"/>
<s:submit value="减" method="sub"/>
<s:submit value="乘" method="mul"/>
<s:submit value="除" method="div"/>
</td>
</tr>

</s:form>
</body>
</html>

(2 )calc4.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'calc4.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<!-- 可以使用通配符的方式进行配置。 -->
<script type="text/javascript">
function doCalc(op)
{
document.myfrm.action="calc"+op;
document.myfrm.submit();
}
</script>

</head>

<body>
<form name="myfrm" action="" method="post">
<table align="center" border="1">
<tr><td>第1个数:</td>
<td><input type="text" name="num1"/></td>
</tr>
<tr><td>第2个数:</td>
<td><input type="text" name="num2"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value=" 加 " onclick="doCalc('add');"/>
<input type="button" value=" 减 " onclick="doCalc('sub');"/>
<input type="button" value=" 乘 " onclick="doCalc('mul');"/>
<input type="button" value=" 除 " onclick="doCalc('div');"/>
</td>
</tr>
</table>
</form>

</body>
</html>

接下来就是最重要的Struts2的配置了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myStruts" extends="struts-default" namespace="/">
<!-- (1) 使用动态方法调用(DMI)方式;(3).s:submit的method方法-->
<action name="calc" class="bzu.edu.action.CalcAction">
<result name="result">/calc_result.jsp</result>
</action>
<!-- (2).使用struts.xml的method来设置不同的action -->
<action name="add" class="bzu.edu.action.CalcAction" method="add">
<result name="result">/calc_result.jsp</result>
</action>
<action name="sub" class="bzu.edu.action.CalcAction" method="sub">
<result name="result">/calc_result.jsp</result>
</action>
<action name="mul" class="bzu.edu.action.CalcAction" method="mul">
<result name="result">/calc_result.jsp</result>
</action>
<action name="div" class="bzu.edu.action.CalcAction" method="div">
<result name="result">/calc_result.jsp</result>
</action>
<!-- (4).通配符进行配置 -->
<action name="calc*" class="bzu.edu.action.CalcAction" method="{1}">
<result name="result">/calc_result.jsp</result>
</action>
</package>
</struts>