首页 > 其他 > 详细

Struts2(四)Action二配置

时间:2015-03-29 01:47:18      阅读:297      评论:0      收藏:0      [点我收藏+]

一、method参数

action

package com.pb.web.action;

public class HourseAction {
    
    public String add(){
        System.out.println("执行添加操作!");
        return "success";
    }
    public String update(){
        System.out.println("执行更新操作!");
        return "success";
    }
    public String delete(){
        System.out.println("执行删除操作!");
        return "success";
    }

}

struts.xml

<?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>

  <!--   <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/> -->

    <!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="hourse_add" class="com.pb.web.action.HourseAction" method="add">
<result>
addsuccess.jsp
</result>
</action>
<action name="hourse_update" class="com.pb.web.action.HourseAction" method="update">
<result>
updatesuccess.jsp
</result>
</action>
<action name="hourse_del" class="com.pb.web.action.HourseAction" method="delete">
<result>
deletesuccess.jsp
</result>
</action>
</package>
</struts>

默认action配置

<!--默认action  -->
<default-action-ref name="index" />
<action name="index">
<result>
index.jsp
</result>
</action>

 

技术分享

二、使用动态方法调用

 

package com.pb.web.action;

public class UserAction {
    
    public String add(){
        System.out.println("执行添加操作!");
        return "success";
    }
    public String update(){
        System.out.println("执行更新操作!");
        return "success";
    }
    public String delete(){
        System.out.println("执行删除操作!");
        return "success";
    }
}

struts.xml

<package name="default" namespace="/" extends="struts-default">
<action name="user" class="com.pb.web.action.UserAction">
<result>
userSuccess.jsp
</result>
</action>

页面

<!--  使用感叹号,调用指定方法-->
<form action="user!add">
<input type="submit"  value="添加"/>
</form>
<form action="user!update">
<input type="submit"  value="更新"/>
</form>
<form action="user!delete">
<input type="submit"  value="删除"/>
</form>

另一种写法

<!--  使用感叹号,调用指定方法-->
<form action="user!add.action">
<input type="submit"  value="添加"/>
</form>
<form action="user!update.action">
<input type="submit"  value="更新"/>
</form>
<form action="user!delete.action">
<input type="submit"  value="删除"/>
</form>

技术分享

三、使用通配符简化配置

 action

package com.pb.web.action;

public class HourseAction {
    
    public String add(){
        System.out.println("执行添加操作!");
        return "success";
    }
    public String update(){
        System.out.println("执行更新操作!");
        return "success";
    }
    public String delete(){
        System.out.println("执行删除操作!");
        return "success";
    }

}

struts.xml

<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="hourse_*" class="com.pb.web.action.HourseAction" method="{1}">
<result>
{1}success.jsp
</result>
</action>
</package>

页面

<%@ 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>
<form action="hourse_add">
<input type="submit"  value="添加"/>
</form>
<form action="hourse_update">
<input type="submit"  value="更新"/>
</form>
<form action="hourse_delete">
<input type="submit"  value="删除"/>
</form>
</body>
</html>

技术分享

 技术分享

 

Struts2(四)Action二配置

原文:http://www.cnblogs.com/liunanjava/p/4375069.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!