Struts第一個程序。

 

 

1:建立完程序後。先寫web.xmljava

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <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>

2:加入jar包。web

3:寫struts.xmlapache

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <!--  
        package: 包. struts2 使用 package 來組織模塊. 
        name 屬性: 必須. 用於其它的包應用當前包. 
        extends: 當前包繼承哪一個包, 繼承的, 便可以繼承其中的全部的配置. 一般狀況下繼承 struts-default
                 struts-default 這個包在 struts-default.xml 文件中定義.
        namespace 可選, 若是它沒有給出, 則以 / 爲默認值. 
                                若 namespace 有一個非默認值, 則要想調用這個包裏的Action, 
                                就必須把這個屬性所定義的命名空間添加到有關的 URI 字符串裏
                                
                  http://localhost:8080/contextPath/namespace/actionName.action
    -->
    <package name="helloWorld" extends="struts-default">
        
        <!-- 
            配置一個 action: 一個 struts2 的請求就是一個 action 
            name: 對應一個 struts2 的請求的名字(或對一個 servletPath, 但去除 / 和擴展名), 不包含擴展名
            class 的默認值爲: com.opensymphony.xwork2.ActionSupport
            method 的默認值爲: execute
            result: 結果. 
        -->
        <action name="zhuyemian-dao-struts" >
            <!--  
                result: 結果. 表示 action 方法執行後可能返回的一個結果. 因此一個 action 節點可能會有多個 result 子節點.
                多個 result 子節點使用 name 來區分
                name: 標識一個 result. 和 action 方法的返回值對應. 默認值爲 success
                type: 表示結果的類型. 默認值爲 dispatcher(轉發到結果.)
            -->
            <result>/pages/input.jsp</result>
        </action>
        
        <action name="product-save" class="com.struts2.helloworld.Product"
            method="save">
            <result name="details">/pages/details.jsp</result>    
        </action>
        
    </package>

</struts>

 

<body>
    <a href="zhuyemian-dao-struts.action">Product Input</a>
  </body>

struts跳轉到jsp上。安全

方法名對應的就是action中的methodapp

return對應的就是return中的name。jsp

知識點1:namespace   訪問的時候要在項目名的後面,否則會出404錯誤url

 

 

 

 

 Strutst2會爲每個HTTP請求建立一個新的Action實例。即Action不是單例的。是線程安全的。spa