【Structs2】Struts2入门之hello world程序的跑通

1.去官网下载Struts2.3.4.1的包,总大小约62M,用迅雷下载速度很快。

2.解压后如下图所示:

【Structs2】Struts2入门之hello world程序的跑通_第1张图片

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包,参见下图。

【Structs2】Struts2入门之hello world程序的跑通_第2张图片
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

即可正确访问。结果为:


你可能感兴趣的:(eclipse,exception,struts,filter,action,encoding)