Struts

来源:互联网 发布:斗米兼职 知乎 编辑:程序博客网 时间:2024/06/06 07:41

    网上商城中接触了很多新的内容,Struts2的配置信息,在struts.xml文件中,查了相关资料,简单了解了struts.xml中代码的作用;

    Struts2的执行过程: 请求>过滤器>拦截器>Action>result > 回调 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.devMode" value="false" /><package name="Shop" extends="struts-default" namespace="/"><!-- 很多的页面都要往这里跳转;全局 ;带回来一些信息; --><global-results><result name="msg">/WEB-INF/jsp/msg.jsp</result></global-results><!-- 配置首页访问的Action --><action name="index" class="indexAction"><result name="index">/WEB-INF/jsp/index.jsp</result></action><!-- 配置用户模块的Action _*:action处理所有以user_开头的请求 method="{1}"调用action中的以methodname命名的方法 在jsp中”/”表示tomcat 服务器的根目录,在struts.xml配置文件中”/”表示webapp的根路径,即MyEclipse web项目中的WebRoot路径。 --><action name="user_*" class="userAction" method="{1}"><result name="registPage">/WEB-INF/jsp/regist.jsp</result><result name="input">/WEB-INF/jsp/regist.jsp</result><result name="loginPage">/WEB-INF/jsp/login.jsp</result><result name="login">/WEB-INF/jsp/login.jsp</result><result name="loginSuccess" type="redirectAction">index</result><!-- 直接定向,能把Session中的数据带过来。用过的信息存入到Session,定向 --><result name="quit" type="redirectAction">index</result> <!-- 销毁Session中的内容,然后重定为到页面; --><result name="checkcodeFail">/WEB-INF/jsp/regist.jsp</result></action><!-- 验证码的Action --><action name="checkImg" class="checkImgAction"></action><!-- 商品模块的Action --><action name="product_*" class="productAction" method="{1}"><result name="findByPid">/WEB-INF/jsp/product.jsp</result><result name="findByCid">/WEB-INF/jsp/productList.jsp</result><result name="findByCsid">/WEB-INF/jsp/productList.jsp</result></action><!-- 购物车的Action --><action name="cart_*" class="cartAction" method="{1}"><result name="addCart">/WEB-INF/jsp/cart.jsp</result></action></package></struts>

分为三个节点部分:

Package , Action,result


pakage:

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

1.name:包名,这里指项目的名称;

2.extends:继承,指定继承的包,将继承包下的配置信息复制到当前包下;

3.namespace:命名空间,用于规定Action的访问路径,“/”开头;


action:

业务控制器,用于注册业务控制器组件;

<action name="user_*" class="userAction" method="{1}">

1.name:用于规定Action的访问路径 。配置用户模块的Action _*:action处理所有以user_开头的请求

2.class:业务控制器组件,指定控制器对应的类;

3.method:指定访问当前action要调用的方法;method="{1}"调用action中的以method name命名的方法 。


result:

输出组件,转发,重定向,直接输出

<result name="registPage">/WEB-INF/jsp/regist.jsp</result>

    1.name:action 中,method指定方法的返回字符串,方法返回到该字符串;
    2.默认做转发,标记内容为转发的页面;
        在jsp中”/”表示tomcat 服务器的根目录,在struts.xml配置文件中”/”表示webapp的根路径,即MyEclipse web项目中的WebRoot路径。


<result name="loginSuccess" type="redirectAction">index</result><!-- 直接定向,能把Session中的数据带过来。用过的信息存入到Session,定向 -->

    type="redirectAction"  标记内容配置action的名称,不带后缀.action ; 如果带参数则在action 后面加上需要的内容。例如index ,可修改为index?id=XXX;

总结:

    实践之后,认真的研究一下理论,就会豁然开朗很多,每一行代码都有他的意义,不同的时期,不同的阶段对自己要有不同的要求。

1 0