在Eclipse中配置Struts2

來源:互聯網
上載者:User

標籤:http   java   os   io   檔案   for   ar   2014   

下面介紹在Eclipse中配置Struts2的過程:

一、下載Struts2,因為Struts2是開源的,百度或者google一下就可以找到。我下載的是struts-2.3.1.2版本 的,解壓之後入:



apps目錄:Struts2的範例

docs目錄:Struts2的文檔

lib目錄:Struts2的類庫

src目錄:關於Struts2的一些資源

二、現在是利用apps中的範例實現一個helloworld的action,在apps目錄下有幾個war檔案,如:



 

這幾個檔案可以用WinRAR開啟,我用好壓開啟struts2-blank.war之後,如



 

在WEB-INF目錄下開啟classes目錄,找到struts.xml檔案,如



 

將struts.xml檔案複製到Eclipse建立工程的src目錄下,在我的工程中如:



 

三、如上面所示,在WEB-INF目錄下開啟lib目錄,如:



 開啟之後會有如的jar包,為保險起見,將所有的jar包複製到建立工程的lib目錄下,在我的工程中如:



 

四、返回到WEB-INF目錄下,找到web.xml檔案並開啟,開啟之後會看到有如下代碼:

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <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">  

  3.   

  4.     <display-name>Struts Blank</display-name>  

  5.   

  6.     <filter>  

  7.         <filter-name>struts2</filter-name>  

  8.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  9.     </filter>  

  10.   

  11.     <filter-mapping>  

  12.         <filter-name>struts2</filter-name>  

  13.         <url-pattern>/*</url-pattern>  

  14.     </filter-mapping>  

  15.   

  16.     <welcome-file-list>  

  17.         <welcome-file>index.html</welcome-file>  

  18.     </welcome-file-list>  

  19.   

  20. </web-app>  

<?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">    <display-name>Struts Blank</display-name>    <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>index.html</welcome-file>    </welcome-file-list></web-app>

 將其中<filter></filter>和<filter-mapping></filter-mapping>元素複製到Eclipse建立工程的web.xml檔案中,我的web.xml檔案如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <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">  

  3.   <display-name>Structs0317</display-name>  

  4.   <welcome-file-list>  

  5.     <welcome-file>index.html</welcome-file>  

  6.     <welcome-file>index.htm</welcome-file>  

  7.     <welcome-file>index.jsp</welcome-file>  

  8.     <welcome-file>default.html</welcome-file>  

  9.     <welcome-file>default.htm</welcome-file>  

  10.     <welcome-file>default.jsp</welcome-file>  

  11.   </welcome-file-list>  

  12.    <filter>  

  13.         <filter-name>struts2</filter-name>  

  14.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  15.     </filter>  

  16.   

  17.     <filter-mapping>  

  18.         <filter-name>struts2</filter-name>  

  19.         <url-pattern>/*</url-pattern>  

  20.     </filter-mapping>  

  21. </web-app>  

<?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>Structs0317</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>

 

五、在Eclipse中開啟複製來的struts.xml檔案,將檔案中<struts>和</struts>之間的內容注釋掉,並添加如下所示的代碼:

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

  2.   

  3.         <action name="hello">  

  4.             <result>  

  5.                 /Hello.jsp   

  6.             </result>  

  7.         </action>  

  8.     </package>  

<package name="default" namespace="/" extends="struts-default">        <action name="hello">            <result>                /Hello.jsp            </result>        </action>    </package>

 

六、在Ecplise中建立一個Hello.jsp檔案,在<body>標籤之間寫如下代碼:

     

  1. Hello Struts2<br>  

Hello Struts2<br>

 

然後啟動Tomcat伺服器,在IE瀏覽器中輸入http://localhost:8080/Structs0317會發現出現如下錯誤:

  1. There is no Action mapped for namespace [/] and action name [] associated with context path [/Structs0317]. - [unknown location]   

  2.     at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)   

  3.     at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)   

  4.     at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)   

  5.     at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)   

  6.     at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)   

  7.     at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)   

  8.     at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)   

  9.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)   

  10.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)   

  11.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)   

  12.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)   

  13.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)   

  14.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)   

  15.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)   

  16.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)   

  17.     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)   

  18.     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)   

  19.     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)   

  20.     at java.lang.Thread.run(Unknown Source)  

There is no Action mapped for namespace [/] and action name [] associated with context path [/Structs0317]. - [unknown location]at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:500)at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)at java.lang.Thread.run(Unknown Source)

 

其實正確的輸入是http://localhost:8080/Structs0317/hello或者http://localhost:8080/Structs0317/hello.action  訪問到的Hello.jsp如所示:



 

 

OK,至此所有的配置都配置好了,接下來就可以好好的去玩Struts2了。在這裡還想再談一點感受就是,遇到新的東西首先是先拿去 用,等會用了在好好研究它的原理和細枝末節,就像別人給你一把槍一樣,你不會一拿到槍就把它拆開來研究它的內部構造,而是直接拿去用,會用了,覺得這東西 有意思了,再好好去研究的內部結構甚至是仿製出一把出來。這也是我以前所沒有意識到的,希望今後改進,好好學習,天天向上。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.