首页 > 代码库 > Struts2之通配符映射(降低配置量)

Struts2之通配符映射(降低配置量)

通过在配置文件中使用*取代具体的字符下面举例说明

1 StudentAction和TeacherAction

package org.action;

public class StudentAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String add(){
System.out.print("增加学生记录中。。。。");
return "add";
}
public String delete(){
System.out.print("删除学生记录中。。。。");
return "delete";
}
public String update(){
System.out.print("修改学生记录中。。。。");
return "update";
}
public String query(){
System.out.print("查询学生记录中。。。。");
return "query";
}

}


package org.action;

import com.opensymphony.xwork2.ActionSupport;

public class TeacherAction extends ActionSupport {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String add(){
System.out.print("增加老师记录中。。。。");
return "add";
}
public String delete(){
System.out.print("删除老师记录中。。。。");
return "delete";
}
public String update(){
System.out.print("修改老师记录中。。。。");
return "update";
}
public String query(){
System.out.print("查询老师记录中。。。。");
return "query";
}

}

2 配置文件(代码量极少

<?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="default" extends="struts-default">
<action name="*_*" class="org.action.{1}Action" method="{2}">
<result name="{2}">/index.jsp</result>
</action>

</package>
</struts>

3 index.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=http://www.mamicode.com/"">
    
    <title>My JSP ‘index.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=http://www.mamicode.com/"styles.css">
    -->
  </head>
 
  <body>
    <form action="Teacher_add" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"增加老师记录">
    </form>
    <form action="Teacher_delete" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"删除老师记录">
    </form>
    <form action="Teacher_update" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"修改老师记录">
    </form>
    <form action="Teacher_query" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"查询老师记录">
    </form>
    <form action="Student_add" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"增加学生记录">
    </form>
    <form action="Student_delete" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"删除学生记录">
    </form>
    <form action="Student_update" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"修改学生记录">
    </form>
    <form action="Student_query" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value=http://www.mamicode.com/"查询学生记录">
    </form>
  </body>
</html>
4运行截图

技术分享



Struts2之通配符映射(降低配置量)