Struts2学习(二)

本文详细介绍了Struts2的配置文件,包括web.xml、struts.xml、struts-default.xml等,以及struts-default.xml的组成部分。同时,讨论了Action类的实现方式和访问方法,包括直接指定method、动态方法调用和使用通配符。文章中还提出了关于不同配置下allowed-methods的疑问。
摘要由CSDN通过智能技术生成

Struts2配置文件

1、可能需要使用的配置文件

  • web.xml(必须 ,根据需要去填写)
  • struts.xml ( 自己创建,必须 )
  • struts.properites ( 自己创建,不是必须 )
  • struts-default.xml ( struts2-core-2.5.10.1.jar 必须 )
  • default.properites ( org/apache/struts2/default.properties,当需要找常量名称时候打开他)
  • struts-plugin.xml ( 每个插件都有一个 配置文件 )

2、struts-default.xml的组成(struts2-core-2.5.10.1.jar)

  • 定义常量 ( constant )
  • 定义内部需要的组件Bean ( bean )
  • 定义了一个名称是 struts-default 的抽象包 ( package ),所谓抽象包,是指该package内部没有一个<action>

3、struts-default 的包组成

  • 在 result-types 标签内部定义了一批 result-type
  • 在 interceptors 标签定义了一批 interceptor 和 一批 interceptor-stack
  • 使用 default-interceptor-ref 指定了默认的拦截器栈 ( <default-interceptor-ref name="defaultStack"/>)
  • 使用 default-class-ref 定义了默认的 Action 类 ( ActionSupport )
  • 使用 global-allowed-methods 声明了全局范围内允许被访问的方法 ( execute,input,back,cancel,browse,save,delete,list,index

Action类

1、查看ActionSupport(默认的 Action 类)的源码

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
............................
}

ActionSupport实现了Action接口,Action接口的源码如下

package com.opensymphony.xwork2;

public interface Action {

    public static final String SUCCESS = "success";

    public static final String NONE = "none";

    public static final String ERROR = "error";

    public static final String INPUT = "input";

    public static final String LOGIN = "login";

    public String execute() throws Exception;

}

2、实现 Action 类的方法

  • 实现 com.opensymphony.xwork2.Action 接口(推荐使用的方法,因为struts.xml中未指定method时候回找execute方法)
  • 继承 com.opensymphony.xwork2.ActionSupport 类
  • 不实现 Action 接口 、 也不继承 ActionSupport 类 ,而是自己写自己的类,但是方法参考 Action 中的 execute 方法的形式去声明(public String),返回的是一个名称

3、Action的三种访问方式,在同一个Action中定义不同的方法测试案例

  • 第一种配置,使用 <action> 标签的 method 属性指定相应的方法

    index.jsp

    <%@ page language = "java" pageEncoding = "UTF-8" %>
    <%@ page contentType = "text/html; charset= UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Apache Struts</title>
    <style type="text/css">
        .container { width: 90% ; margin: 10px auto ; box-shadow: 0px 0px 5px 4px #dedede ; padding: 5px 5px ; }
        ul .required { color : blue ; }
        ul li { font-size: 16px ; padding: 5px 5px ;  }
    </style>
    </head>
    <body>
       <div class="container">
            <h4>在同一个 Action 中定义不同的 方法:</h4>
            <form action="${ pageContext.request.contextPath }/customer/action/action/register" method="post" >
                    <input type="text" name="username" placeholder="用户名">
                    <input type="password" name="password" placeholder="密码">
                    <input type="password" name="confirm" placeholder="确认密码&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值