Struts的action和Result总结

编程技术  /  houtizong 发布于 3年前   86

 

 

一:Action的配置详解:

     下面是一个Struts中一个空的Struts.xml的配置文件

   

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <package name="default" namespace="/" extends="struts-default">        <default-action-ref name="index" />        <global-results>            <result name="error">/WEB-INF/jsp/error.jsp</result>        </global-results>        <global-exception-mappings>            <exception-mapping exception="java.lang.Exception" result="error"/>        </global-exception-mappings>        <action name="index">            <result type="redirectAction">                <param name="actionName">HelloWorld</param>                <param name="namespace">/example</param>            </result>        </action>    </package>    <include file="example.xml"/>    <!-- Add packages here --></struts>

 

 

   

    1,   包配置  

      <package name="default" namespace="/" extends="struts-default">  </package>

 

  2,  name  表示包的名字 

 

  3,  namespace="/"  ;表示访问该包下面的路劲时必须要在路径前加 /xx

                 也可以使用namespace="/space"  访问时加上/space/xx

 <package name="default"  extends="struts-default">        <action name="hello" >            <result >/login.html</result>        </action>    </package>

 

  当namespace不存在时如何访问;

     xxx/aaa/bbb/hello 

     aa/hello    都可以访问到

 

  4,extends="struts-default"继承默认的标签

 

 

  5,Action中的调用;

   

   a; 普通调用

 <action name="hello" >            <result >/login.html</result>        </action>

 

 b,动态调用;

  2,3可能会包错: 

There is no Action mapped for namespace [/user] and action name [user!add] associated with context p

解决办法;

    将DMI打开 <constant name="struts.enable.DynamicMethodInvocation" value="true" />

 

 

6,include模块

  Struts.xml可以相互引入

    <include file="login.xml" />  login,xml的配置与Struts.xml相同

 

7,Struts中Session会话的使用例子;

   保存会话

@Overridepublic String execute() throws Exception {System.out.println(">"+username+"<>"+userpwd+"<");if ("scott".equals(username) && "tiger".equals(userpwd)) {   HttpServletRequest request=(HttpServletRequest) ServletActionContext.getRequest();   HttpSession session=request.getSession();   session.setAttribute("name", username);return "hello";} else {return "hellow";}}

 

获取会话

<% 

  String name=(String)session.getAttribute("name");

%>

 欢迎<a href="login.html"><%=name %>登陆</a>

 

 

 

9,Result的使用

   <result type="redirectAction">   重新请求action

   Chain 处理action连接

   Dispatcher 处理jsp页面

    Redirect重定向

 

 

 

 

 

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客