Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5018530
  • 博文数量: 891
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8636
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(891)

文章存档

2020年(8)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类:

2011-09-28 22:31:54

struts2 学习

    一。 核心文件struts.xml 的配置

      1。 bean的配置

          主要就是名字的一个引用

   

     

      ...

    The bean element has one required attribute, class, which specifies the Java class to be created or manipulated. A bean can either be created by the framework's container and injected into internal framework objects, or have values injected to its static methods The first use, object injection, is generally accompanied by the type attribute, which tells the container which interface this object implements. The second use, value injection, is good for allowing objects not created by the container to receive framework constants. Objects using value inject must define the the static attribute.Attribute  Required  Description  class  yes  the name of the bean class  type  no  the primary Java interface this class implements  name  no  the unique name of this bean; must be unique among other beans that specify the same type  scope  no  the scope of the bean; must be either default, singleton, request, session, thread  static  no  whether to inject static methods or not; shouldn't be true when the type is specified  optional  no  whether the bean is optional or not 3 Packages的配置简单就包的配置 是配置的一个逻辑规划Attribute  Required  Description  name  yes  key to for other packages to reference  extends  no  inherits package behavior of the package it extends  namespace  no  see Namespace Configuration  abstract  no  declares package to be abstract (no action configurations required in package) Sample usage Package Example (struts.xml)
     
       

                  class="org.apache.struts2.showcase.action.EmployeeAction" >
            /empmanager/listEmployees.jsp
           
       

       
          {1}
          /empmanager/editEmployee.jsp
           
              execute
           

         

                        class="org.apache.struts2.showcase.action.EmployeeAction" >
            /empmanager/editEmployee.jsp
            edit-${currentEmployee.empId}.action
         

                      class="org.apache.struts2.showcase.action.EmployeeAction" >
            /empmanager/editEmployee.jsp
            edit-${currentEmployee.empId}.action
         

     
   

   2 Constant 的配置。主要是对struts2 应用项目的一个全局配置,配合web。xml里配置Constants provide a simple way to customize a Struts application by defining key settings that modify framework and plugin behavior. There are two key roles for constants. First, they are used to override settings like the maximum file upload size or whether the Struts framework should be in "devMode" or not, and so on. Second, they specify which Bean implementation, among multiple implementations of a given type, should be chosen. Constants can be declared in multiple files. By default, constants are searched for in the following order, allowing for subsequent files to override previous ones:In the various XML variants, the constant element has two required attributes: name and value. Attribute  Required  Description  name  yes  the name of the constant  value  yes  the value of the constant In the struts.properties file, each entry is treated as a constant.In the web.xml file, any FilterDispatcher initialization parameters are loaded as constants.Sample usage Constant Example (struts.xml)

     

      ...

   

 

    Constant Example (struts.properties)

    struts.devMode = true

 

    Constant Example (web.xml)

         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">

       
            struts
            org.apache.struts2.dispatcher.FilterDispatcher
           
             struts.devMode
             true
           

       

        ...

   


    4.Namespace 的配置就是命名空间的配置 说白了就是你访问action 的前置url 有个默认空间 当你制定的空间找不到 就转到default里
       
            greeting.jsp
       

       
            bar1.jsp
       

   

   
       
            moo.jsp
       

   

   
       
            bar2.jsp
       

   

    5 include的配置
    你可以将配置分割成多个
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">
   
       
       
       
       
       
   

    6 interceptors的配置
    对拦截类的配置 还有堆栈 堆栈里有对个拦截 就是这个意思
   
     
     
       
       
     

   

    7 Action 的配置 这个比较关键 也比较常用。与struts 1 的不同就是 这个action 把原来的action和form 都包含了
       (1) action mapping  就是action类的对应名字
   
      Menu
      /Logon.jsp
   

       (2) Action Methods
     默认是execute的方法,可以通过method参数访问多个action的方法,即 action可以有多个方法 可以扩展

   
    即时访问这个类的delete方法
    Wildcard Method

   

    即时访问deleteCrud 访问deleteCrud 方法
    即时访问eCrud 访问eCrud 方法
    如此对应
     (3) action default
    

       

       
            /UnderConstruction.jsp
       

       (4) action wilddefault
   
      /{1}.jsp
   

       (5 )  扩展映射
     
            name="/edit*"
        class="org.apache.struts.webapp.example.Edit{1}Action">
                    name="failure"
            path="/mainMenu.jsp"/>
                    path="/{1}.jsp"/>
   


   
      list{1}s.jsp
   

   
      listSpons.jsp
   

 

    Wildcard patterns can contain one or more of the following special tokens:
    * Matches zero or more characters excluding the slash ('/') character. ** Matches zero or more characters including the slash ('/') character. \character The backslash character is used as an escape sequence. Thus '\*'matches the character asterisk ('*'), and '\\'matches the character backslash ('\').

  8 Result 的配合 就是返回也的配置 与struts1是一一致的

    struts2一些固有的返回类型

      (1) Chain Result  就是返回一个类里 同时可以带参数进去
       
       
            login
       

       
           
           
                dashboard
                /secure
           

       

   

   
       
            dashboard.jsp
       

   

    (2)Dispatcher Result
    就是个跳转 不过可以带着request的值  sendredirect 便不会带着request 只能是session的作用域
    (3)FreeMarker Result 返回ftl 格式的模板foo.ftl

    (4)
    HttpHeader Result就是对http头 改变

   
      204
      a custom header value
      another custom header value
   

   
      305
      this action must be accessed through a prozy
   

    (5)
    Redirect Result前面说个 不能传递request的值
   
    <-- Pass parameters (reportType, width and height) -->
   
   
   
    generateReport.jsp
    /genReport
    pie
    100
    100
   

   

   

    (6)redirect action 就跳到action里

   
       
           
           
                dashboard
                /secure
           

       

   

   
        <-- Redirect to an action in the same namespace -->
       
            dashboard.jsp
            error
       

       
            error.jsp
       

   

   
       <-- Pass parameters (reportType, width and height) -->
      
      
         
             generateReport
             /genReport
             pie
             100
             100
         

      

   

(7) streat result 跳到流文件
   
      image/jpeg
      imageStream
      filename="document.pdf"
      1024
   

    (8)Velocity Result vm模板
      foo.vm
   

    (9)xslt
    用xslt 格式化输出
   
      foo.xslt
      ^/result/[^/*]$
      .*(hugeCollection).*
   

    (9)PlainText Result
      /myJspFile.jsp
   


   
     
         /myJspFile.jsp
         UTF-8
     

   
二struts-default.xml的配置 是一些初始化的配置参数A base configuration file named struts-default.xml is included in the struts2.jar file. This file is automatically included into struts.xml file to provide the standard configuration settings without having to copy them.


 

阅读(2046) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~