搭建 Struts2 的环境:
加入 jar 包: 复制 struts\apps\struts2-blank\WEB-INF\lib 下的所有 jar 包到当前 web 应用的 lib 目录下.
在 web.xml 文件中配置 struts2: 复制 struts\apps\struts2-blank1\WEB-INF\web.xml 文件中的过滤器的配置到当前web 应用的 web.xml 文件中
在当前 web 应用的 classpath 下添加 struts2 的配置文件 struts.xml: 复制 struts1\apps\struts2-blank\WEB-INF\classes 下的 struts.xml文件到当前 web 应用的 src 目录下
1. 配置web.xml
<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>
2. 配置dtd
Windowsàperferenceàxmlàxmlcatalogàaddà
Location=path
Key=http://struts.apache.org/dtds/struts-2.3.dtd
3. 配置struts
package: 包. struts2 使用 package 来组织模块.
name 属性: 必须. 用于其它的包应用当前包.
extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置.通常情况下继承 struts-default
struts-default 这个包在struts-default.xml 文件中定义.
namespace 可选, 如果它没有给出, 则以 / 为默认值.
若 namespace 有一个非默认值, 则要想调用这个包里的Action,就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里 http://localhost:8080/contextPath/namespace/actionName.action
4. 配置action
配置一个 action: 一个 struts2 的请求就是一个action
name:对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
class的默认值为: com.opensymphony.xwork2.ActionSupport
method的默认值为: execute
result: 结果.
5.步骤:
I. 由product-input.action 转到 /WEB-INF/pages/input.jsp
在 struts2 中配置一个 action
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
II. 由 input.jsp 页面的 action:product-save.action 到 Product's save, 再到 /WEB-INF/pages/details.jsp
<action name="product-save"class="com.atguigu.struts2.helloworld.Product"
method="save">
<resultname="details">/WEB-INF/pages/details.jsp</result>
</action>
在 Prodcut 中定义一个 save 方法, 且返回值为 details