1.环境搭建

1.建立web项目

2.建立Struts2的配置文件(struts.xml)

  将Struts2 的空项目 中的配置文件 (struts.xml)复制到项目的 src目录下

  struts2.x配置文件的默认存放路径是在/WEB-INF/classes目录下,也就是说,把struts.xml放在src的目录下

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7 
 8     <!--  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 9     <constant name="struts.devMode" value="false" />
10 
11     <include file="example.xml"/>
12 
13 
14 
15     <package name="default" namespace="/" extends="struts-default">
16         <default-action-ref name="index" />
17         <action name="index">
18             <result type="redirectAction">
19                 <param name="actionName">HelloWorld</param>
20                 <param name="namespace">/example</param>
21             </result>
22         </action>
23     </package>
24     -->
25     <!-- Add packages here -->
26     <constant name="struts.devMode" value="true" />
27     <package name="default" namespace="/" extends="struts-default">
28         <action name="helloy">
29             <result>
30             hello.jsp
31             </result>
32         </action>
33     </package>
34 
35 </struts>

3.复制Struts2相应的jar包及第三方包

4.修改对应的web.xml,建立struts2的filter(参考struts自带的项目)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <welcome-file-list>
 8     <welcome-file>index.jsp</welcome-file>
 9   </welcome-file-list>
10   
11   <filter>
12         <filter-name>struts2</filter-name>
13         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
14     </filter>
15 
16     <filter-mapping>
17         <filter-name>struts2</filter-name>
18         <url-pattern>/*</url-pattern>
19     </filter-mapping>
20 </web-app>

 

Struts2的访问过程

Struts2的访问过程:
1.客户机发送请求 (http://localhost:8080/struts2/hello.action)
2.请求发送到tomcat,tomcat查看web.xml文件,web.xml里面配置了使用哪个过滤器进行过滤,要过滤哪些请求
3.通过web.xml指导 要使用struts2Filter 这个过滤器,调用这个过滤器,这个过滤器会去参考stuts.xml这个文件
4.struts.xml里面配置了,namespace("/") action("helloy")访问哪个action result("hello.jsp")返回哪个结果 http://localhost:8080/Struts_01/helloy.action
5.filter参考了struts.xml之后,将请求转发到 hello.jsp
6.hello.jsp 返回显示

 

注:

  如果没有编写对应的Action类,不会报错,只要在struts.xml文件中,使用action标签对某个Action进行了注册,
  那么会自动执行result为 success 的结果

 

struts2 为什么要这样做,为什么不直接访问 hello.jsp?
  作用: /*将你的请求和展现(视图)分开*/,
  每个请求都要经过struts的中转,如果直接访问hello.jsp,那么请求里面就带有了你所需要的展现,如果需要其他的展现,则需要对请求进行修改
  而使用struts之后,只需要修改struts.xml这个配置文件就可以了
  /*之所以复杂化,是为了可扩展性*/

 

namespace 决定了action的访问路径,默认为 "" 即 不指定即为 namespace=""
  namespace 可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为 /index.action   /xxx.index.action /xxx/yyy/index.action
  namespace 最好也用模块来进行命令

 

 

  

 

posted @ 2017-08-16 17:52  白日梦想家12138  阅读(139)  评论(0编辑  收藏  举报