专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Struts2框架的搭建(导包、书写Action、配置文件)

1、Struts2概念:

(1)Struts2是WebWork框架的升级版本,与Struts1无任何关系,替代了Servlet,负责处理请求。

(2)Struts2已经帮我们封装了很多常用的功能。

2、导包:

由于用IDEA下载jar包失败,直接创建手动导包。

(1)Struts2的目录结构:

96_1.png

(2)导入jar包:

96_2.png

96_3.png

3、书写Action类:

96_4.png

4、创建struts.xml文件(不可更改文件名,在src目录下创建)

(1)创建文件:

96_5.png

(2)导入约束:

<?xml version="1.0" encoding="UTF-8"?>

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

</struts>

(3)书写配置文件:

<struts>
<package name="hello" namespace="/hello" extends="struts-default">
    <action name="HelloAction" class="pers.zhb.hello.HelloAction" method="hello">
        <result name="nihao">/hello.jsp</result>
    </action>
</package>
</struts>

5、在web.xml配置文件中配置Struts2核心过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>struts2</filter-name><!--不重复即可-->
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class><!--过滤器类名-->
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

6、测试:

(1)将项目发布到服务器;

(2)将以下属性写到地址栏:

96_6.png

会显示hello.jsp页面中的信息。

96_7.png

文章永久链接:https://tech.souyunku.com/23145

未经允许不得转载:搜云库技术团队 » Struts2框架的搭建(导包、书写Action、配置文件)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们