struts2的action方法匹配以及通配符的使用
struts2的action方法匹配以及通配符的使用
乐在克里特 发表于11个月前
struts2的action方法匹配以及通配符的使用
  • 发表于 11个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

1、 ActionMethod:Action执行的时候并不一定要执行execute方法,可以在配置文件中配置action的时候用“method”属性来指定执行哪个方法,也可以在url地址中动态指定(动态方法调用DMI)struts.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="user" extends="struts-default" namespace="/user">
        <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
            <result>/user_add_success.jsp</result>
        </action>
        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
            <result>/user_add_success.jsp</result>
            <result name="delete">/user_delete_success.jsp</result>
        </action>
    </package>
</struts>

 

index.jsp页面的内容:

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<% String context = request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
     <a href="<%=context %>/user/userAdd">添加用户</a><br />
     <a href="<%=context %>/user/user!add">添加用户</a><br />
     <a href="<%=context %>/user/user!delete">删除用户</a><br />
</body>
</html>

 

UserAction的内容:

package com.bjsxt.struts2.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
     public String add() {
         return SUCCESS;
     }
     public String delete(){
         return "delete";
     }    
}

 

2、 使用通配符
strtus.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="actions" extends="struts-default" namespace="/actions">
        <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
            <result>/Student{1}_success.jsp</result>
        </action>
        
        <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
            <result>/{1}_{2}_success.jsp</result>
        </action>
    </package>
</struts>

 

Action的内容:

public class CourseAction extends ActionSupport {
     public String add() {
         return SUCCESS;
     }
     public String delete() {
         return SUCCESS;
     }
}
public class StudentAction extends ActionSupport {
     public String add() {
         return SUCCESS;
     }
     public String delete() {
         return SUCCESS;
     }
}
public class TeacherAction extends ActionSupport {
     public String add() {
         return SUCCESS;
     }
     public String delete() {
         return SUCCESS;
     }
}

 

  index.jsp页面的内容:

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
     pageEncoding="GB18030"%>
<%String context = request.getContextPath();%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
         <title>Insert title here</title>
     </head>
     <body>
         使用通配符,将配置量降到最低<br />
         <a href="<%=context%>/actions/Studentadd">添加学生</a>
         <a href="<%=context%>/actions/Studentdelete">删除学生</a><br />
         不过,一定要遵守"约定优于配置"的原则<br />
         <a href="<%=context%>/actions/Teacher_add">添加老师</a>
         <a href="<%=context%>/actions/Teacher_delete">删除老师</a>
         <a href="<%=context%>/actions/Course_add">添加课程</a>
         <a href="<%=context%>/actions/Course_delete">删除课程</a>
     </body>
</html>

 

相应的jsp页面有:
Course_add_seccess.jsp
Course_delete_success.jsp
Teacher_add_seccess.jsp
Teacher_delete_success.jsp
Studentadd_success.jsp
Studentdelete_success.jsp

 

http://blog.csdn.net/llbupt/article/details/6638078

标签: struts2 通配符
共有 人打赏支持
粉丝 14
博文 254
码字总数 387498
×
乐在克里特
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: