struts2使用笔记

来源:互联网 发布:centos安装开发工具包 编辑:程序博客网 时间:2024/06/16 02:03
从一个action直接跳到另一个action中,struts提供了两种结果类型可以实现:chain,redirect.
两者的区别在于chain可以保存参数,而redirect则不可以。
http://blog.csdn.net/chenssy/article/details/7960599
写法:<package name="mystruts1" extends="struts-default" namespace="/mystruts1">
<!-- 第一个Action -->
<action name="test_*" class="com.action.TestAction" method="{1}">
<result name="text_chain" type="chain">result_resultChain</result>
<result name="text_redirect" type="redirect">result_resultRedirect</result>
</action>

<!-- 第二个Action -->
<action name="result_*"  class="com.action.ResultAction" method="{1}">
</action>
</package>




当然不是绝对的使用另一种方法可以实现redirect带参数
写法<result name=”success” type=”redirectAction”>
<param name=”actionName”>testAction</param>
<param name=”id”>${id}</param>//从这个action的值栈中取值
</result>






使用ognl表达式时


当使用ognl表达式时 我们可以使用iterator标签进行嵌套,可以将两层的iterator进行一起使用,在使用上一个iterator的中元素需要在本iterator中使用用#标记就可以获取上一个iterator中的元素。
在xml文件中使用ognl是使用${},而是在html文件中使用的是%{}。
值栈(value stack)是包括map栈与对象栈(list)。
Struts会将parameters request,session,application,attr压入map栈中
每创建一个action会创建一个值栈
Action返回时会将属性值放在对象栈中,使用%{#}无法取到对象栈的值,但是如果使用%{}是先找对象栈而后去找map栈(其原因是map栈中含有_root索引)




 在struts.xml文件中使用namesapce的情况是当加上/WEB-INF是从文件的根目录(WEBRoot下)去查找,不使用是从当前的namespace目录下去查找的。
在html下不使用/表示的是从当前的目录下去查找,使用时从当前的域名下去查找当前的文件。


三大框架整合配置404页面
在web.xml中配置
<error-page>
  <error-code>404</error-code>
  <location>/test/java.jpg</location>
 </error-page>
<error-page>
  <error-code>500</error-code>
  <location>/test/java.jpg</location>
 </error-page>
可以使访问不到的jsp页面转到java.jpg
在struts.xml中配置文件
<package name="serveAll" namespace="" extends="struts-default">


   <action name="*">

       <result>/test/java.jsp</result>

   </action>

</package>
原创粉丝点击