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="确认密码&#