Struts2第一个工程helloStruts极其基本配置

前面已经准备好了Struts-2.3.15,现在就可以直接搭建Struts2的工程了。前面http://blog.csdn.net/huangchnegdada/article/details/9179041有对Struts-2.3.15的准备工作的详述。

首先打开MyEclispe新建一个Web Project,名字就叫Struts2_0100_Introduction,接下来就是配置Struts2的框架:

然后解压E:Study ToolsStruts2struts-2.3.15apps下的struts2-blank.war


里面有struts的最基本的配置,首先找到E:Study ToolsStruts2struts-2.3.15appsstruts2-blankWEB-INF下面的web.xml文件,将其拷贝到工程下的WEB-INF下面。web.xml文件中的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <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>

    <welcome-file-list>
        <welcome-file>hello.jsp</welcome-file>
    </welcome-file-list>

</web-app>

然后将E:Study ToolsStruts2struts-2.3.15appsstruts2-blankWEB-INFclasses下的struts.xml文件考到工程的src文件夹中。

最后工程的结构如下:


struts中的文件代码如下:

<?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="true" />

    <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"/>
	 -->
	 <!-- 此段代码非常关键,它可以让你在修改代码后,不需要重新启动服务器 -->
	 <constant name="struts.devMode" value="true" />
	 <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="hello_struts">
            <result>
                /hello.jsp
            </result>
        </action>
    </package>
    <!-- Add packages here -->
	
</struts>

将此项目部署起来后,在Web Brower中输入:http://localhost:8080/Struts2_0100_Introduction/hello_struts后会出现下面结果,,就说明成功了。


原文地址:https://www.cnblogs.com/dyllove98/p/3157177.html