struts2--package配置

本文详细介绍了Struts2框架中的包配置概念,包括如何通过包来组织动作、拦截器等配置,实现继承与覆盖功能。文章还展示了具体的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Packages are a way to group actions, results, result types, interceptors, and interceptor-stacks into a logical configuration unit. Conceptually, packages are similiar to objects in that they can be extended and have individual parts that can be overridden by "sub" packages.

Packages

The package element has one required attribute, name, which acts as the key for later reference to the package. The extends attribute is optional and allows one package to inherit the configuration of one or more previous packages - including all interceptor, interceptor-stack, and action configurations.

(warning) Note that the configuration file is processed sequentially down the document, so the package referenced by an "extends" should be defined above the package which extends it.

The optional abstract attribute creates a base package that can omit the action configuration.(没有action定义)

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)

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

Inherit from more than one package

Multi package Example (struts.xml)
<struts>
   < package name="employee" extends ="struts- default , json- default " namespace="/employee">
 
     <action name="list" method="list" class ="org.apache.struts2.showcase.action.EmployeeAction" >
         <result>/empmanager/listEmployees.jsp</result>
         <result type="json">
             <param name="root">employees</param>
         </result>
     </action>
 
   </ package >
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值