struts2配置文件-------------------------------------------*站位符{1}、{2}是分别取出通配符的值***常用配置方式:1)驼峰式:<action name="*User" class="com.cyjch.action.UserAction" method="{1}">    <result>/{1}UserSuc.jsp</result></action>

2)下划线式:<action name="*_*" class="com.cyjch.action.{1}Action" method="{2}">    <result>/{1}_{2}.jsp</result></action>

或:<action name="*_*" class="com.cyjch.action.{1}Action" method="{2}">    <result>/{0}.jsp</result></action>

//{0}表示action定义中的name值:*_* 

***Result1)服务器跳转 type="chain"<action name="test" class="com.cyjch.action.TestAction">    <result name="success" type="chain">        <param name="actionName">test</param>    </result></action>2)客户端跳转<action name="test" class="com.cyjch.action.TestAction">    <result name="success" type="redirectAction">        <param name="actionName">test</param>    </result></action>

注意:1)有通配符时,是按顺序来访问2)如果没有通配符时,优先级最高3)ACTION文件启名要以Action结尾

***OGNL应用:------------------------------- OGNL和EL区别EL在JSP中使用加${xxx},在STRUTS标签中使用OGNL表达式时不用加${xxx}直接写表达式即可标签获取request中的值如:<s:property value="user.username"/>调用值栈中的普通方法如:<s:property value="user.get()"/>//action中的属性会放到值栈中调用ACTION中的静态方法如:<s:property value="@com.cyjch.action.LoginAction@get()"/>调用JDK中类的静态方法如:<s:property value="@java.lang.Math@floor(4.6)"/>调用静态属性如:<s:property value="com.cyjch.action.LoginAction@USERNAME"/>//静态属性应该为public的调用普通的构造方法如:<s:property value="new com.cyjch.bean.Student('张三','23')/>

获取List集合:<s:property value="testList"/> //结果为:['list1','list2','list3']获取List集合中某一个元素:<s:property value="testList[0]"/> //结果为:list1获取Map集合中某一个元素:<s:property value="testMap['m1']"/> //结果为:map1获取Map集合中所有元素的键:<s:property value="testMap.keys"/> 获取Map集合中所有元素的值:<s:property value="testMap.values"/> 获取List集合中的所有对象:<s:property value="students"/> //students为List,需要set和get方法用投影获取List集合中所有对象的属性:<s:property value="students.{username}"/>//从集合中取出来一个子集合用投影(子集合)获取List集合中某一个对象的属性:<s:property value="students.{username}[0]"/>选择:? ^(开始) $(结束)用选择?(有条件)获取List集合中的成绩极格的对象:<s:property value="students.{?#this.grade>=60}"/>//#this为循环的当前对象用选择?(有条件)获取List集合中的成绩极格的第一个对象的username:<s:property value="students.{?#this.grade>=60}.{username}[0]"/>//#this为循环的当前对象用选择^(有条件)获取List集合中的成绩极格的第一个对象的username:<s:property value="students.{^#this.grade>=60}.{username}[0]"/>//#this为循环的当前对象用选择$(有条件)获取List集合中的成绩极格的最后一个对象的username:<s:property value="students.{$#this.grade>=60}.{username}[0]"/>//#this为循环的当前对象

***OGNL中的#的使用(可以取出堆栈上下文中的存放的对象,以下匀在<s:property value=""/>中使用)-------------------------------1)parameters 例:#parameters.id[0] <==> request.getParameter("id")

2)request 例:#request.userName <==> request.getAttribute("userName")

3)session 例:#session.userName <==> session.getAttribute("userName")

4)application 例:#application.userName <==> application.getAttribute("userName")

5)attr(默认按顺序去读取context/request/session/application四种中的值) 例:attr.username

***OGNL中%{}的使用(可以取出存在值栈中的Action对象,直接调用它的方法)------------------------------------例:ACTION如继承了ActionSupport那么可以用<s:property value="%{getText('key')}"/>的方式拿出国际化的信息

***OGNL中的$使用------------------------------------1)用于国际化资源文件中2)用于配置文件中

***valueStack为根对象,可以省写#---------------------------------<s:property value="user.username"/>

***服务器跳转时共用一个值栈

***使用top获取值栈中的第二个对象:<s:property value="[1].top"/>//[1]代表第二个对象

***使用N获取值栈中的第二个对象属性:<s:property value="[1].username"/>//[1]代表第二个对象

