Struts2初步入门

如何搭建Struts2项目

导入相关架包

编写web.xml,配置strus2过滤器
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Hello

package com.lanou3g.hello;

/*
 * struts2 初体验
 * 执行流程
 * 1.通过网址请求中的hello
 * /hello/HelloAction
 * 2.找对应 命名空间(网址)
 * 3.找到后 再通过网址中HelloAction去匹配
 *  Action标签中的 name
 * 4.匹配上 用class 标签 创建其类的对象
 * 5.调用该类中的方法
 * 6.拿到类中的方法的返回值 去匹配 result标签
 * 7.返回值 匹配上 调用标签中的页面
 * 
 * 
 */
public class HelloAction {
    public String hello() {
        System.out.println("hello struts");
        return "success";
    }
}

编写Struts2配置文件struts.xml

<!-- struts根标签 -->
<!-- package name 标签 随便写 只要不重复就行 可以进行模块化区分
     namespace 命名空间 (访问Action网址前的一个网址部分)
     extends 继承一个包 "struts-default"  默认配置 默认提供的功能
      -->
<!-- Action
       name 访问的路径
       class 类的全限定类名
       method 类方法被访问的方法 
       result
       name 方法的返回值(匹配) 
       type 请求跳转的方式 默认是 请求转发dispatcher
       值部分: 跳转网站资源 -->
<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction" class="com.lanou3g.hello.HelloAction" method="hello">
            <result name="success" type="dispatcher">/hello.jsp</result>
        </action>
    </package>
    <!-- 引入其他的struts配置文件 -->
    <include file="com/lanou3g/def/struts.xml"></include>
    <include file="com/lanou3g/dynamic/struts.xml"></include>
    <include file="com/lanou3g/test/struts.xml"></include>
</struts>

默认Action 和 Action的默认处理类

1) 默认Action , 解决客户端访问Action不存在的问题 ,客户端访问Action, Action找不到,默认Action 就会执行
2) 默认处理类 ,客户端访问Action,已经找到匹配元素,但是元素没有class属性,执行默认处理类

  • 在struts-default.xml 配置默认处理类 ActionSupport

Struts2的常量配置

1) struts2 默认常量 在 default.properties 中配置
2) 开发者自定义常量

struts.xml
    格式 : <constant name="struts.devMode" value="true" />
struts.properties
    格式 : struts.devMode = true
web.xml 
    格式 : 
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>struts.devMode</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

3) 常用常量

<!-- 常量配置修改 -->
<!-- 去 defalut.properties文件中  找到要修改的配置  struts.action.extension=action,,     name:键值    value:值 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.action.extension" value="action,,"></constant>
<!-- struts.devMode = false -->
<!-- 可以给配置文件提供热加载(更改完了不用重启服务器) -->
<constant name="struts.devMode" value="true"></constant>
<!-- 访问action访问路径 扩展名 struts.action.extension=action, 默认以.action结尾扩展名 和不写扩展名都会分发给 action -->
<constant name="struts.action.extension" value="action"/>
<!-- false不缓存,true浏览器会缓存静态内容  -->  
<constant name="struts.serve.static.browserCache" value="false"/> 

Action

第一种方式:

package com.lanou3g.test;
/*
 * Action类 创建方式1
 * 随便一个普通类都可以作为一个Action
 * 只需要你去配置struts.xml文件
 * 相比于Servlet 减少代码的侵入性
 */
public class Demo03Action {

}

第二种方式:

package com.lanou3g.test;

import com.opensymphony.xwork2.Action;
/*
 * 实现Action方式二
 * 实现 Action 接口
 * 意义在于  可以提醒你 action类中的方式 该怎么写
 * 
 */
public class Demo04Action implements Action{

    @Override
    public String execute() throws Exception {  
        return null;
    }
}

第三种方式:

package com.lanou3g.test;

import com.opensymphony.xwork2.ActionSupport;

/*
 * 常用创建 Action类 方式
 * public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable 
 * 因为 该类实现类 很多接口  一个接口就有一个功能
 */
public class Demo05Action extends ActionSupport{

}

struts.xml

<!-- 使用 通配符 配置访问路径 星号 * 是方法名   method 标签中填 {1} 代表取到前面星 获取的方法名 -->
<action name="Demo02Action_*" class="com.lanou3g.dynamic.Demo02Action" method="{1}">

动态方法调用

<!-- 常量配置  动态方法(不常用  搜索引擎抓取不好 网址过于复杂) -->
<!-- struts.enable.DynamicMethodInvocation = false -->
<!-- 默认 动态方法是关闭的 -->
<!-- 使用 /dynamic/Demo02Action ! 方法名 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>

页面

http://localhost:8080/sh-struts/CustomerAction_findByName
http://localhost:8080/sh-struts/CustomerAction_findByName.action

Struts2拦截器

struts2中在struts-default.xml文件中声明了所有的拦截器。
而struts2框架默认使用的是defaultStack这个拦截器栈。
在这个拦截器栈中使用了18个拦截器。简单说,struts2框架
在默认情况下,加载了18个拦截器。
注意:只要显示声明使用了一个拦截器。那么默认的拦截器就不在加载。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值