1、Struts2概念:
(1)Struts2是WebWork框架的升级版本,与Struts1无任何关系,替代了Servlet,负责处理请求。
(2)Struts2已经帮我们封装了很多常用的功能。
2、导包:
由于用IDEA下载jar包失败,直接创建手动导包。
(1)Struts2的目录结构:
(2)导入jar包:
3、书写Action类:
4、创建struts.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>
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)将以下属性写到地址栏:
会显示hello.jsp页面中的信息。