2、Struts2开始深入
阅读目录
一、Struts2的配置文件加载顺序
二、Action的配置文件Struts.xml。
三、修改常量的值,可以有三个位置进行修正
四、include的配置
五、Action的写法
六、Action的访问:
七、总结
回到目录
一、Struts2的配置文件加载顺序
1 、进入过滤器【StrutsPrepareAndExecuteFilter】跟代码,可以看到对应的文件加载顺序
进入StrtsPrepareAndExecuteFilter,跟里面的init方法:
进入:
在进入:
init_DefaultProperties() ----加载default.properties
init_TraditionalXmlConfigurations(); ----加载struts-default.xml、struts-plugin.xml、struts.xml
init_LegacyStrutsProperties(); ----加载struts.properties
init_CustomConfigurationProviders(); ----加载配置提供类
init_FilterInitParameters() ; ----加载web.xml中过滤器初始化参数
init_AliasStandardObjects() ; ----加载Bean对象
2 、根据上面的代码,我们就可以得出配置文件的加载顺序了
前三个配置文件不需要关心,是Struts2的内部配置文件,我们无法修改,能修改的就是后面三个配置文件,这几个配置文件的加载是有一定的顺序的。
重点关系后面的几个配置文件,那是我们开发中需要进行更改的。后加载的配置文件中的常量会覆盖先加载配置文件中的常量。
default.properties
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
注意:后配置的常量的值会覆盖先配置的常量的值。
回到目录
二、Action的配置文件Struts.xml。
1、配置
Strtus2框架的核心组件是Action和拦截器,他使用包来管理Action和拦截器。每个包就是多个Action、多个拦截器、多个拦截器引用的集合。
在Struts.xml中,package元素用来定义包配置。在配置包时,必须指定name属性,且不可重复。还需设置extends的属性,extends的属性值
必须是另一个包的name属性,但该属性值通常设置为struts-default。这样该包中的Action就具有了Struts2框架默认的拦截器等功能。
name :包的名称,只有在一个项目中不重名即可。
extends :继承哪个包,通常值为struts-default。
namespace :名称空间,与标签中的name属性共同决定访问路径。
名称空间有三种写法:
带名称的名称空间 :namespace=”/aaa”
跟名称空间 :namespance=”/”
默认名称空间 :namespace=””
abstract :抽象的,用于其他包的继承。
2、配置
action映射是框架中的基本单元。action映射就是将一个请求的url映射到一个action类,然后在通过参数配置对应的方法。
name :与namespace共同决定访问路径
class :Action类的全路径
method :执行Action中的哪个方法的方法名,默认值execute
converter :用于设置类型转换器
3、Struts2常量的配置:
在Struts2的框架中,提供了非常多的常量:(在default.properties)
struts.i18n.encoding=UTF-8 ----Struts2中所有的post请求的中文乱码不用处理。
struts.action.extension=action,, ----Struts2请求的默认的扩展名。默认扩展名是.action或者什么都不写。
回到目录
三、修改常量的值,可以有三个位置进行修正
1、struts.xml中进行修改
在struts.xml中通过元素配置常量是最常用的方式。需要指定两个必须的属性:name 、value.
name:指定了常量的常量名
value:指定了常量的常量值
复制代码
复制代码
2、struts.properties中进行修改
struts.properties文件其格式是Key-value对,每个Key对应一个value,key表示的struts2中的常量、value则是其常量值。
### 设定默认编码为UTF-8
struts.i18n.encoding=UTF-8
3、web.xml中进行修改
在web.xml文件中配置核心过滤器的时候,可以通过初始化参数来配置常量。
复制代码
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts.i18n.encoding
UTF-8
struts2
/*
复制代码
注意:众多的常量记错难度系数太大,我们可以参考default.properties文件来选择我们需要的常量配置。而且,后加载的
配置文件的常量是会覆盖先加载的配置文件中常量的值。
回到目录
四、include的配置
分模块开发,也就是每个人负责不同的模块。那么Struts.xml 肯定是无法满足共用的,太容易出冲突了。一旦出现问题,会导致整个项目都
出现问题的。这个时候可以使用Sturts2的分模块开发。也就是一个主配置文件,然后再关联其他不同模块的配置文件来协助 开发即可。
协同开发,互不影响。分而治之的思想,将任务分配下去。
包含文件
复制代码
复制代码
被包含文件
复制代码
复制代码
回到目录
五、Action的写法
1、Action类是POJO的类,不继承特殊的类、不实现任何特殊的接口,仅仅是一个pojo。
复制代码
package com.turtle.demo1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Demo1Action {
Logger logger = LoggerFactory.getLogger(Demo1Action.class);
public String execute() {
logger.debug("进入了服务器。");
return "SUCCESS";
}
}
复制代码
Struts2中默认执行Action类中的execute方法,该方法有具体的规则:
方法的权限修饰符为public
返回一个字符串,会用来指示跳转到下一个Result
方法没有参数
2、Action类实现一个Action的接口
复制代码
package com.turtle.demo1;
import com.opensymphony.xwork2.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Demo2Action implements Action {
Logger logger = LoggerFactory.getLogger(Demo2Action.class);
// 该提供了五个常量
// String SUCCESS = "success";
// String NONE = "none";
// String ERROR = "error";
// String INPUT = "input";
// String LOGIN = "login";
@Override
public String execute() {
logger.debug("进入了服务器。");
return SUCCESS;
}
}
复制代码
3、 Action类继承ActionSupport类【用得最多的写法】
复制代码
package com.turtle.demo1;
import com.opensymphony.xwork2.ActionSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Demo3Action extends ActionSupport {
Logger logger = LoggerFactory.getLogger(Demo3Action.class);
@Override
public String execute() {
logger.debug("进入了服务器。");
return SUCCESS;
}
}
复制代码
回到目录
六、Action的访问:
定义一个action类:
复制代码
package com.turtle.demo2;
import com.opensymphony.xwork2.ActionSupport;
import com.turtle.demo1.Demo1Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Demo2Action extends ActionSupport {
Logger logger = LoggerFactory.getLogger(Demo1Action.class);
/**
* 保存
* @return
*/
public String save() {
logger.debug("进入了save方法");
return null;
}
public String update() {
logger.debug("进入update方法");
return null;
}
public String delete() {
logger.debug("进入delte方法");
return null;
}
}
复制代码
1、通过配置action标签中method属性来完成:
页面
复制代码
<%--
Created by IntelliJ IDEA.
User: TurtleZhang
Date: 2019/10/27
Time: 17:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
复制代码
配置很简单,也能正确访问,但是同一个Action类配置了很多次,而且改动的只是后面的method的值。这样有点重复了,大忌。体验感觉不太好。
2、通过通配符的配置完成
页面
复制代码
<%--
Created by IntelliJ IDEA.
User: TurtleZhang
Date: 2019/10/27
Time: 17:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
复制代码
这种方法是最常使用的,在中的name属性使用*来代表任意字符,metho中的{1}代表的是属性中出现的第一个*所代替的字符。这个时候就只需要
配置一个即可。
3、动态方法访问
动态方法访问在Struts2中默认是不开启的,如果想要使用需要去开启一个常量,我们加载struts.xml中
页面
复制代码
<%--
Created by IntelliJ IDEA.
User: TurtleZhang
Date: 2019/10/27
Time: 17:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
https://www.cnblogs.com/zhh19981104/p/11730260.html