struts 2 学习

struts2 学习

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

  1。 bean的配置

      主要就是名字的一个引用

<struts>

  <bean type="com.opensymphony.xwork2.ObjectFactory" name="myfactory" class="com.company.myapp.MyObjectFactory" />
  
  ... 

</struts>

The bean element has one required attribute, class, which specifies the Java class to be created or manipulated. A bean can either

  1. be created by the framework's container and injected into internal framework objects, or
  2. 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)
<struts>
  <package name="employee" extends="struts-default" namespace="/employee">
    <default-interceptor-ref name="crudStack"/>

    <action name="list" method="list"
      class="org.apache.struts2.showcase.action.EmployeeAction" >
        <result>/empmanager/listEmployees.jsp</result>
        <interceptor-ref name="basicStack"/>
    </action>
    <action name="edit-*" class="org.apache.struts2.showcase.action.EmployeeAction">
      <param name="empId">{1}</param>
      <result>/empmanager/editEmployee.jsp</result>
        <interceptor-ref name="crudStack">
          <param name="validation.excludeMethods">execute</param>
        </interceptor-ref>
      </action>
      <action name="save" method="save"
          class="org.apache.struts2.showcase.action.EmployeeAction" >
        <result name="input">/empmanager/editEmployee.jsp</result>
        <result type="redirect">edit-${currentEmployee.empId}.action</result>
      </action>
      <action name="delete" method="delete"
        class="org.apache.struts2.showcase.action.EmployeeAction" >
        <result name="error">/empmanager/editEmployee.jsp</result>
        <result type="redirect">edit-${currentEmployee.empId}.action</result>
      </action>
  </package>
</struts>
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)
<struts>

  <constant name="struts.devMode" value="true" />

  ... 

</struts>
Constant Example (struts.properties)
struts.devMode = true
Constant Example (web.xml)
<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">

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        <init-param>
        	<param-name>struts.devMode</param-name>
        	<param-value>true</param-value>
        </init-param>
    </filter>

    ...

</web-app>
4.Namespace 的配置
就是命名空间的配置 说白了就是你访问action 的前置url 有个默认空间 当你制定的空间找不到 就转到default里
<package name="default">
    <action name="foo" class="mypackage.simpleAction">
        <result name="success" type="dispatcher">greeting.jsp</result>
    </action>

    <action name="bar" class="mypackage.simpleAction">
        <result name="success" type="dispatcher">bar1.jsp</result>
    </action>
</package>

<package name="mypackage1" namespace="/">
    <action name="moo" class="mypackage.simpleAction">
        <result name="success" type="dispatcher">moo.jsp</result>
    </action>
</package>

<package name="mypackage2" namespace="/barspace">
    <action name="bar" class="mypackage.simpleAction">
        <result name="success" type="dispatcher">bar2.jsp</result>
    </action>
</package>
5 include的配置
你可以将配置分割成多个 
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="Home.xml"/>
    <include file="Hello.xml"/>
    <include file="Simple.xml"/>
    <include file="/util/POJO.xml"/>
    <include file="/com/initech/admin/admin-struts.xml"/>
</struts>
6 interceptors的配置
对拦截类的配置 还有堆栈 堆栈里有对个拦截 就是这个意思
<interceptors>
  <interceptor name="security" class="com.company.security.SecurityInterceptor"/>
  <interceptor-stack name="secureStack">
    <interceptor-ref name="security"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>
7 Action 的配置 这个比较关键 也比较常用。与struts 1 的不同就是 这个action 把原来的action和form 都包含了
   (1) action mapping  就是action类的对应名字
<action name="Logon" class="tutorial.Logon">
  <result type="redirectAction">Menu</result>
  <result name="input">/Logon.jsp</result>
</action>
   (2) Action Methods
	默认是execute的方法,可以通过method参数访问多个action的方法,即 action可以有多个方法 可以扩展
