Struts2 通配符使用

来源:互联网 发布:什么值得买 类似 知乎 编辑:程序博客网 时间:2024/05/18 00:01

Struts2通配符的使用可以在同一个Action中实现一个类别的(如学生管理) 增,删,改,查  方便项目后期的维护和查看。

1.页面请求

<%@ 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>

<a href="alladd.action">添加数据</a><br/>
<a href="allupdate_updateSuccess.action">修改数据</a><br/>
<a href="alldelete.action">删除数据</a><br/>
<a href="allquery.action">查询数据</a><br/>

</body>
</html>

2.Action配置



import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.regexp.internal.recompile;

public class AllAction extends ActionSupport{

public String add(){
System.out.println("执行添加");
return "success";
}

public String update(){
System.out.println("执行修改");
return "updateSuccess";
}

public String delete(){
System.out.println("执行删除");
return "success";
}

public String query(){
System.out.println("执行查询");
return "success";
}

}

3.请求的控制转发struts.xml, <action name="all*" class="action.AllAction" method="{1}">其中all后面的*等于

上面jsp <a href="alladd.action">添加数据</a><br/>中alladd.action all后面的add。 {1}表示第一个通配符也就是add。

method="{1}"表示跳转到AllAction中对应的方法中

<?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="mypackage" extends="struts-default">
<action name="all*" class="action.AllAction" method="{1}">
</action>
</package>
</struts>


原创粉丝点击