美文网首页
Struts2理解--动态方法和method属性及通配符_默认A

Struts2理解--动态方法和method属性及通配符_默认A

作者: Zak1 | 来源:发表于2018-12-19 22:39 被阅读0次

Struts2 理解 -- 动态方法和 method 属性及通配符_默认 Action

众所周知,默认条件下,在浏览器输入 indexAction!execute.action,便会执行 indexAction 类里的 execute 方法,这样虽然方便,但可能带来安全隐患,通过 url 可以执行 Action 中的任意方法。

   想要禁止调用动态方法,则要在 struts.xml 中通过 constant 元素将属性 strutsenableDynamicMethodInvocation 设置为 false,来禁止调用动态方法。

            <constant name="strutsenableDynamicMethodInvocation" value="false"/>

method 属性:

这时我们需要通过其他安全的方式来实现动态方法的调用

一、通过 action 元素的 method 属性来指定 Action 执行时调用的方法

<action name="empAction_register" class="com.syaccp.erp.action.emp.EmpAction" method="register">

/WEB-INF/jsp/basic/emp_list.jsp</result>

/WEB-INF/jsp/basic/emp_edit.jsp</result>

/WEB-INF/jsp/basic/emp_add.jsp</result>

empAction.action</result>

</action>

以上一个配置片段便用到了 method 属性,当请求 / empAction_register.action 时,Struts2 框架会在 EmpAction 中查找 register 方法并执行。

Struts2 根据 method 属性查找方法有两种途径:

      1、查找与 method 属性值完全一致的方法

      2、查找 doMethod 形式的方法

使用动态方法调用和 method 属性的区别:

````

   <action name="empAction" class="com.syaccp.erp.action.emp.EmpAction">

/WEB-INF/jsp/basic/emp_list.jsp</result>

             empAction.action</result>

   </action>

<action name="empAction_register" class="com.syaccp.erp.action.emp.EmpAction" method="register">

/WEB-INF/jsp/basic/emp_register.jsp</result>

  </action>     

<action name="empAction_login" class="com.syaccp.erp.action.emp.EmpAction" method="login">

/WEB-INF/jsp/basic/emp_login.jsp</result>

    </action>

````

 通过以上三个配置来叙说,这三个配置实质是操作同一个 Action。

第一个动态方法模式,通过请求 / empAction!register.action 或者 /empAction!login.action、或者 /empAction.action。如果

register 方法、login 方法、execute 方法返回值都是是 "success",则都会进入 emp_list.jsp 页面。

    第二三个通过指定 method 属性,动态访问。这里 register 和 login 方法假设返回都是 "success",但它们不会进入同一个页面,而是分别进入各自 result 指定的页面中。

由上我们分析出:

  如果同一个 Action 的不同方法的响应使用相同的的配置 (result 等),则使用动态方法调用

  如果同一个 Action 的不同方法的响应分别使用不同的配置,则使用 action 元素的 method 属性,为同一个 Action 配置多个名称。

通配符:

在使用 method 属性来实现同一个 Action 的不同方法处理不同的请求时,会发现,随着方法的增多,从而导致大量的 Action 配置,这时我们就需要通过使用通配符来解决 Action 配置过多的方法。

在配置 <action.../> 元素时,需要指定 name、class、method 属性。其中 name 属性可支持通配符,然后可以在 class、method 属性中使用表达式。通配符用星号 * 表示。

<action name="empAction_*" class="com.syaccp.erp.action.emp.EmpAction" method="{1}">

/WEB-INF/jsp/basic/emp_{1}.jsp</result>

    </action> 

以上配置表明,当请求 / empAction_login 时,通配符匹配的是 login,这个值将替换 {1},最终执行 EmpAction 的 login 方法,如果方法返回值为 success,跳转到 emp_login.jsp 页面。

默认 Action:

在浏览器输入一个不存在的 Action,页面将呈现 404 错误,为了网站更友好,我们可以设置一个默认的 Action。

设置默认 Action 有两种形式:

1、配置每个包的默认 Action,如果在相应的 namespace 下没有一个 Action 匹配请求,那么将执行该 namespace 默认的 Action,不同的包,可配置不同的默认 Action,配置如下:defaultAction 为默认 Action 的 name 属性值,default 语句必须写在首行。

<result>/error.jsp</result>

</action>

</package>

   2、在根目录下配置默认 Action,不用填写 namespace 属性

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

<result>/error.jsp</result>

</action>

</package> 

如果声明了第一种,Struts2 将会调用当前包下声明的默认 Action。忽视全局的默认 Action。

一般用第二种,统一默认的 Action,不论在 url 中输入哪个目录或包下没有的 Action,都显示错误页面。      

相关文章

网友评论

      本文标题:Struts2理解--动态方法和method属性及通配符_默认A

      本文链接:https://www.haomeiwen.com/subject/kgnskqtx.html