前面已經準備好了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 Tools\Struts2\struts-2.3.15\apps\下的struts2-blank.war
里面有struts的最基本的配置,首先找到E:\Study Tools\Struts2\struts-2.3.15\apps\struts2-blank\WEB-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 Tools\Struts2\struts-2.3.15\apps\struts2-blank\WEB-INF\classes下的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后會出現下面結果,,就說明成功了。
?
?