struts2动态访问方式

[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:
<%@ 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值