Struts2【配置】知识要点

前言

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

通配符

为何要学习通配符

在讲解通配符以前,咱们来看一下需求.....微信

如今个人Action中有两个方法,处理登录和处理注册:jsp

public class PrivilegeAction extends ActionSupport {
    
    public String login() {
        System.out.println("我是登录");

        return "success";
    }

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

所以,咱们就须要在struts.xml文件中配置两个action节点ide

<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>

复制代码

如今咱们发现:它们仅仅只有访问路径和方法的名称是不同的....可是这却要多配置一个action节点,会形成浪费!学习

因而乎通配符就应运而生了...编码


使用通配符改造

没有通配符以前,咱们是须要配置两个action的...有了通配符,咱们是这样作的:spa

<package name="privilige" extends="struts-default">

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

复制代码

解释一下:code

  • name=privilege_*,咱们用了_做为分隔符。*就是咱们的通配符
  • {1},就是表明着第一个通配符

咱们来看一下效果:cdn

这里写图片描述

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

这里写图片描述


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>
复制代码

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

这里写图片描述

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

这里写图片描述

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

这里写图片描述


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

  • 首先,Struts会把请求的资源路径拿到,也就是privilege_login
  • 而后在看看有没有/user/a/a这个名称空间;若是有,就返回结果
  • 若是没有,就看看有没有/user/a这个名称空间;若是有,就返回结果
  • 若是没有,再看看有没有/user这个名称空间;若是有,就返回结果
  • 直到最后,若是都没有找到,就报错!

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


Struts常量

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

这里写图片描述

修改默认访问路径

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

咱们在jar包上找到它的配置文件...

这里写图片描述

咱们发现它的默认值是**action,,**

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

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

这里写图片描述

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

<constant name="struts.action.extension" value="action"/>
复制代码

这里写图片描述

也就是说,“,”号可以匹配空格键

举例子说明:

  • 若是配置后缀为action。那么后缀必定要写action
  • 若是配置后缀为action,do,。那么后缀能够是action,能够是do,也能够不写
  • 若是配置后缀为action,,。那么后缀能够是action,能够不写

Struts经常使用的常量

这里写图片描述

<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属性,在地址栏使用资源名称!方法名的方式去调用业务方法

这里写图片描述


跳转全局视图

为何须要跳转全局视图?

首先,咱们仍是来看一个需求:如今我有两个Action,PrivilegeAction和CategoryAction

  • PrivilegeAction
public class PrivilegeAction extends ActionSupport {

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

        return "success";
    }

    public String register() {
        System.out.println("我是注册");
        return "success";
    }
}
复制代码
  • CategoryAction
public class CategoryAction extends ActionSupport {

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

        return "success";
    }

    public String find() {
        System.out.println("我是查找");
        return "success";
    }
}

复制代码
  • 在struts.xml文件中配置
<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>
复制代码

咱们发现一个问题,只要是方法返回值是success,那么就跳转到首页....若是有大量Action方法返回的都是success,那么就要写不少不少个result节点了..

使用跳转全局视图

  • 代码就能够变成是这样子了,注意:global-results节点须要在action节点的上面
<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>

复制代码
  • 效果:

这里写图片描述


Action节点中的默认配置

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

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

method

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

public class BBAtion extends ActionSupport {

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

        return SUCCESS;
    }
}

复制代码

这里写图片描述


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>
复制代码

若是文章有错的地方欢迎指正,你们互相交流。习惯在微信看技术文章,想要获取更多的Java资源的同窗,能够关注微信公众号:Java3y

相关文章
相关标签/搜索