Struts2框架的搭建

时间:2019-11-27
本文章向大家介绍Struts2框架的搭建,主要包括Struts2框架的搭建使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Struts2是WebWork框架的升级版本,替代了Servlet。

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

1、导包:

(1)Struts2的目录结构:

 (2)导入jar包:

 2、书写Action类:

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

(1)创建文件:

 (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>

4、在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>

5、测试:

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

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

原文地址:https://www.cnblogs.com/zhai1997/p/11945501.html