***使用@调用静态方法<s:property value="@vs1@getName()"/>//其中vs1代表第一个,vs2代表第二个栈中的Action

***自定义拦截器栈-------------------------<package name="test" extends="struts-defalut">    <interceptors>        <interceptor name="testInteceptor" class="com.cyjch.inteceptor.TestInteceptor"/>    </interceptors>

    <action name="login" class="com.cyjch.action.LoginAction">        <result name="success">/loginSuc.jsp</result>        <result name="error">/loginFail.jsp</result>        <interceptor-ref name="testInterceptor"/>        <interceptor-ref name="defaultStack"/>    </action></package>

代码:TestInteceptor.java

public class TestInteceptor extends AbstractInterceptor{

public String intercept(ActionInvocation invocation)throws Exception{

        System.out.println("TestInterceptor--begin");long startTime = System.currentTimeMillis();        String result = invocation.invoke();long endTime = System.currentTimeMillis();

long time = endTime- startTime;//共执行多少时间

        System.out.println("执行全部--end");        System.out.println("TestInterceptor--end");return result;    }}

***等待的拦截器(execAndWaitIntercept)-- 一般的拦截器在API文档中都有案例<action name="login" class="com.cyjch.action.loginAction">     <result name="wait">wait.jsp</result>     <result name="success" type="dispatcher">/loginSuc.jsp</result>     <interceptor-ref name="timer"/><!--可在控制台显示用的毫秒数-->     <interceptor-ref name="defaultStack"/>     <interceptor-ref name="execAndWait"><!--必须在最后一个拦截器-->    <param name="delay">3000</param><!--延时3秒-->     </interceptor-ref> </action>

wait.jsp---------------<%   response.setHeader("refresh", "3;URL=login.action");%><html>  <head>    <meta http-equiv="refresh" content="2;URL=login.action">  </head><body>    <h1>数据已经提交,正在等服务返回信息,请耐心等待。。。</h1></body></html>

***令牌<s:token> : 用来防止重复提交信息服务器产生令牌并放置到客户端一份,客户端提交信息后和服务器端令牌比较,如一致则正常跳转并清空令牌;客户端再后退提交则和服务器端令牌不一致,显示错误提示

<!--方法一:拦截器“token”重复提交后转向错误提示页面--><action name="login" class="com.cyjch.action.LoginAction">    <result name="success" type="dispatcher">loginSuc.jsp</result>    <result name="login">/login.jsp</result>    <result name="invalid.token">/repeaSubmitError.jsp</result>    <interceptor-ref name="token"></interceptor-ref>    <interceptor-ref name="defaultStack"></interceptor-ref></action>

<!--方法二:拦截器“tokenSession”重复提交后转向成功结果页面--><action name="login" class="com.cyjch.action.LoginAction">    <result name="success" type="dispatcher">loginSuc.jsp</result>    <result name="login">/login.jsp</result>    <result name="invalid.token">/repeaSubmitError.jsp</result>    <interceptor-ref name="tokenSession"></interceptor-ref>    <interceptor-ref name="defaultStack"></interceptor-ref></action><%@ taglib uri="/struts-tags" prefix="s"%>

<form>    <s:token></s:token></form>

***自定义拦截器(以下的案例在登录时并不使用,只作演示,正常时应用过滤器实现,对整个目录过滤)AuthInterceptor.java//登录拦截器-----------------------public class AuthInterceptor extends AbstractInterceptor{public  String intercept(ActionInvocation invocation) throws Exception{//Map session=invocation.getInvocationContext().getSession();        Map session = ActionContext.getContext().getSession();    //如果SESSION为空,则跳转到登录页面        if(session.get("login" == null){            retrun Action.LOGIN;        }else{            invocation.invoke();//如果有session,则表示用户已经登录,那么直接执行下一步        }

}}

struts.xml中配置-----------------------<package name="test" extends="struts-default">    <global-results>        <result name="login">/login.jsp</result>    </global-results>    <interceptors>        <interceptor name="authInterceptor" class="com.cyjch.interceptor.AuthInterceptor"/>        <!--自定义一个拦截器栈-->        <interceptor-stack name="authStack">            <interceptor-ref name="authInterceptor"></interceptor-ref>            <interceptor-ref name="defaultStack"></interceptor-ref>        </interceptor-stack>    </interceptors>    <action name="success" >        <result name="success" type="despatcher">/loginSuc.jsp</result>        <result name="input">/login.jsp</result>    </action>    <!--对登录后进入的主页进行拦截-->    <action name="suc" >        <interceptor-ref name="authStack"/><!--直接引用认证的栈-->        <result>/admin/main.jsp</result>    </action></package>-----------------------

