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 ...
随机推荐
- H5的新应用-使用Web Worker为学生考试计时
-------------------------- <script type="text/javascript"> //初始化函数 ...
- find文件后cp、rm
复制: find <src-path> -name 'some names' -exec cp {} <dest-path> \; find <src-path> ...
- golang vs java
http://programmers.stackexchange.com/questions/83780/how-fast-can-go-go In terms of language design, ...
- ios中的关键词@property @synthesize
@interface Person : NSObject{ int myNumber;} @property(nonatomic) int myNumber;//这个关键字是可以带套get 与s ...
- CentOS ulimit
Linux 文件系统设置 文件句柄数 ulimit -n 查看 echo "* - nofile 65536" >> /etc/security/limits.conf ...
- hostent h_addr_list
struct hostent { char FAR * h_name; /* official name of host */ char FAR * FAR * h_aliases; /* alias ...
- Qt之操作系统环境
来源:http://blog.sina.com.cn/s/blog_a6fb6cc90102uy9k.html Qt中操作系统环境,官方解释如下: QStringList QProcess::syst ...
- error while loading shared libraries: libmcrypt.so.4
/usr/local/php/sbin/php-fpm: error while loading shared libraries: libmcrypt.so.4: cannot open share ...
- C++随机崩溃捕捉处理
1. 会引起异常的几个原因(主要记录目前遇到过的几个问题) 程序读取了无效的内存地址 堆栈的溢出,比如无限循环导致那段内存溢出,比如把size为20的缓存拷贝到size为10的缓存块等 无法申请到有效 ...
- Servlet Filter 过滤器
Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源: 例如Jsp, Servlet, 静态图片文件或静态 ht ...