Struts2框架学习笔记一:Struts2框架搭建、配置详解

1. Struts2框架学习笔记一:Struts2框架搭建、配置详解

1.1. Struts2框架搭建

  1. 创建一个Dynamic web project项目
  2. 在lib文件下导入相应的包,解压Struts压缩包
  • 在G:\struts-2.3.24\apps找struts-blank.war,这里面包含Struts必须jar包。
  1. 书写Action测试类
public class HelloAction {
    public String hello() {
        System.out.println("hello world");
        return "success";
    }
}
  1. 书写Struts.xml配置文件
  • 添加struts2约束信息
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
  • struts配置信息
<struts>
	<package name="hello" namespace="/hello" extends="struts-default">
		<action name="HelloAction" class="com.itheima.web.HelloAction" method="hello">
			<result name="success">/hello.jsp</result>
		</action>
	</package>
</struts>
  • 首先配置包名信息。 ,namespace内容为访问的action名,这个要填正确,和前端的访问路径要一致。

name: 随意填,这里是包名,可以被继承。

namespace: 内容为访问的action名。这个要和前端的访问路径要一致,否则会找不到。

extends: 表示继承那个包,如果继承了谋个包,那么该包里的所有配置都会被继承。这里继承了struts-default包

注意: 对于namespace问题,这样想,首先通过url访问,会被拦截器拦截,拦截器根据url去匹配namespace,比如:httt://localhost: 8080/Struts2/test/test2/testMap.action 这个拦截器会到struts.xml去找namespace为/test/test2的package ,如果找不到,就去找namespace为/test的package,如果还是找不到这样的package,就会去找namespace值为/的namespace,这个是默认的,就会去找里面是否有testMap.action,如果没找到,就报没有/test/test2的错。参考文章

一定要注意拦截器寻找的是namespace,而不是某个action.

  • 其次配置action内容

name: 为Action类名。

class: 为Action类全限名。

method: 为本次访问要访问的这个类的那个方法。

  • result配置信息

name: 为return返回后的值,如果为该值信息,就转向该result包裹内的结果。

type: 指定调用哪一个result类来处理结果,默认使用转发.
标签体: 填写页面的相对路径

  • Struts2访问流程

点击前端–>生成url访问路径–>过滤器过滤url–>过滤器拦截Action–>从struts.xml中查找该Action–>进而找到该类–>进而找到该类的方法–>处理方法,返回结果–>转向result包裹的内容。

  1. 配置web.xml
<filter>
    <filter-name>struts2</filter-name>
  <filterclass>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

1.2. Struts2拦截器的重要性

  1. Struts拦截器帮助我们封装了很多功能。
  2. 拦截器优秀的设计,可插拔设计,即可以添加拦截器,还可以改装
  3. aop思想,面向切面编程:即纵向重复代码,横向抽取。

aop思想

  • Struts2中的应用

aop思想在struts2中的应用

1.3. struts2常量配置

  1. Struts2的常量配置就是重写default.properties文件里的一些配置信息
  2. Struts2默认加载顺序是,properties–>Struts.xml–>web.xml,如果重复,后加载会覆盖先加载内容。
  3. Struts2常量配置可以在三个地方配置,即:

一、 在src下添加struts.properties文件二、 在struts.xml中添加常量配置,一般选择在此处配置常量。

三、 在web.xml中添加常量配置

  1. 配置内容
  • i18n国际化配置,根据浏览器编码解决post乱码问题,get方法提交无效
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
  • 指定访问action时的后缀名,value=action,表示访问action时后缀为.action或者没有后缀都可以,两个逗号表示无后缀也可以。
<constant name="struts.action.extension" value="action"></constant>
  • 指定struts2是否以开发模式运行,一是热加载主配置.(不需要重启即可生效),二是提供更多错误信息输出,方便开发时的调试
<constant name="struts.devMode" value="true"></constant>

1.4. struts2动态调用

  1. 利用通配符*实现动态调用
<action name="CustomerAction_*" class="cn.it.web.action.CustomerAction" method="{1}" >
    <result name="list" >/jsp/customer/list.jsp</result>
</action>
  • name 属性里面有一个通配符,会记录前提传来的内容,并将该值传递给method的属性{1}中。实现动态调用该Action中的方法。

1.5. struts2的默认值配置

  1. method属性默认方法是execute()方法,如果不写,就执行该方法。
  2. result的name属性是success
  3. result的type属性是dispatcher转发
  4. class的默认属性是com.opensymphony.xwork2.ActionSupport,默认找name属性中的Action

1.6. Action类的操作

  1. Action类需要继承ActionSupport
public class CustomerAction extends ActionSupport {}
  • 帮助我们实现了validateable,validationAware,TextProvider,LocalProvider.自己需要这些时不需要再实现了。