如何搭建Struts2環境
第一步:加入jar包:把jar包加入到web應用的lib下面。
第二步:配置web.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置 Struts2 的 Filter -->
<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>
</web-app>
第三步:在src目錄下配置Struts.xml檔案
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!--
packeage:包,struts2使用package來組織模組
name:屬性,隨便什麼名字都行,用於其他包引用當前包
extends:當前包繼承的那個包,繼承的可以繼承其中的所有配置,通常情況下繼承struts-default
struts-default包在struts-default.xml中定義
namespace:可選,如果他沒有給出,則以/為預設值
若有namespace有一個預設值,則要想呼叫這個包裡的Action,就必須把這個屬性所定義的名稱空間新增到關聯
的url的字串裡,
http://localhost:8080/contextPath/namespace/actionName.action
-->
<package name="helloword" extends="struts-default" namespace="/">
<!--
class:comopensymphony.xwork2.ActionSupport
method:預設值為execute
result:返回結果,表示action方法執行後可能返回一個結果,所以一個action節點可能會有多個result子節點
多個result子節點就要name來區分
name:標識一個result,和action方法的返回值對應,預設值為success
type:標書結果的型別,預設值為dispatcher
<action name="product-input" class="comopensymphony.xwork2.ActionSupport"
method="execute">
<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
-->
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
<!-- 配置action:一個struts2的請求就是一個action
name:對應一個struts2請求的名字(或對應一個ServletPath但去除/和副檔名)不包含副檔名 -->
</action>
<action name="product-save" class="com.enity.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
第四步:就是寫Javabean和jsp了