<action name="delete" class="example.CrudAction" method="delete">
即时访问这个类的delete方法
Wildcard Method
<action name="*Crud" class="example.Crud" method="{1}">
即时访问deleteCrud 访问deleteCrud 方法
即时访问eCrud 访问eCrud 方法
如此对应
 (3) action default
 <package name="Hello" extends="action-default">

    <default-action-ref name="UnderConstruction">

    <action name="UnderConstruction">
        <result>/UnderConstruction.jsp</result>
    </action>
   (4) action wilddefault
<action name="*">
  <result>/{1}.jsp</result>
</action>
   (5 ) 	扩展映射
	<!-- Generic edit* mapping -->
<action
    name="/edit*"
    class="org.apache.struts.webapp.example.Edit{1}Action">
    <result
        name="failure"
        path="/mainMenu.jsp"/>
    <result
        path="/{1}.jsp"/>
</action>
<action name="List*s" class="actions.List{1}s">
  <result>list{1}s.jsp</result>
</action>
<action name="ListSpons" class="actions.ListSpons">
  <result>listSpons.jsp</result>
</action>
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.
/characterThe 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 就是返回一个类里 同时可以带参数进去
<package name="public" extends="struts-default">
    <!-- Chain creatAccount to login, using the default parameter -->
    <action name="createAccount" class="...">
        <result type="chain">login</result>
    </action>

    <action name="login" class="...">
        <!-- Chain to another namespace -->
        <result type="chain">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
    </action>
</package>
(2)Dispatcher Result
就是个跳转 不过可以带着request的值  sendredirect 便不会带着request 只能是session的作用域
(3)
          
FreeMarker Result 返回ftl 格式的模板
<result name="success" type="freemarker">foo.ftl</result>
(4)
HttpHeader Result就是对http头 改变
<result name="success" type="httpheader">
  <param name="status">204</param>
  <param name="headers.a">a custom header value</param>
  <param name="headers.b">another custom header value</param>
</result>

<result name="proxyRequired" type="httpheader">
  <param name="error">305</param>
  <param name="errorMessage">this action must be accessed through a prozy</param>
</result>
(5)
Redirect Result前面说个 不能传递request的值
< package name= "passingRequestParameters" extends= "struts-default" namespace= "/passingRequestParameters"> <-- Pass parameters (reportType, width and height) --> <!-- The redirect-action url generated will be : /genReport/generateReport.jsp?reportType=pie&width=100&height=100 --> <action name= "gatherReportInfo" class= "..."> <result name= "showReportResult" type= "redirect"> <param name= "location">generateReport.jsp</param> <param name= "namespace">/genReport</param> <param name= "reportType">pie</param> <param name= "width">100</param> <param name= "height">100</param> </result> </action> </ package>
(6)redirect action 就跳到action里
<package name="public" extends="struts-default">
    <action name="login" class="...">
        <!-- Redirect to another namespace -->
        <result type="redirect-action">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <-- Redirect to an action in the same namespace -->
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
        <result name="error" type="redirect-action">error</result>
    </action>

    <action name="error" class="...">
        <result>error.jsp</result>
    </action>
</package>

<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <-- Pass parameters (reportType, width and height) -->
   <!--
   The redirect-action url generated will be :
   /genReport/generateReport.action?reportType=pie&width=100&height=100
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirect-action">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
      </result>
   </action>
</package>
(7) streat result 跳到流文件
<result name="success" type="stream">
  <param name="contentType">image/jpeg</param>
  <param name="inputName">imageStream</param>
  <param name="contentDisposition">filename="document.pdf"</param>
  <param name="bufferSize">1024</param>
</result>
(8)
                
Velocity Result vm模板
<result name="success" type="velocity">
  <param name="location">foo.vm</param>
</result>
(9)xslt  
用xslt 格式化输出
<result name="success" type="xslt">
  <param name="location">foo.xslt</param>
  <param name="matchingPattern">^/result/[^/*]$</param>
  <param name="excludingPattern">.*(hugeCollection).*</param>
</result>
(9)
                    
PlainText Result
<action name="displayJspRawContent" > <result type="plaintext">/myJspFile.jsp </result> </action> <action name="displayJspRawContent" > <result type="plaintext"> <param name="location">/myJspFile.jsp </param> <param name="charSet">UTF-8 </param> </result> </action>
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值