Struts2--入门

一 概述
1 Struts2 框架是用在javaEE三层中web层框架
2 Struts2框架是在Structs1和webwork基础之上发展全新的框架
3 Sturuts2解决问题:

基础操作
Sturts2基本原理

4 Sturuts2版本
Sturuts-2.3.24
5 web层常见框架
(1)Sturuts2
(2)springMVC
二 Sturuts2入门案例
1 导入jar包

jar包

maven配置

<!-- https://mvnrepository.com/artifact/asm/asm -->
    <dependency>
      <groupId>asm</groupId>
      <artifactId>asm</artifactId>
      <version>3.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/asm/asm-commons -->
    <dependency>
      <groupId>asm</groupId>
      <artifactId>asm-commons</artifactId>
      <version>3.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javassist/javassist -->
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.11.0.GA</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.8.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.8.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ognl/ognl -->
    <dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>3.0.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.3.24</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core -->
    <dependency>
      <groupId>org.apache.struts.xwork</groupId>
      <artifactId>xwork-core</artifactId>
      <version>2.3.24</version>
    </dependency>

2 创建action

package Action;

/**
 * Created by pc on 2017/9/18.
 */
public class HelloAction {
    /*
    * (1)每次访问servlet时候,都会service方法
    * - 写继承HttpServlet,重写类里面的方法
    * - 在web.xml里面配置servlet访问路径
    * (2)访问action,每次访问action时候,默认执行名称execute
    * - 配置action访问路径
    * */
    public String execute(){
        return "ok";
    }
}

3 配置action访问路径

  • 创建Struts2核心配置文件

    • 核心配置文件名称和位置是固定的
    • 位置必须在src下面,名称 struts.xml
  • 引入DTD约束

<?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>
</struts>
  • 配置
<?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="hellodemo" extends="struts-default" namespace="/">
        <!--name:访问名称-->
        <action name="hello" class="Action.HelloAction">
            <!--配置方法的返回值到页面-->
            <result name="ok">/hello.jsp</result>
        </action>

    </package>
</struts>
  • 访问路径

http://localhost:8080/Struts2/hello.action

4 配置Struts2过滤器

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <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>
</web-app>

注释:Class FilterDispatcher Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilterand StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one..即,从Struts 2.1.3起已被标注为过时的,改用StrutsPrepareAndExecuteFilter

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>

会出现

************************************************************************
*                                     WARNING!!!                                         *
*                                                                                                  *   
*>>> FilterDispatcher <<< is deprecated! Please use the new filters! *
*          This can be a source of unpredictable problems!                  *         
*             Please refer to the docs for more details!                         *
*           http://struts.apache.org/2.x/docs/webxml.html              *      
*                                                                                         *
*************************************************************************

运行结果

运行结果

注释:在idea编辑器中,路径中不需要项目名称
三 运行过程图解

图解
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335

推荐阅读更多精彩内容

  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入门3、Struts 2 的执行流程4、配置 st...
    廖少少阅读 2,932评论 3 13
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 2,225评论 0 50
  • Struts2入门初步需掌握 1.struts2概述 2.struts2环境搭建(第一个struts2的应用程序)...
    牛马风情阅读 249评论 0 0
  • [TOC] struts2 概念:Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个ser...
    呦後阅读 373评论 0 0
  • 1、搭建struts2项目步骤:1.1 新建web项目1.2 导入所需jar包 asm-3.3.jarasm-...
    梦幻随手记阅读 494评论 0 1