给 ecplise 配置struts2配置环境

下面介绍在Eclipse中配置Struts2的过程:

一、下载Struts2,因为Struts2是开源的,百度或者google一下就可以找到下载地址。我下载的是struts-2.3.1.2版本 的,解压之后入下图:

给 ecplise 配置struts2配置环境
apps目录:Struts2的范例

docs目录:Struts2的文档

lib目录:Struts2的类库

src目录:关于Struts2的一些资源

二、现在是利用apps中的范例实现一个helloworld的action,在apps目录下有几个war文件,如下图:

给 ecplise 配置struts2配置环境

这几个文件可以用WinRAR打开,我用好压打开struts2-blank.war之后,如下图

给 ecplise 配置struts2配置环境

在WEB-INF目录下打开classes目录,找到struts.xml文件,如下图

给 ecplise 配置struts2配置环境

将struts.xml文件复制到Eclipse新建工程的src目录下,在我的工程中如下图:

给 ecplise 配置struts2配置环境

三、如上面所示,在WEB-INF目录下打开lib目录,如下图:

给 ecplise 配置struts2配置环境
 打开之后会有如上图的jar包,为保险起见,将所有的jar包复制到新建工程的lib目录下,在我的工程中如下图:

给 ecplise 配置struts2配置环境

四、返回到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>

将其中<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>

五、在Eclipse中打开复制来的struts.xml文件,将文件中<struts>和</struts>之间的内容注释掉,并添加如下所示的代码:

<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会发现出现如下错误:

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如下图所示:

给 ecplise 配置struts2配置环境

上一篇:在Eclipse里面配置Struts2


下一篇:【AutoMapper】实体类间自动实现映射关系,及其转换。