Struts2【配置】

 
 

前言

上篇Struts博文已经讲解了Struts的开发步骤以及执行流程了…..对Struts的配置文件有了了解…..本博文继续讲解Struts在配置的时候一些值得要学习的细节

通配符

为什么要学习通配符

在讲解通配符之前,我们来看一下需求…..

现在我的Action中有两个方法,处理登陆和处理注册:

public class PrivilegeAction extends ActionSupport {    public String login() {        System.out.println("我是登陆");        return "success";    }    public String register() {        System.out.println("我是注册");        return "success";    }}class PrivilegeAction extends ActionSupport {

   public String login() {
       System.out.println("我是登陆");

       return "success";
   }

   public String register() {
       System.out.println("我是注册");
       return "success";
   }
}

因此,我们就需要在struts.xml文件中配置两个action节点

    <action name="login" class="privilegeaction.PrivilegeAction" method="login">        <result name="success">/index.jsp</result>    </action>    <action name="register" class="privilegeaction.PrivilegeAction" method="register">        <result name="success">/index.jsp</result>    </action>
       <result name="success">/index.jsp</result>
   </action>

   <action name="register" class="privilegeaction.PrivilegeAction" method="register">
       <result name="success">/index.jsp</result>
   </action>

现在我们发现:它们仅仅只有访问路径和方法的名称是不一样的….但是这却要多配置一个action节点,会造成浪费!

于是乎通配符就应运而生了…


使用通配符改造

没有通配符之前,我们是需要配置两个action的…有了通配符,我们是这样做的:

<package name="privilige" extends="struts-default">    <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">        <result name="success">/index.jsp</result>    </action></package>

   <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">
       <result name="success">/index.jsp</result>
   </action>

</package>

解释一下:

我们来看一下效果:

640?wx_fmt=png
这里写图片描述

同理可得,当我们在地址栏访问login的时候,就会执行login的方法

640?wx_fmt=png
这里写图片描述

Struts中路径的匹配原则

有的时候,我们可能会在package节点中指定namespace名称空间,我们在访问对应的资源名称的时候,就需要在前面加入相对应名称空间的值

比如:

名称空间的值为“/user”

<package name="privilige" extends="struts-default" namespace="/user">    <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">        <result name="success">/index.jsp</result>    </action></package>
   <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">
       <result name="success">/index.jsp</result>
   </action>

</package>

那么在访问资源的时候,就需要在项目名称后边加上名称空间的值:

640?wx_fmt=png
这里写图片描述

现在就有一个很奇怪妙的事情发生了:在名称空间和资源路径的中间可添加任意的路径

640?wx_fmt=png
这里写图片描述

但是呢,不能在名称空间之前加入不存在的路径

640?wx_fmt=png
这里写图片描述

其实这就涉及到了Struts中路径的匹配原则,我就拿随便拿个路径来举例子http://localhost:8080/user/a/a/privilege_login

看完这个例子,我们就可以知道为啥在名称空间和资源路径的中间可添加任意的路径,而不能在名称空间之前加入不存在的路径….这就是Struts的路径匹配原则..


Struts常量

Struts2默认的访问后缀是.action……

640?wx_fmt=png
这里写图片描述

修改默认访问路径

有的时候,可能根据项目的需要…我想后缀名默认并不是.action,那我该怎么办呢??

我们在jar包上找到它的配置文件…

640?wx_fmt=png
这里写图片描述

我们发现它的默认值是action,,

值得注意的是:两个逗号并不是多余的

我们不可能是直接修改jar包中的配置文件的,在struts.xml文件中提供了constant节点供我们修改struts的常量

640?wx_fmt=png
这里写图片描述

前面已经说了,两个逗号并不是多余的。那么直接在配置文件中配置action时,会怎么样:

<constant name="struts.action.extension" value="action"/>
640?wx_fmt=png
这里写图片描述

也就是说,“,”号能够匹配空格键

举例子说明:


Struts常用的常量

