Configuring Struts2 in Eclipse

Source: Internet
Author: User
Tags tomcat server

The procedure for configuring STRUTS2 in Eclipse is described below:

First, download Struts2, because Struts2 is open source, Baidu or Google can be found a bit. I downloaded the struts-2.3.1.2 version, unzip it into:



Apps directory: Examples of Struts2

Docs directory: Documents for STRUTS2

LIB Directory: Class library for Struts2

SRC Directory: Some resources about STRUTS2

Second, now is to use the example in the application to implement a HelloWorld action, in the Apps directory there are several war files, such as:



These files can be opened with WinRAR, I use a good pressure to open struts2-blank.war, such as



Open the Classes directory under the Web-inf directory and locate the Struts.xml file, as



Copy the Struts.xml file to the SRC directory of the Eclipse new project, in my project such as:



As shown above, open the Lib directory under the Web-inf directory, such as:



After opening the jar package like, for the sake of insurance, all the jar package copied to the new project in the Lib directory, in my project such as:



Four, return to the Web-inf directory, find the Web. xml file and open it, and then you will see the following code:

  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. < Display-name > Struts Blank</display-name>

  4. < Filter >

  5. < Filter-name >struts2</filter-name>

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

  7. </ Filter >

  8. < filter-mapping >

  9. < Filter-name >struts2</filter-name>

  10. < Url-pattern >/*</url-pattern>

  11. </ filter-mapping >

  12. < welcome-file-list >

  13. < Welcome-file >index.html</welcome-file>

  14. </ welcome-file-list >

  15. </ 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 >

Place <filter></filter> and <filter-mapping></filter-mapping> element to the Eclipse new Project's Web. xml file, my web. xml file looks like this:

  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. < filter-mapping >

  17. < Filter-name >struts2</filter-name>

  18. < Url-pattern >/*</url-pattern>

  19. </ filter-mapping >

  20. </ 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>  &Nbsp;<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>

Open the copied struts.xml file in Eclipse, comment out the contents between <struts> and </struts> in the file, and add the code as follows:

  1. <  Package name="Default" namespace="/" extends="Struts-default" >

  2. < Action name="Hello">

  3. < result >

  4. /hello.jsp

  5. </ result >

  6. </ Action >

  7. </ Package >

<package name= "Default" namespace= "/" extends= "Struts-default" > <action name= "Hello" > <re sult>/hello.jsp </result> </action> </package>

VI. Create a new hello.jsp file in Ecplise and write the following code between the <body> tags:

    1. Hello struts2<br>

Hello struts2<br>

Then start the Tomcat server and enter http://localhost:8080/Structs0317 in Internet Explorer to find the following error:

  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:+)

  4. At Org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy (Strutsactionproxyfactory.java: )

  5. At Com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy (Defaultactionproxyfactory.java:58 )

  6. At Org.apache.struts2.dispatcher.Dispatcher.serviceAction (Dispatcher.java:$)

  7. At Org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction (Executeoperations.java:)

  8. At Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter ( Strutsprepareandexecutefilter.java:()

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

  12. At Org.apache.catalina.core.StandardContextValve.invoke (Standardcontextvalve.java:175)

  13. At Org.apache.catalina.core.StandardHostValve.invoke (Standardhostvalve.java:+)

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

Actually, the correct input is Http://localhost:8080/Structs0317/hello or http://localhost:8080/Structs0317/hello.action. The access to the hello.jsp is as follows:



OK, at this point all the configuration is ready, then you can play a good Struts2. Here also want to talk about a little bit of feeling is, meet the new things first is to use, and so will be used in a good study of its principles and details, like others give you a gun, you will not take it apart to study its internal structure, but directly take to use, will use, think this thing interesting, The internal structure of the study is even copied out. This is what I have not been aware of, hope to improve in the future, study hard, day up.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.