用通配符配置 action的name,class,result

   此次主要介绍用通配符来配置struts的action的class,name,result之间的链接。下面是实例:
1,struts.xml文件中配置:
 [color=red]  <action name="*_*" class="com.dd.action.{1}" method="{2}">
		     <result name="success">/{1}Result.jsp</result>
		</action>
、[/color]

注意:abcAction,*Action.* ,如果有个请求是abcAction那么struts接受该请求的顺序是:
      abcAction>*Action>*

  一般不让用户直接请求视图资源,一般的做法是有这样的配置:
  <action name="*">
            <result>/{1}.jsp</result>
  </action>
  解释:名字为*表示优先级最低的接受所有请求,因为没有class属性,那么就会让ActionSupport来处理,而这个action的execute方法总是返回success字符串,而上面的result又没有那么属性默认的就是success那么就直接跳转至{1}.jsp中去了。


2,加法Action
 
 
引用
package com.dd.action; public class AddAction { private int number1; private int number2; private int sum; public int getNumber1() { return number1; } public void setNumber1(int number1) { this.number1 = number1; } public int getNumber2() { return number2; } public void setNumber2(int number2) { this.number2 = number2; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } public String add(){ setSum(getNumber1()+getNumber2()); return "success"; } }

3,减法Action
 package com.dd.action;
import com.opensymphony.xwork2.ActionSupport;
public class ReduceAction extends ActionSupport {
	private int number1;
	private int number2;
	private int sum;
	public int getNumber1() {
		return number1;
	}
	public void setNumber1(int number1) {
		this.number1 = number1;
	}
	public int getNumber2() {
		return number2;
	}
	public void setNumber2(int number2) {
		this.number2 = number2;
	}
	public int getSum() {
		return sum;
	}
	public void setSum(int sum) {
		this.sum = sum;
	}
	public String reduce(){
		setSum(getNumber1()-getNumber2());
		return "success";
	}
}

4,用户操作页面。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<%
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 'add.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">
	-->
	<script type="text/javascript">
	    function changeAction(){
	    	document.forms[0].action = "ReduceAction_reduce";
	    	document.forms[0].submit();
	    }
	</script>

  </head>
  
  <body>
      <s:form action="AddAction_add">
          <s:textfield name="number1"></s:textfield>
          <s:textfield name="number2"></s:textfield>
           <input type="button" value="减法" onclick="changeAction();"/>
          <s:submit vlue="提交"></s:submit>
      </s:form>
  </body>
</html>

4,加法跳转页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
  </head>
  <body>
                         加法的结果页面<br>
                         <s:property value="sum"/>
  </body>
</html>

5,减法跳转页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    减法的结果页面<br>
    <s:property value="sum"/>
  </body>
</html>

你可能感兴趣的:(用通配符配置 action的name,class,result)