[b]strtus2中对action的访问形式[/b]
[color=blue]
对于对action的访问方式有三种
[list]
[*]1.写死的形式 也就是说当你访问一个路径是,在struts2.xml中就要写死,例如调用增加的方法<action name="hello" class="com.test.HelloAction" method="add">
[*]</action>,这时要是在调用删除的方法就必须重新再写一个action 例如:<action name="hello" class="com.test.HelloAction" method="delete">
[*]</action>
[*]这样的问题就是会产生多个action。
[*]2.使用!的形式
[*]3.使用通配符的方式
[/list]
对于第二种和第三种,值需要在配置文件中写一个action就可以,具体是调用的是哪个方法是由客户端输入的访问路径决定的。
示例代码如下:
[/color]
DMI.jsp:
DMI1.jsp:
DMI2jsp:
DMI3.jsp:
DMI4jsp:
DMIAction.java:
strtus2.xml:
[color=blue]
对于对action的访问方式有三种
[list]
[*]1.写死的形式 也就是说当你访问一个路径是,在struts2.xml中就要写死,例如调用增加的方法<action name="hello" class="com.test.HelloAction" method="add">
[*]</action>,这时要是在调用删除的方法就必须重新再写一个action 例如:<action name="hello" class="com.test.HelloAction" method="delete">
[*]</action>
[*]这样的问题就是会产生多个action。
[*]2.使用!的形式
[*]3.使用通配符的方式
[/list]
对于第二种和第三种,值需要在配置文件中写一个action就可以,具体是调用的是哪个方法是由客户端输入的访问路径决定的。
示例代码如下:
[/color]
DMI.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>动态方法调用</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="${pageContext.request.contextPath }/DMIAction!test1.action">"!"形式调用1</a>
<a href="${pageContext.request.contextPath }/DMIAction!test2.action">"!"形式调用2</a>
<br/>
<a href="${pageContext.request.contextPath }/DMIAction_test3.action">通配符的形式调用1</a>
<a href="${pageContext.request.contextPath }/DMIAction_test4.action">通配符的形式调用2</a>
</body>
</html>
DMI1.jsp:
<body>
"!"的形式访问test1方法
</body>
DMI2jsp:
<body>
"!"的形式访问test2
</body>
DMI3.jsp:
<body>
通配符的形式访问test3
</body>
DMI4jsp:
<body>
通配符的形式访问test4
</body>
DMIAction.java:
package com.sinwee.action;
import com.opensymphony.xwork2.ActionSupport;
public class DMIAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 999956944790156527L;
public String test1() {
return "gantanhao1";
}
public String test2() {
return "gantanhao2";
}
public String test3() {
return "tongpeifu1";
}
public String test4() {
return "tongpeifu2";
}
}
strtus2.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="blogs" namespace="/" extends="struts-default">
<action name="DMIAction" class="com.sinwee.action.DMIAction">
<result name="gantanhao1">
/WEB-INF/pages/DMI1.jsp
</result>
<result name="gantanhao2">
/WEB-INF/pages/DMI2.jsp
</result>
</action>
<action name="DMIAction_*" class="com.sinwee.action.DMIAction" method="{1}">
<result name="tongpeifu1">
/WEB-INF/pages/DMI3.jsp
</result>
<result name="tongpeifu2">
/WEB-INF/pages/DMI4.jsp
</result>
</action>
</package>
</struts>