struts2 --- 注解详解

数据结构与算法——7-2 一元多项式的乘法与加法运算 (20分) 设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。 输出格式: 输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。 输入样例: 4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1 输出样例: 15 24 -25 22 30 21 -10 2 阅读详情

一、引入struts2支持注解开发的jar包

  • struts2-convention-plugin-2.3.24.jar


二、struts2使用注解开发需要遵循一些规范

  • Action必须要继承ActionSuppert父类
  • Action所在的包名必须以.action结尾


三、struts2 convention插件的常量介绍

  • struts.convention.result.path : 设置Convention插件定位视图资源的根路径。默认值为/WEB-INF/content
  • struts.convention.result.flatLayout : 如果是为false则可以将试图放置Action对应的目录下,无需放入WEB-INF/content
  • struts.convention.package.locators : 包的结尾名称必须是指定的名称(action,actions,struts,struts2),这样struts2才能进行进行注解扫描
  • struts.convention.exclude.packages : 扫描排除的包(org.apache.struts.*,org.apache.struts2.*)


四、注解详解

4.1 Convention的Annotation

与Action相关的两个Annotation是@Action和@Actions

(1)@Action中可指定一个value属性。@Action中还可以指定一个params属性,该属性是一个字符串数组,用于该Acion指定的参数名和参数值。params属性应遵守如下格式:{“name1”,”value1”,”name2”,”value2”}

示例:

package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
  
public class HelloWorld extends ActionSupport {  
  @Action("/different/url")  
  public String execute() {  
    return SUCCESS;  
  
  }  
  
  @Action("url")  
  public String doSomething() {  
    return SUCCESS;  
  }  
}  


使用@Action的interceptorRefs 属性可以指定action或者方法的interceptor,如下面的例子

package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
  
public class HelloWorld extends ActionSupport {  
  @Action(interceptorRefs={@InterceptorRef("validation"), @InterceptorRef("defaultStack")})  
  public String execute() {  
    return SUCCESS;  
  }  
  
  @Action("url")  
  public String doSomething() {  
    return SUCCESS;  
  }  
}  

还可以使用params属性指定要传给拦截器的参数。形式为{键,值,键,值…………},键值总是会成对出现,如下面的例子

package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
  
