在Eclipse中配置Struts2
下面介绍在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文件并打开,打开之后会看到有如下代码:
- <?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>
将其中<filter></filter>和<filter-mapping></filter-mapping>元素复制到Eclipse新建工程的web.xml文件中,我的web.xml文件如下所示:
- <?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> <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>之间的内容注释掉,并添加如下所示的代码:
- <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"> <result> /Hello.jsp </result> </action> </package>
六、在Ecplise中新建一个Hello.jsp文件,在<body>标签之间写如下代码:
- Hello Struts2<br>
Hello Struts2<br>
然后启动Tomcat服务器,在IE浏览器中输入http://localhost:8080/Structs0317会发现出现如下错误:
- 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)
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了。在这里还想再谈一点感受就是,遇到新的东西首先是先拿去用,等会用了在好好研究它的原理和细枝末节,就像别人给你一把枪一样,你不会一拿到枪就把它拆开来研究它的内部构造,而是直接拿去用,会用了,觉得这东西有意思了,再好好去研究的内部结构甚至是仿制出一把出来。这也是我以前所没有意识到的,希望今后改进,好好学习,天天向上。