Struts工作原理和核心配置

Struts 工作原理和核心配置
(一)            strurs 的工作原理
1.       使用 Jsp/Servlet 时的流程
Jsp à web.xml 中配置的 servlet Servlet-mapping 找到对应的 Servlet à Servlet( 接受参数 验证 调用 DAO 等操作 ) à
跳转到不同的页面
 
2.       使用 Struts 时的流程
Jsp ( 提交到 *.do) --> web.xml( 通过 *.do 找到 ActionServlet) --> ActionServlet ( 通过 web.xml ActionServlet 所在的配置中找到 struts-config.xml) --> struts-config.xml ( 通过 struts-config 中的 path 路径找到 AcionForm Action )  -->
ActionForm ( 通过 ActionForm 中的 validate 方法验证 , 如果通过则进入 Action 失败则通过 input 返回错误页 )  -->
验证通过 :Action ( 调用 DAO 进行逻辑判断 , 验证失败则通过 input 返回错误页 --> 验证成功 则通过 Struts-config.xml 中的 forward 跳转到相应的页面 .
 
Struts 流程图 :

 
 
 
(二)            Struts 的核心配置 (struts-config.xml)
Struts-config.xml 中的配置组
(1).
< form-beans >
    < form-bean name = "loginForm" type = "org.cgz.struts.form.LoginForm" />
</ form-beans >
 
以上为 ActionForm 的配置
1.< form-beans/ > 中可以包含多个 < form-bean/>
2.< form-bean/> 中包含了 2 个属性 : name-> bean 的名称 是此 bean 的唯一标识 ;type-> bean 的包 . 类名称
: 通过此段代码可以读到 form-bean 但看不出是如何跳转到 form-bean 中的 . 如何跳转到 form-bean 中在下面的
Action 中会有体现
 
(2).
< data-sources />
以上是可以用来配置数据源连接,但 struts 中开发中一般不会去使用。
 
(3).
< action-mappings >
    < action
      attribute = "loginForm"
      input = "/login.jsp"
      name = "loginForm"
      path = "/login"
      scope = "request"
      type = "org.cgz.struts.action.LoginAction" >
      < forward name = "login_success" path = "/login_success.jsp" />
</ action >
</ action-mappings >
以上为 Action 的配置
1.< action-mappings / > 中可以包含多个 < action >...</action>
2. name attribute 表示该 Action 对应的 Actionform name 属性 ( 由此可以看出一个 Action 只能对应一个 ActionForm, 而一个 ActionForm 可以对应多个 Action)
3. input 后面的页面是用来作为错误页的   validate 方法验证失败后 会自动跳转到该页面
   : 所有 struts-config 中配置的路径都必须加 / 表示从 WebRoot 下开始定义
4.path  表示该 Action 的虚拟路径 必须加 /, 且不能加 .do 的后缀 (
       
servlet-mapping 中的 url-path 类似 )
5.scope 表示 action 所保存的属性范围 request 表示每次请求重新建立新的 action
6.type 表示 action 的包 . 类名
7. <forward ... /> action 中可以包含多个不同的 forward 路径   forward 中的属性
Name forwar 的唯一标识 Action 代码中执行跳转时 需要通过其来查找对应的路径
Path 表示该 forward 所要跳转的路径
 
(4).
 
< message-resources parameter = "org.cgz.struts.ApplicationResources" />
以上是用来配置资源文件所在的路径
资源文件的后缀名必须为 properties
 
(5)
< global-exceptions >
     < exception key = "num" type = "java.lang.NumberFormatException" path = "exception" ></ exception >
</ global-exceptions >
< global-forwards >
    < forward name = "exception" path = "/errors.jsp" ></ forward >
</ global-forwards >
. 以上是公共跳转路径和公共异常处理 一般不去使用
 
 
: 一般情况下 需要手工配置的是 Action 中的 forward
 

你可能感兴趣的:(java,jsp,struts,职场,休闲)