public class HelloWorld extends ActionSupport {  
  @Action(interceptorRefs=@InterceptorRef(value="validation",params={"programmatic", "false", "declarative", "true}))  
  public String execute() {  
    return SUCCESS;  
  }  
  
  @Action("url")  
  public String doSomething() {  
    return SUCCESS;  
  }  
}  


(2)@Actions 也用于修饰Action类里的方法,用于将该方法映射到多个@Action.因此它可将一个方法映射成多个逻辑Action。

示例:
package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
  
public class HelloWorld extends ActionSupport {  
  @Actions({  
    @Action("/different/url"),  
    @Action("/another/url")  
  })  
  public String execute() {  
    return SUCCESS;  
  }  
}  


4.2 Result的Annotation

(1)@Results用于组织多个@Result因此它只需指定一个value属性值,该value属性值为多个@Result

示例:

package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
import org.apache.struts2.convention.annotation.Result;  
import org.apache.struts2.convention.annotation.Results;  
  
@Results({  
  @Result(name="failure", location="fail.jsp")  
})  
public class HelloWorld extends ActionSupport {  
  @Action(value="/different/url",   
    results={@Result(name="success", location="http://struts.apache.org", type="redirect")}  
  )  
  public String execute() {  
    return SUCCESS;  
  }  
  
  @Action("/another/url")  
  public String doSomething() {  
    return SUCCESS;  
  }  
}  

(2)@Result相当于struts.xml文件中的<result/>元素。

  • 使用@Result必须指定一个name属性,相当于<result name=””/>。
  • type 相当于<result type=””/>指定返回视图资源的类型
  • location 相当于<result>…..</result>中间部分,用于指定实际视图位置
  • params:该属性相当于<result/>元素里多个<param../>子元素的作用,用于为该Result指定参数值。该属性应满足{“name1”,”value1”,”name2”,”value2”}格式

@Result有以下两种用法:

  • Action级的Result映射:以@Actions组合多个@Action后修饰的Action类。这种Result映射对该Action里的所有方法都有效。
  • 方法级Result映射:将多个@Result组成数组后作为@Action的results属性值。这种Result映射仅对被修饰的方法有效。

(3)@ResultPath则用于修饰包和Action类,用于改变被修饰Action所对应的物理视图资源的根路径。举例说:默认情况下,Convention都会到WEB-INF/content路径下找物理视图资源,一旦我们使用@ResultPath(“/abc”)修饰该Action,系统将回到abc目录下寻找物理视图资源。


package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
import org.apache.struts2.convention.annotation.Result;  
import org.apache.struts2.convention.annotation.Results;  
  
public class HelloWorld extends ActionSupport {  
  @Action(value="/different/url",   
    results={@Result(name="success", type="httpheader", params={"status", "500", "errorMessage", "Internal Error"})}  
  )  
  public String execute() {  
    return SUCCESS;  
  }  
  
  @Action("/another/url")  
  public String doSomething() {  
    return SUCCESS;  
  }  
}  

4.3 与包和命名空间相关的Annotation

(1)@Namespace:修饰Action类或其所在的包。该Annotation中指定一个value属性值,用于指定被修饰的Action所在的命名空间

(2)@Namespaces:修饰Action类或其所在的包,用于组合多个@Namespace

(3)@ParentPackage: 用于指定被修饰的Action所在包的夫包。


示例:

@Namespace("/")
@ParentPackage("struts-default")
public class UserAction extends ActionSupport implements ModelDriven<User> {

	private User user = new User();

	@Override
	public User getModel() {
		return user;
	}

	@Action(value = "user_login", results = { @Result(name = "success", location = "/product.jsp"),
			@Result(name = "error", location = "/login.jsp") })
	public String login() {

		// 调用service完成操作
		IUserService userService = new UserServiceImpl();
		User _user = userService.login(user.getUsername(), user.getPassword());
		if (_user != null) {
			// 将用户存储到session中
			ServletActionContext.getRequest().getSession().setAttribute("user", _user);
			return "success";
		} else {
			this.addActionError("用户名或密码错误");
			return "error";
		}
	}
}

4.4 异常处理相关的Annotation

(1)@ExceptionMappings 用于组织多个@ExceptionMapping,因此它只需指定一个value属性值,该value属性值为多个@ExceptionMapping。

(2)@ExceptionMapping 用于定义异常类和物理视图之间的对应关系,也相当于struts.xml文件里<exception-mapping../>元素的作用 使用时,必须注意以下两个属性:

  • exception: 用于指定异常类
  • result : 用于指定逻辑视图

@ExceptionMpping有如下两种用法

  • Action级的异常定义:以@ExceptionMappings组合多个@ExceptionMapping后修饰的Action类。这种异常定义对Action中的所有方法有效
  • 方法级的异常定义: 将多个@ExceptionMapping组成数组后作为@Action的exceptionMappings属性值,这种异常定义仅对被修饰的方法有效。

4.5 拦截器相关的Annotation

与拦截器配置的Annotation有@InterceptorRef、@InterceptorRefs和@DefaultInterceptorRef

(1)@InterceptorRefs用于组织多个@InterceptorRef,因此它只需要指定一个value属性值,该value属性值为多个@InterceptorRef

(2)@InterceptorRef用于为指定Action引用拦截器或者是拦截器栈。也就相当于strut.xml中位于<action../>元素内部的<interceptor-ref../>子元素的作用。

(3)@InterceptorRefAnnotation时,必须制定一个value属性,用于指定所引用的拦截器或拦截器栈的名字。相当于<interceptor-ref../>子元素里name属性的作用。


示例:

package com.example.actions;  
  
import com.opensymphony.xwork2.ActionSupport;   
import org.apache.struts2.convention.annotation.Action;  
import org.apache.struts2.convention.annotation.Actions;  
  
@InterceptorRefs({  
    @InterceptorRef("interceptor-1"),  
    @InterceptorRef("defaultStack")  
})  
public class HelloWorld extends ActionSupport {  
  @Action(value="action1", interceptorRefs=@InterceptorRef("validation"))  
  public String execute() {  
    return SUCCESS;  
  }  
  
  @Action(value="action2")  
  public String doSomething() {  
    return SUCCESS;  
  }  
}  









[QMT量化交易小白入门]-105、震荡市最适合的策略:ETF多标的动态网格策略,回测年化收益47.81% 本专栏主要是介绍QMT的基础用法,常见函数,写策略的方法,也会分享一些量化交易的思路,大概会写200篇左右。QMT的相关资料较少,在使用过程中不断的摸索,遇到了一些问题,记录下来和大家一起沟通,共同进步。 阅读详情

相关推荐

优秀的 Verilog/FPGA开源项目介绍(二十六)- ISP (图像信号处理)

介绍查看《ISP算法及架构分析介绍》今天项目的顺序就是先介绍几个关于ISP算法架构的项目,包括python、matlab、c等高级语言实现的ISP算法架构,最后介绍一个FPGA实现ISP的项目。openISPhttps://github.com/cruxopen/openISP项目介绍用python实现的ISP架构,该架构涉及到的知识如下:下图是最新实现的模块:主要模块如...

OpenFPGA的博客 4871

struts2注解总结----@Action和@Result

除了使用配置文件配置之外,还能够使用注解来配置 以下是一些经常使用的注解 介绍: @Action/@Actions: @Action指定一个类为action,相应配置文件里的&lt;action&gt;....&lt;/action&gt;标签,当中能够配置例如以下属性 results:配置返回的结果集属性,相当于struts2中的&lt;result&gt;列表,能够在...

weixin_34310785的博客 522

基于 YOLOv26 的食品包装缺陷检测系统:智能视觉质检方案

本文提出了一种基于YOLOv26深度学习的食品包装缺陷智能检测系统。该系统能够自动识别多种常见包装缺陷,包括密封不良、破损、污染、标签错误等8类问题。相比传统人工检测方法,该系统具有高精度、多类别分类、实时推理等优势。文章详细介绍了系统架构,包含数据采集、预处理、YOLOv26检测模型和后处理四个核心模块,并提供了完整的环境配置和数据处理代码实现。该系统为食品包装行业提供了一种高效、准确的智能视觉质检解决方案。

一键难忘的博客 639

Struts2之Result详解

  上一篇我们把Struts2中的Action接收参数的内容为大家介绍了,本篇我们就一起来简单学习一下Action的4种Result type类型,分为:dispatcher(服务端页面跳转);redirect(客户端页面跳转);chain(动作链跳转);redirectAction(客户端Action跳转),当然还有其他类型,这里我们就以这四种为例为大家介绍一下,其他几种大家有兴趣单独学习。下面...

weixin_33705053的博客 267

Struts2注解(annotation)方式下chain的设置方式

最近做一个项目,Struts2+Spring+Hibernate这三个开源框架下的,基本上抛弃了传统的XML配置方式,采用了注解(annotation),的确注解减轻了程序员很多工作量,注入也变得简单多了,只是有些细节还在慢慢适应中: 先来说个“chain”方式跳转的问题 action的访问,我们采用了动态方法调用的方式来访问,也就是使用叹号“!”,有些需求是需要跳转到另一个a

itjoy的专栏 5183

struts2零配置中@result跳转总结

key...

KEYUAN358的专栏 802

Struts2注解详解

使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了。  要使用注解方式,我们必须添加一个额外包:struts2-convention-plugin-2.x.x.jar。  虽说是零配置的,但struts.xml还是少不了的,配置如下:  <!DOCTYPE struts PUBLIC "-//Ap

engineer_hzl 3982

struts2注解详解

一、配置web.xml &lt;filter&gt; &lt;filter-name&gt;struts2&lt;/filter-name&gt; &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt; &lt;in...

jinvasshole 102

Struts2 注解详解

给大家推荐个靠谱的公众号程序员探索之路,大家一起加油  使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了。  要使用注解方式,我们必须添加一个额外包:struts2-convention-plugin-2.x.x.jar。  虽说是零配置的,但struts.xml还是少不了的,配置如下:   &lt;?xml versio...

yueloveme的博客 334

深入详解Struts2——使用注解配置Action

注解的作用就是代替传统的xml配置文件,struts2提供了注解功能,这样我们就不需要再配置struts.xml文件。与Action相关的注解有: ParentPackage:指定action所在包要继承的包 Namespace:所属action的命名空间 Result:定义一个result映射 Results:定义一组result映射 下面看一个注解配置Action的实例:@Namesp

路漫漫,水迢迢 1582

Struts2注解功能详解

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34409703的博客 108

【IT之路】Struts2注解详解:Action注解

一、方法名实现动态方式的注解(推荐) 1、在action类前添加注解:@Action(value="") 示例: 2、调用方式 http://主机:端口号/项目名/Namespace指定名/Action指定名!动态方法名.后缀扩展名 示例: http://localhost:8080/StandardMavenDemo1/strutsDemo/userAction!addUs...

七星云-恒 1679

示例详解struts2注解

使用注解完成一个Action的流程必须要如下的7个jar包 1.commons-fileupload-1.2.1.jar 2.commons-io-1.3.2.jar 3.freemarker-2.3.15.jar 4.ognl-2.7.3.jar 5.struts2-convention-plugin-2.1.8.1.jar 6.struts2-core-2.1.8.1.

岁寒松柏 651

struts2的json插件详解 注解

如果我们要在struts2中使用JSON的功能,需要引入struts2-json-plugin-2.3.4.1.jar, 如果使用注解的方式配置JSON,还需要引入 struts2-convention-plugin-2.3.4.1.jar commons-io-2.0.1.jar 这两个包 使用注解的方式配置需要注意两点 1.包必须继承 @Parent

u013444177的专栏 3785

java脚本 action_struts2中使用注解配置Action方法详解

使用注解来配置Action可以实现零配置,零配置将从基于纯XML的配置转化为基于注解的配置。使用注解,可以在大多数情况下避免使用struts.xml文件来进行配置。struts2框架提供了四个与Action相关的注解类型,分别为ParentPackage、Namespace、Result和Action。ParentPackage:ParentPackage注解用于指定Action所在的包要继承的父...

weixin_39953102的博客 390

struts注解 ajax,struts2实现ajax校验的2种方法详解

共同的一点是,Action都需要将一个方法暴露出来,给前端javascript调用javascript的代码都是一样的:Js代码 functiontestAjax(){var$userNameInput=$("#ajax_username");varuserName=$userNameInput.val();$.ajax({url:"originAjax.action",type...

weixin_30903865的博客 310

2024新版周易测算系统源码

2024新版周易测算系统源码更新了三套首页新ui 自己后台切换就行需要2个域名,前后台各一个域名!后台域名绑定到根目录,测算域名绑定到子目录ffsm1、导入数据库和源码2、配置数据库账号密码config/inc_config.php 70行左右3、登录后台:域名/acs账号:admin密码:1234564、登录后台在后台网站域名改为自己前台域名!!!!一定得得更改,否则无法支付!

Chrome浏览器页面崩溃,STATUS_INVALID_IMAGE_HASH

在85,87版本上测试可用问题:Chrome浏览器所有页面崩溃,“喔唷,崩溃啦!( STATUS_INVALID_IMAGE_HASH )”原因:Google在79版本(2019年1220号左右)的更新中又重新启用了Renderer Code Integrity Protection(渲染器代码完整性保护),会阻止签名不是谷歌和微软的模块加载。该功能已经在之前一个版本中导致同样的问题,并由Google自己禁用了。解决:禁用谷歌chrome的这项功能: RendererCodeIntegrityEnabled,下载到电脑中双击运行

上一篇: struts2 --- ajax的三种实现方式
下一篇: Hibernate --- 核心API
__静禅__
博客等级 码龄9年 132粉丝 247原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值