640?wx_fmt=png
这里写图片描述
<struts>    <!-- 0. 请求数据编码 -->     <constant name="struts.i18n.encoding" value="UTF-8"/>    <!-- 1. 修改Struts默认的访问后缀 -->    <constant name="struts.action.extension" value="action,do,"></constant>    <!-- 2. 修改xml自动重新加载 -->    <constant name="struts.configuration.xml.reload" value="true"/>    <!-- 3. 开启动态方法调用 (默认不开启)-->    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>    <!-- 4. 修改上传文件的最大大小为30M -->    <constant name="struts.multipart.maxSize" value="31457280"/></struts>

   <!-- 0. 请求数据编码 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
   <!-- 1. 修改Struts默认的访问后缀 -->
   <constant name="struts.action.extension" value="action,do,"></constant>
   <!-- 2. 修改xml自动重新加载 -->
   <constant name="struts.configuration.xml.reload" value="true"/>
   <!-- 3. 开启动态方法调用 (默认不开启)-->
   <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
   <!-- 4. 修改上传文件的最大大小为30M -->
   <constant name="struts.multipart.maxSize" value="31457280"/>


</struts>

只有动态方法调用是我们没有接触过的。
那动态方法调用是怎么回事呢???这是样的:在action节点不配置method属性,在地址栏使用资源名称!方法名的方式去调用业务方法

640?wx_fmt=png
这里写图片描述

跳转全局视图

为什么需要跳转全局视图?

首先,我们还是来看一个需求:现在我有两个Action,PrivilegeAction和CategoryAction

public class PrivilegeAction extends ActionSupport {    public String login() {        System.out.println("我是登陆");        return "success";    }    public String register() {        System.out.println("我是注册");        return "success";    }}class PrivilegeAction extends ActionSupport {

   public String login() {
       System.out.println("我是登陆");

       return "success";
   }

   public String register() {
       System.out.println("我是注册");
       return "success";
   }
}
public class CategoryAction extends ActionSupport {    public String add() {        System.out.println("我是添加");        return "success";    }    public String find() {        System.out.println("我是查找");        return "success";    }}class CategoryAction extends ActionSupport {

   public String add() {
       System.out.println("我是添加");

       return "success";
   }

   public String find() {
       System.out.println("我是查找");
       return "success";
   }
}
    <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">        <result name="success">/index.jsp</result>    </action>    <action name="category_*" class="privilegeaction.CategoryAction" method="{1}">        <result name="success">/index.jsp</result>    </action>
       <result name="success">/index.jsp</result>
   </action>

   <action name="category_*" class="privilegeaction.CategoryAction" method="{1}">
       <result name="success">/index.jsp</result>
   </action>

我们发现一个问题,只要是方法返回值是success,那么就跳转到首页….如果有大量Action方法返回的都是success,那么就要写很多很多个result节点了..

使用跳转全局视图

<package name="privilige" extends="struts-default" >    <global-results>        <result name="success">/index.jsp</result>    </global-results>    <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">    </action>    <action name="category_*" class="privilegeaction.CategoryAction" method="{1}">    </action></package>

   <global-results>
       <result name="success">/index.jsp</result>
   </global-results>

   <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">
   </action>

   <action name="category_*" class="privilegeaction.CategoryAction" method="{1}">
   </action>


</package>
640?wx_fmt=png
这里写图片描述

Action节点中的默认配置

一份相对完整的action节点是这样子的:

        <action name="privilege_*" class="privilegeaction.PrivilegeAction" method="{1}">        </action>
       </action>

method

如果我们不写method的话,默认执行的是execute()方法,execute()方法默认返回值是SUCCESS

public class BBAtion extends ActionSupport {    @Override    public String execute() throws Exception {        System.out.println("我是execute()");        return SUCCESS;    }}class BBAtion extends ActionSupport {

   @Override
   public String execute() throws Exception {
       System.out.println("我是execute()");

       return SUCCESS;
   }
}
640?wx_fmt=png
这里写图片描述

class

如果不写class,class默认执行的action在struts-default有配置<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />

什么时候会不写class?就是需要跳转到WEB-INF目录下的资源的时候…..这就类似与Servlet需要跳转到WEB-INF下的资源。【Tomcat不允许直接访问WEB-INF资源】

        <!-- 什么情况不配置class? 即处理的aciton -->        <!-- 答案: 当只是需要跳转到WEB-INF下资源的时候。 -->         <action name="test2">            <result name="success" >/WEB-INF/index.jsp</result>         </action>
       <!-- 答案: 当只是需要跳转到WEB-INF下资源的时候。 -->
        <action name="test2">
           <result name="success" >/WEB-INF/index.jsp</result>
        </action>


如果文章有错的地方欢迎指正,大家互相交流。习惯在微信看技术文章,想要获取更多的Java资源的同学,可以关注微信公众号:Java3y

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值