使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了。
要使用注解方式,我们必须添加一个额外包:struts2-convention-plugin-2.x.x.jar。
虽说是零配置的,但struts.xml还是少不了的,配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 请求参数的编码方式-->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开-->
<constant name="struts.action.extension" value="action,do,htm"/>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.devMode" value="false"/>
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 指定由spring负责action对象的创建
<constant name="struts.objectFactory" value="spring" />
-->
<!-- 是否开启动态方法调用-->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
</struts>
action类的注解:
package com.tjcyjd.web.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
/**
* Struts2基于注解的Action配置
*
*/
@ParentPackage("struts-default")
@Namespace("/annotation_test")
@Results( { @Result(name = "success", location = "/main.jsp"),
@Result(name = "error", location = "/error.jsp") })
@ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 2730268055700929183L;
private String loginName;
private String password;
@Action("login") //或者写成 @Action(value = "login")
public String login() throws Exception {
if ("yjd".equals(loginName) && "yjd".equals(password)) {
return SUCCESS;
} else {
return ERROR;
}
}
@Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })
public String add() throws Exception {
return SUCCESS;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password= password;
}
}
这样就完成了一个基于注解的action配置。
总结常用的注解如下:
Namespace:指定命名空间。
ParentPackage:指定父包。
Result:提供了Action结果的映射。(一个结果的映射)
Results:“Result”注解列表
ResultPath:指定结果页面的基路径。
Action:指定Action的访问URL。
Actions:“Action”注解列表。
ExceptionMapping:指定异常映射。(映射一个声明异常)
ExceptionMappings:一级声明异常的数组。
InterceptorRef:拦截器引用。
InterceptorRefs:拦截器引用组。
分享到:
相关推荐
Struts 2使用注解配置Action,不配置struts.xml,通过注解直接配置action
struts2注解配置Action及拦截器几种不同方式写法对应的路径指向.docx
使用struts2基于注解(零配置)实现的文件上传与下载的代码,可以正常运行
struts2 使用注解现在零配置不需要在使用struts.xml配置文件。 struts2 注解实例。可以直接跑
都被它莫名其妙的错误搞的郁闷,而网上关于这方面的东西大多都是基于struts2.0版本的,对我们现在用的2.1以上的版本不起什么作用,所以特整理出一份文档,里面详细说明了怎样用注解出配置struts2的action,...
主要介绍了struts2中使用注解配置Action方法详解,涉及一个示例,具有一定参考价值,需要的朋友可以了解下。
而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行配置,甚至不需要使用Annotation进行配置,而是由struts2根据约定...
基于注解的Action名• 基于注解的拦截机(Interceptor)• 基于注解的命名空间(Nameespace)• 基于注解的XWork包• 默认action以及默认的结果(比如:/products 将会尝试寻找com.example.actions.Products ...
Struts2的框架及注释和使用法,希望大家支持,我们一起努力,谢谢!
struts2将请求在Action中封装为Map并将配置文件放到web-info中还可以自定义配置文件位置就是不将struts.xml放到src下但还是不如spring mvc 的封装来得方便
第1章 Struts 2:现代Web框架 2 1.1 Web应用程序:快速学习 2 1.1.1 构建Web应用程序 2 1.1.2 基础技术简介 3 1.1.3 深入研究 6 1.2 Web应用程序框架 7 1.2.1 什么是框架 7 1.2.2 为什么使用框架 8 1.3 Struts 2框架...
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...
使用的是注解的方式完成的,因为注解的方式可以减少一些配置文件,比较方便的。 【特别强调】一定要清楚如何调试项目,因为自己的环境和下载的资源的环境可能不一样,需要修改配置文件或是其他才能正常运行。...
使用注解配置Action示例。里面已经包涵java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils错误异常的解决方案。
Struts2直接使用注解的详细配置action 去掉Struts.xml 省去多余的代码 让代码更直观
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 • 包命名习惯来指定Action位置 • 命名习惯制定结果(支持JSP,FreeMarker等)路径 • 类名到URL的约定转换 • 包名...
求有爱的大大帮忙解决一下strut2和sprin3注解找不到action的问题
ssh2注解配置,全部是注解配置,struts2和hibernate3和spring2.5全部是注解配置,,访问路径为http://localhost:8080/mytest/student/findAll.action
struts2 注解与Action相关的两个Annotation是@Action 和@Actions2)@Action中可指定一个value属性。类似于指定<action name=””/>属性值