【Struts2複習知識點一】-配置struts2環境

mlinge-奮鬥吧發表於2012-07-03

1.新增struts2支援包到lib目錄下。

2.建立struts.xml檔案並放到src目錄下。

View Code 

<?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>
    <!-- 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="example.xml"/>



    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
     -->
     
     <!--即時載入-->
     <constant name="struts.devMode" value="true" />
     <package name="default" namespace="/" extends="struts-default">
        
        <action name="hello">
            <result>
                /Hello.jsp
            </result>
        </action>
    </package>
    <!-- Add packages here -->

</struts>


3.配置web.xml檔案

View Code 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <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>


4. 訪問 http://localhost:8080/projectrname/hello 即可檢視Hello.jsp

注意:tomcat安裝路徑不能有空格。負責啟動報錯。

相關文章