Struts2利用注解实现action跳转
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了。
要使用注解方式,我们必须添加一个额外包:struts2-convention-plugin-2.x.x.jar。
实例为例,供参考:
- <?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配置。
总结常用的注解如下:
Struts2利用注解实现action跳转的更多相关文章
- Struts2基于注解的Action配置
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...
- struts2基于注解的action
使用注解来配置Action的最大好处就是可以实现零配置,但是事务都是有利有弊的,使用方便,维护起来就没那么方便了. 要使用注解方式,我们必须添加一个额外包:struts2-convention-plu ...
- struts2基于注解配置action
如果使用struts2,就需要配置文件或者注解,关于struts2的配置文件struts.xml非常熟悉,对于注解可能spring使用的比较多.配置文件的繁琐衬托出了注解的简洁方便,一条或者几条注解解 ...
- Struts2 从一个Action跳至另一个Action
Struts2 从一个Action跳至另一个Action 一.注解的 @Result(name=SUCCESS,type="chain", params={"actio ...
- struts/Servlet,action转到jsp后,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)
问题:使用struts2,如何处理action的路径?还有,在action转到的jsp中,如何写js,css,图 片的路径?(例如访问 http://localhost/project/listUse ...
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- struts2学习笔记之十四:使用注解配置Action(不是和spring集成使用)
Struts2支持使用注解配置Action,减少配置文件的配置 Struts2如果要支持注解配置Action,需要插件的支持,导入插件struts2-convention-plugin-2.1.8.1 ...
- Struts/Servlet,action转到jsp后,CSS失效,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)
问题:使用struts2,如何处理action的路径?还有,在action转到的jsp中,如何写js,css,图 片的路径?(例如访问http://localhost/project/listUser ...
- struts2使用注解--ACTION中的应用
1.在类中指定包:@ParentPackage("system").其中system是在struts.xml中定义的包名.2.配置文件--->注解2.1配置文件方式(返回js ...
随机推荐
- MySQL 不允许从远程访问的解决方法
解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 ...
- 【转】Ecshop 后台增加一个左侧列表菜单menu菜单的方法
cshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages/ ...
- Linux KVM 安装配置
--------------------------一.前言二.环境三.安装与配置四.创建kvm虚拟机 一.前言 KVM,即Kernel-based Virtual Machine的简称,是一个开源的 ...
- adb 卸载android系统程序
下面是通过 pm list packages -f 列出手机中的软件,然后跟模拟器中的软件进行对比后得出的可以安全卸载的列表. 注意:卸载之后就没有Google Market了,还想用google ...
- Oracle where 0=1 or 1=1
本文转载自:http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html sql where 1=1和 0=1 的作用 where 1 ...
- no ia32-libs
sudo apt-get install lib32stdc++6 sudo apt-get install lib32z1 http://blog.csdn.net/wangbin_jxust/ar ...
- 关于项目刚才还能运行的,重启Myeclipse就不能运行(报错)的解决方法
这个是以为内存不足引起的,就是打开MyEclipse的时候,因为内存不足,没有加载完整的项目,这个时候需要重启电脑,即可解决问题.
- 转 : ANT 调用sqlplus 客户端
Jenkins安装与配置 (版本控制) 1 用ant脚本执行sql语句现在我需要写一个ant脚本来实现项目安装,情况是这样的,客户现在运行的版本可能是2.0,安装目录里可能有REL_1_0,REL_ ...
- CSS3的一些前缀
为了兼容多个浏览器,css3通常前面加一大堆前缀 -webkit /*为Chrome/Safari*/-moz /*为Firefox*/-ms /*为IE*/-o /*为Opera*/ -w ...
- OpenGL 图形管道(graphics pipeline)过程
1.总结:Graphics pipeline 主要分为两部分工作 把3D坐标转换成2D坐标 把2D坐标转换成真实的有颜色的像素 2.下图就是一个顶点数据经过几个步骤后转化成显示在屏幕上像素的过程(一般 ...