多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 下载 [http://struts.apache.org/downloads.html](http://struts.apache.org/downloads.html) 可能需要VPN。 我们下载2.3.31版本。 ![](https://box.kancloud.cn/afea5ba14fecc6a0017a0203742acc23_374x272.png) 同时,我们将下载后压缩包放在了tower中的『梦云智』项目中,这意味着你可以直接登陆tower来下载教程使用的struts。 ## 引入 在JAVA开发的世界中,我们可以使用很多的第三方包来扩充我们的应用。这就像是在搭积木。我们想要什么的时候,直接去下载个积木包(一个包中包括了很多种积木),然后使用从这个积木包中找到一些我们需要的积木,引入进来就可以了。 struts下载后,默认给我们了所有的积木,但按项目需求的不同,我们可能只需要部分积木就能达到我们学习的目的。当然了,你也可以把所有的积木都拿过来使用,这仅仅只会增加项目的占用空间而已。在此,我们抱着学习的目的,只引struts中最核心的包(积木)。 > 查看struts的基本jar包,请参考:[http://www.imooc.com/video/9022](http://www.imooc.com/video/9022) 在这,我们已经为大家分解好,并放在团队的tower中 ![](https://box.kancloud.cn/783a60463a422960e1aaf38eba19ee76_364x195.png) 我们下载解压,并将其放置到如下位置: ![](https://box.kancloud.cn/ea96cc9cb599ec23a7969c5af6ba3907_512x792.png) ### 使用Struts JAR包 将在eclipse中,通过Java Build Path中的Libraries进行第三方库(jar包)的引入 在项目文件夹上点右键 -&gt; Build path -&gt; configure Build Path -&gt; Libraries -&gt; add JARS 然后选择上面lib文件夹中的所有jar文件,最后点确定。此时struts已经被我们成功引用到项目中,并可以在项目中使用他们了。我们此时打开Java Resources -&gt; Libraries 能够查看到我们刚刚引入的包,视为正常。 ![](https://box.kancloud.cn/71f2a0029b99902d10cbe6f1e007f580_526x430.png) # 配置web.xml 下面,我们共同将struts配置至项目中: ``` <display-name>Java EE study</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> ``` 我们新配置了两项信息: * 定义了一个过滤器,名字叫做:struts2, 它位于:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter * 指定了过滤规则(路由)。 * 在此规则下的所有请求,请交给指定的过滤器来处理。 * 即:所有的请求都会交给我们在上面定义好的struts2过滤器(类). ## 测试: 重启tomcat,控制台无报错.即成功加了struts。 如果未成功引入的话,将报找不到 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter的错误。此时请检查: * 是否将struts基本jar包复制到了项目的指定位置。 * tomcat在启动时,将复制这些jar包。 * 是否在Libraries中,由上面的位置添加了这些jar包。 * 添加后,我们将在左侧的 src中的Libraries中找到它们。 ![https://box.kancloud.cn/3454d3b2d9c29bc5e699a22fd6cc7263_536x356.png](https://box.kancloud.cn/3454d3b2d9c29bc5e699a22fd6cc7263_536x356.png) 在其包上点右键,点击属性,文件的位置应该如下: ![https://box.kancloud.cn/40fcdf062f1df4f56cd61835124cd641_1114x264.png](https://box.kancloud.cn/40fcdf062f1df4f56cd61835124cd641_1114x264.png) 加入注释后如下: ``` <?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>java ee study!</display-name> <!-- 指定过滤器 --> <filter> <!-- 定义过滤器的名字:struts2 --> <filter-name>struts2</filter-name> <!-- 过滤器对应的类 该类位于Java Resources -> Libraries -> struts2-core-2.3.31.jar ->org.apache.struts2.dispatcher.ng.filter -> StrutsPrepareAndExecuteFilter --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 为过滤器指定路由 --> <filter-mapping> <!-- 指定过滤器名称:struts2 --> <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> ``` 配置信息的关系如下: # ![](https://box.kancloud.cn/28f05466af76ab6e2f525ebaaf5c3515_1198x465.png) ## V:helloStruts.jsp struts能够找到WebContent下的jsp文件渲染。但人们更习惯将一些模板文件建立在WebContent下的WEB-INFO目录下。没错,web.xml也在这个目录下,这更多的是出于安全角度来考虑的,因为tomcat的策略是:用户无法直接访问WEB-INFO目录下的任何文件,所以做为V层的模板文件放在此,当然最合适不过了。 在WebContent/WEB-INFO中新建jsp/helloStruts.jsp, 并输入以下内容。 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h2>hello struts!</h2> </body> </html> ``` 有了jsp文件,下面,我们通过配置struts,来达到展示该V层文件的目的。 # 初始化struts.xml 在web.xml配置好struts后,它会自动加载src文件夹中的struts.xml文档,以达到配置struts的目的。 在src中,我们新建struts.xml文档,并写入标准的文件头,来告知此文件的编码及遵循的dtd格式。 > 我们只需要知道:不同的dtd格式,解析的方式会不同 同时,写入基本的struts标签。 > 在前期,我们其实可以完全的不去关心这到底是什么,又是为什么要这样写,仅管这样写就好了。 ``` <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts> ``` 保存后,注意看eclipse的提示,在文件名上是否有红色的X产生,如果有的话,检查拼写。 重启Tomcat,以使得我们刚刚建立的配置文件生效。注意看控制台信息,发生拼写错误时,会报错。 # 新建hellostruts路由 ``` <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 声明包名,并指定命名空间/hello,同时继承于struts-default。 这与我们定义一个class相类似 --> <package name="default" namespace="/hello" extends="struts-default"> <!-- 设置hellostruts Action --> <action name="struts"> <!-- 设置V层,相对于WebContent的绝对路径 --> <result>/WEB-INF/jsp/helloStruts.jsp</result> </action> </package> </struts> ``` # 测试 重启Tomcat 访问以下url [http://localhost:8080/javaee/hello/struts](http://localhost:8080/javaee/hello/struts) ![](https://box.kancloud.cn/61aaa2c78ff10ce9fae6d824bf72154e_412x155.png) ## 代码分析: ![](https://box.kancloud.cn/cc44440b34c8703a89f5c0da8d5da4be_1456x457.png) ## 未找到路由: ![](https://box.kancloud.cn/55ea1ee409beabced9620107dc82927a_475x252.png) 系统未找到路由时,会报如上错误。现在,我们启用开发模式,来查看错误的详细信息: ## 开发模式 ``` <struts> <!-- 开发模式 - ture --> <constant name="struts.devMode" value="true" /> ``` 重启tomcat后,我们再测试 ![](https://box.kancloud.cn/e6b898d5132a636260001610d7a2bcb3_885x220.png) 此时,我们发送前台为我们自动打印了命名空间及action名等信息。有了以上信息,我们去配置struts的xml文件就会变得轻松很多。 > 不仅如此,在开发模式下,当我们再次更改struts.xml时,会被时时的重新加载,从而不再需要重新启动tomcat了。 ## 作业 使用以下URL来显示hellostruts [http://localhost:8080/javaee/hellostruts](http://localhost:8080/javaee/hellostruts) 使用如下信息定义XML,文件如何存放,前台如何访问: ``` <!-- 声明包名,并指定命名空间\hello,同时继承于struts-default。 这与我们定义一个class相类似 --> <package name="default" namespace="/mengyunzhi" extends="struts-default"> <!-- 设置hellostruts Action --> <action name="welcome"> <!-- 设置V层,相对于WebContent的绝对路径 --> <result>/jsp/helloStruts.jsp</result> </action> </package> ``` > 当使用struts做为过滤器后,WebContent下的所有文件,用户都无法直接访问到了,这无疑提升了系统的安全性。