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:
<? 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 >
<?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:
<? 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 >
<?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:
< Package name="Default" namespace="/" extends="Struts-default" >
< Action name="Hello">
< result >
/hello.jsp
</ result >
</ Action >
</ 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:
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:
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:+)
At Org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy (Strutsactionproxyfactory.java: )
At Com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy (Defaultactionproxyfactory.java:58 )
At Org.apache.struts2.dispatcher.Dispatcher.serviceAction (Dispatcher.java:$)
At Org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction (Executeoperations.java:)
At Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter ( Strutsprepareandexecutefilter.java:()
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:)
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)
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.