***类型转换自动转换类型--------------String - StringString - ageString - DateString - double绝大多数情况下,数据的类型转换不需要关注

***上传下载需要再看

转载于:https://www.cnblogs.com/cyjch/archive/2012/02/06/2340132.html

struts2要点总结相关推荐

  1. java零碎要点---struts2中redirect和redirectAction的区别

    struts2中redirect和redirectAction的区别 struts2中关于result的返回类型一般我们是转发到一个jsp页面或者是html页面等,但是struts2中的result的 ...

  2. Struts2【开发Action】知识要点

    前言 前面Struts博文基本把Struts的配置信息讲解完了-..本博文主要讲解Struts对数据的处理 Action开发的三种方式 在第一次我们写开发步骤的时候,我们写的Action是继承着Act ...

  3. struts2教程--标签库详解

    struts2 标签库 tag-reference.html 就是 struts2标签规范 一. 通用标签库 的学习 <s:property> 解析ognl表达式,设置默认值,设置内容是否 ...

  4. struts2教程(10)

    struts2 标签库 tag-reference.html 就是 struts2标签规范 一. 通用标签库 的学习 <s:property> 解析ognl表达式,设置默认值,设置内容是否 ...

  5. Struts2拦截器之FileUploadInterceptor

    一.它能做什么? 借助于这个拦截器我们可以实现文件的上传和下载功能. 理论部分: struts2的文件上传下载功能也要依赖于Apache commons-fileupload和Apache commo ...

  6. struts2中用interceptor实现权限控制

    在jsp servlet中我们通常使用Servlet Filter控制用户是否登入, 是否有权限转到某个页面.在struts2中我们应该会想到他的拦截器(Interceptor), Intercept ...

  7. struts2数据库操作_Struts 2操作错误和操作消息

    struts2数据库操作 Struts 2 provide a lot of custom tags for development and we have already looked into D ...

  8. struts2登录注册示例_Struts 2文件上传示例

    struts2登录注册示例 Welcome to Struts 2 file upload example. File Upload is one of the common tasks of a w ...

  9. struts2路径配置_Struts 2结果路径配置示例

    struts2路径配置 This is the third post in the Struts 2 series. You might want to check out earlier posts ...

  10. struts2生成随机验证码图片

    之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...

最新文章

  1. 机器都会学习了,你的神经网络还跑不动?来看看这些建议
  2. [原创]基于frida的脱壳工具
  3. upgrade yum 指定版本_CentOS 6.9/7通过yum安装指定版本的MySQL
  4. linux为已有磁盘扩容 kvm,KVM虚拟磁盘扩容
  5. buildroot:Linux平台构建嵌入式Linux系统的框架
  6. 报错:error LNK2001:unresolved external symbol _WinMain@16
  7. Fantom已开始网络升级,大约需2个小时
  8. THREEJS - 动态标签(texture纹理方式)
  9. 论文阅读笔记(一)——DESCENDING THROUGH A CROWDED VALLEY—BENCHMARKING DEEP LEARNING OPTIMIZERS
  10. Dynamic 365 中创建编码规则
  11. 斐讯k2虚拟服务器设置,斐讯K2调配设置
  12. 【Keil 5】STM32F401CCU6 固件库配置(超详细教程)
  13. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\composite-element\)...
  14. 【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控
  15. Centos修改DNS
  16. 紫砂壶的起源 计算机操作题,紫砂壶的起源与历史发展
  17. CentOS 7 root 密码破解及 grub2 加密
  18. TypeScript免费视频图文教程(2W字)
  19. 20+ Twitter开源软件精选
  20. Irene Tong的空间

热门文章

  1. keras.preprocessing.image函数比较
  2. 【论文笔记】Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
  3. java求解给定一整数和指定指数求解计算结果
  4. JDK动态代理执行过程分析
  5. 会议服务器维护保养,关于视频会议系统的维护和保养方法
  6. 计算机语言疾病矫治操作流程,言语矫治专家决策系统的构建.doc
  7. 区块链 xuperchain io.grpc.StatusRuntimeException: UNKNOWN: error execution reverted: execution reverted
  8. layui upload 点击上传没有反应 JS动态加载
  9. Hyperledger Fabric 或 Composer的configtx.yaml配置文件解析
  10. ORACLE检查点测试,oracle深度解析检查点