第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

從零打造在線網(wǎng)盤系統(tǒng)之Struts2框架起步

標(biāo)簽:
Java

Struts2概述

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Strus2作为控制器(Controller)来建立模型与视图的数据交互。Struts2使用了大量的拦截器来处理用户请求,从而将业务逻辑控制器和ServletAPI分离

Struts2工作流程

  1. 客户端发起请求

  2. 核心控制器FilterDispatcher拦截请求调用Action

  3. 调用Action的execute方法前调用一系列的拦截器

  4. 调用execute执行业务逻辑

  5. 返回结果

控制器是What?

包含execute方法的POJO既可以作为控制器,即一个简单的JAVA类包含一个execute()方法就可以作为控制器,同时控制器具有封装客户端请求参数的能力.

public class TestAction {    public String execute() throws Exception {        return "test";
    }
}

Struts2XML配置

XML配置完整工程示例源码下载
导入struts依赖jar

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.18</version>
        </dependency>

web.xml配置Struts2拦截器

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

编写struts.xml

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>

    <package name="default" extends="struts-default">
        <action name="hello" class="com.jimisun.action.TestAction">
            <result name="hello">/WEB-INF/jsp/hello.jsp</result>
        </action>
    </package></struts>

编写控制器

public class TestAction {    public String execute() throws Exception {        return "hello";
    }
}

配置好以上步骤即可访问路径http://localhost:8080/hello.action

Struts2注解配置

注解配置Struts2完整示例源码下载
如果需要使用注解开发,则需要增加struts2-convention-plugin的Jar

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.5.18</version>
        </dependency>

那么在你的Action中就可以这样编写Action,不需要再到struts.xml中进行配置

@ParentPackage("struts-default")@Namespace("/test")public class TestAction {    @Action(value = "hello", results = {            @Result(name = "hello", location = "/WEB-INF/jsp/hello.jsp")
    })    public String hello() throws Exception {        return "hello";
    }
}

Struts2的浏览器插件

在进行Struts2开发的时候随着项目的增大,你所需要处理的路径和方法映射越多,有时候会让你手忙脚乱,而struts2-config-browser-plugin插件很好的帮你解决了这个问题,只需要Jar包依赖即可

        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-config-browser-plugin -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-config-browser-plugin</artifactId>
            <version>2.5.18</version>
        </dependency>

5bf014780001e5be19100458.jpg

本章总结

Struts2的起始配置比较简单,但是Struts2其他相关配置就比较繁琐了,不可掉以轻心

原文出处:https://www.cnblogs.com/jimisun/p/9945934.html  

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消