【Structs2】Struts2入門之hello world程式的跑通

pengfoo發表於2012-09-04

1.去官網下載Struts2.3.4.1的包,總大小約62M,用迅雷下載速度很快。

2.解壓後如下圖所示:

3.在eclipse中新建動態網頁project。點選上圖中apps,解壓struts2-blank,依次在struts-2.3.4.1\apps\struts2-blank\WEB-INF\classes下找到struts.xml拷貝至eclipse工程的src目錄下。

4.將  struts-2.3.4.1\apps\struts2-blank\WEB-INF\    下的web.xml開啟,拷貝(如下)程式碼至eclipse工程的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>

eclipse中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" 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" id="WebApp_ID" version="2.5">
  <display-name>Struts2_0100_Introduction</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.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>

5.拷貝struts-2.3.4.1\lib下部分jar包至eclipse工程的lib目錄下,具體拷貝哪些jar包,參見下圖。


6.將struts.xml改為如下:

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

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/>
	 -->
    <!-- Add packages here -->
	<package name="default" namespace="/" extends="struts-default">

   
        <action name="hello">
            <result >
               /Hello.jsp
            </result>
        </action>
    </package>
</struts>


 6.新建Hello.jsp,部署好tomcat,啟動服務後,在瀏覽器輸入:

http://localhost:8080/Struts2_0100_Introduction/hello.action

即可正確訪問。結果為:


相關文章