struts2学习笔记 -- day10 Struts2中的注解
来源:互联网 发布:毛笔书法软件下载 编辑:程序博客网 时间:2024/05/16 15:09
一、strus2中的注解
1、使用前提
首先我们要明确一件事:注解和XML的配置,都是告知struts2框架,当我们jsp页面发送请求,根据配置执行对应动作类的方法,并根据返回值,前往指定的结果视图(jsp页面或者其他动作)。它们只是配置的形式不一样。
其次要想使用struts2的注解,必须要导入一个新的jar包。该jar包是:
2、常用的注解
@NameSpace
出现的位置:它只能出现在package上或者Action类上。一般情况下都是写在Action类上。
作用:指定当前Action中所有动作方法的名称空间。
属性:
value:指定名称空间的名称。写法和xml配置时一致。不指定的话,默认名称空间是""。
@ParentPackage
出现的位置:它只能出现在package上或者Action类上。一般情况下都是写在Action类上。
作用:指定当前动作类所在包的父包。由于我们已经是在类中配置了,所以无需在指定包名了。
属性:
value:指定父包的名称。
@Action
出现的位置:它只能出现在Action类上或者动作方法上。一般情况下都是写在动作方法上。
作用:指定当前动作方法的动作名称。也就是xml配置时action标签的name属性。
属性:
value:指定动作名称。
results[]:它是一个数组,数据类型是注解。用于指定结果视图。此属性可以没有,当没有该属性时,表示不返回任何结果视图。即使用response输出响应正文。
interceptorRefs[]:它是一个数组,数据类型是注解。用于指定引用的拦截器。
@Result
出现的位置:它可以出现在动作类上,也可以出现在Action注解中。
作用:
出现在类上,表示当前动作类中的所有动作方法都可以用此视图。
出现在Action注解中,表示当前Action可用此视图。
属性:
name:指定逻辑结果视图名称。
type :指定前往视图的方式。例如:请求转发,重定向,重定向到另外的动作。
location:指定前往的地址。可以是一个页面,也可以是一个动作。
@Results
出现的位置:它可以出现在动作类上,也可以出现在Action注解中。
作用:用于配置多个结果视图。
属性:
value:它是一个数组,数据类型是result注解。
@InterceptorRef
出现的位置:它可以出现在动作类上或者Action注解中。
作用:用于配置要引用的拦截器或者拦截器栈
属性:
value:用于指定拦截器或者拦截器栈
3、动作类注解使用
import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import com.opensymphony.xwork2.ActionSupport;/** * 使用注解配置动作类* @Description: TODO* @author wingzhe * @date 2017年8月13日 上午9:33:27 * @version V1.0 */@ParentPackage("struts-default") //配置继承的包@Namespace("/cust") //配置命名空间public class UserAction extends ActionSupport {/** * 动作方法 * @return */@Action(value="login", //动作类的name属性值results={ //结果视图@Result(name="success",type="dispatcher",location="/success.jsp"),@Result(name="login",type="redirect",location="/login.jsp")})public String login(){return SUCCESS;}}
4、拦截器注解使用
(1)、写在Action上:整个Action都能用
(2)、写在方法上:当前方法能用
二、使用注解实现案例
1、在web.xml中配置开发者模式
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 配置过滤器初始化参数 --> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> </filter>
2、使用注解编写动作类
package cn.itcast.web.action;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import cn.itcast.domain.Customer;import cn.itcast.service.ICustomerService;import cn.itcast.service.impl.CustomerServiceImpl;/** * 客户的动作类 * @author zhy * */@ParentPackage("struts-default")@Namespace("/customer")public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {private Customer customer = new Customer();private ICustomerService customerService = new CustomerServiceImpl();@Overridepublic Customer getModel() {return customer;}/** * 保存客户 * @return */@Action(value="addCustomer",results={@Result(name="addCustomer",type="redirect",location="/jsp/success.jsp")})public String addCustomer(){//1.调用业务层方法执行保存customerService.saveCustomer(customer);return "addCustomer";}/** * 获取添加客户页面 * @return */@Action(value="addUICustomer",results={@Result(name="addUICustomer",type="dispatcher",location="/jsp/customer/add.jsp")})public String addUICustomer(){return "addUICustomer";}/** * 查询所有客户 * @return */private List<Customer> customers;@Action(value="findAllCustomer",results={@Result(name="findAllCustomer",location="/jsp/customer/list.jsp")})public String findAllCustomer(){//1.调用业务层方法,查询所有客户customers = customerService.findAllCustomer();return "findAllCustomer";}public List<Customer> getCustomers() {return customers;}public void setCustomers(List<Customer> customers) {this.customers = customers;}}
- struts2学习笔记 -- day10 Struts2中的注解
- Struts2学习笔记--注解
- struts2学习笔记 -- day05 Struts2中的标签
- struts2中的注解应用
- struts2注解笔记1
- struts2学习--注解开发
- struts2学习笔记(13)——struts2中的国际化
- struts2学习笔记(6)------配置struts2中的异常处理
- spring学习笔记 -- day10 spring中的jdbcTemplate
- struts2学习笔记--struts2配置
- Struts2学习笔记--安装struts2
- struts2注解中的params传参
- Struts2.2.1+Hibernate3.6+spring3.1注解开发学习笔记
- [Struts2] struts2 @Inject 注解
- struts2注解
- struts2注解
- struts2 注解
- struts2 注解
- 【STL】hdu 5688 Problem D
- Linux设置mysql 表明区分大小写
- Python一些库的安装
- struts2学习笔记 -- day09 Struts2的拦截器
- 立方和
- struts2学习笔记 -- day10 Struts2中的注解
- Android Studio程序无法加载到虚拟机解决方法
- 51nod 1475 建设国家
- tomcat下jndi的三种配置方式
- 编程思想——第三章控制流程
- flex布局学习笔记
- poj1177 Picture 离散 注意排序
- Codeforces-Problem 732D Exam
- hdu 6069 Counting Divisors