3.struts2中Action的三种写法
**Struts2配置常量(重点掌握怎么编写常量,了解一些常见的常量)** 1.可以在Struts2框架中的哪些配置文件中配置常量? * struts.xml(必须要掌握,开发中基本上就在该配置文件中编写常量) * <constant name="key" value="value"></constant> * web.xml * 在StrutsPrepareAndExecuteFilter配置文件中配置初始化参数 * 注意:后加载的配置的文件的常量会覆盖之前加载的常量!!!! 2.需要大家了解的常量 * struts.i18n.encoding=UTF-8 -- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 * struts.action.extension=action,, -- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开 * struts.serve.static.browserCache=true -- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 * struts.configuration.xml.reload=false -- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 * struts.devMode = false -- 开发模式下使用,这样可以打印出更详细的错误信息 ---------- **指定多个struts的配置文件(了解)** 1.在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿。 为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。 2.可以在<package>标签中,使用<include>标签来引入其他的struts_xx.xml的配置文件。例如: <struts> <include file="struts-part1.xml"/> <include file="struts-part2.xml"/> </struts> 3.注意注意注意(重要的事情说三遍):<include file="cn/itcast/demo2/struts-part1.xml"/> ---------- **Action类的三种写法(重点)** 1.配置文件学习完成,下面的重点是Action类的三种写法 * Action类就是一个POJO类 * 什么是POJO类,POJO(Plain Ordinary Java Object)简单的Java对象.简单记:没有继承某个类,没有实现接口,就是POJO的类。 * Action类可以实现Action接口 * Action接口中定义了5个常量,5个常量的值对应的是5个逻辑视图跳转页面(跳转的页面还是需要自己来配置),还定义了一个方法,execute方法。 * 大家需要掌握5个逻辑视图的常量 * SUCCESS -- 成功. * INPUT -- 用于数据表单校验.如果校验失败,跳转INPUT视图. * LOGIN -- 登录. * ERROR -- 错误. * NONE -- 页面不转向. * Action类可以去继承ActionSupport类(开发中这种方式使用最多)。 * 对请求参数进行校验 * 设置错误信息 * 读取国际化信息 2.下面给大家使用代码来演示这三种方式 3.真正在开发中,一般的情况下都是采用第三种方式
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.action.extension" value="do,,"></constant> <include file="demo1/struts_demo1.xml"></include> <include file="demo2/struts_demo2.xml"></include> </struts>
struts_demo2.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> <package name="demo2" extends="struts-default" namespace="/"> <action name="demoAction1" class="demo2.DemoAction1"></action> <action name="demoAction2" class="demo2.DemoAction2"></action> <action name="demoAction3" class="demo2.DemoAction3"></action> </package> </struts>
Action:
package demo2;
/**
* 就是POJO类
* @author mjl
*
*/
public class DemoAction1 {
public String execute(){
System.out.println("DemoAction1就是POJO类");
return null;
}
}
package demo2;
import com.opensymphony.xwork2.Action;
/**
* 实现Action接口
* @author mjl
*
*/
public class DemoAction2 implements Action{
public String execute() throws Exception {
System.out.println("DemoAction2实现了Action接口");
// ==return null 页面不跳转的
return NONE;
}
}
package demo2;
import com.opensymphony.xwork2.ActionSupport;
/**
* 继承ActionSupport类
* @author mjl
*
*/
public class DemoAction3 extends ActionSupport{
public String execute() throws Exception {
System.out.println("DemoAction3继承了了ActionSupport类");
return NONE;
}
}

浙公网安备 33010602011771号