Struts2-Action-4-系列问题(路径、调用Action的自定义方法、通配符)
2017-04-18 10:25
513 查看
4.路径设置
Struts中的路径问题是根据action的路径而不是通过jsp的路径来确定,所以尽量不要使用相对路径。虽然可以使用redirect方式解决,但redirect方式非必要。解决的办法非常简单,统一使用绝对路径(在jsp中使用request.getContextPath()方式拿到webapp的路径,或者在jsp的head标签中指定”/>)
说明:如果当前url路径是http://localhost:8080/hello/path/path.action
访问,那么url接下来会访问http://localhost:8080/hello/path/index.jsp(会造成一些问题,因为是相对路径比较麻烦)
访问,那么url接下来会访问http://localhost:8080/index.jsp(因为在jsp中“/”代表站点的跟路径,而不是应用的根路径)
5.调用Action的自定义方法
Action执行的时候并不一定要执行execute方法,可以在配置文件中配置method=来指定执行哪个方法,也可以在URL中动态指定(DMI推荐),前者会产生大量的action,不推荐
注意:使用Struts2.5版本DMI时,因为增加了安全性相关内容,要在struts.xml中添加
6.通配符(wildcard)
使用通配符,将配置量降到最低(需要约定好JSP和ACTION等的命名规则)
不过,一定要遵守“约定优于配置”的原则
通配符的方式比DMI的方式还要优,因为DMI的方式还是要定义多个result对应的jsp名字,而通配符的方式可以用于为jsp命名
如果某个访问action,能和多个name匹配,那么首先匹配最精确的那个(不带通配符的),再匹配次精确的(只要有的均算同一等级,不管几个 ,谁在前面调哪个)
Struts中的路径问题是根据action的路径而不是通过jsp的路径来确定,所以尽量不要使用相对路径。虽然可以使用redirect方式解决,但redirect方式非必要。解决的办法非常简单,统一使用绝对路径(在jsp中使用request.getContextPath()方式拿到webapp的路径,或者在jsp的head标签中指定”/>)
说明:如果当前url路径是http://localhost:8080/hello/path/path.action
访问,那么url接下来会访问http://localhost:8080/hello/path/index.jsp(会造成一些问题,因为是相对路径比较麻烦)
访问,那么url接下来会访问http://localhost:8080/index.jsp(因为在jsp中“/”代表站点的跟路径,而不是应用的根路径)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String path = request.getContextPath();// hello String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";// http://localhost:8080/hello/ %> <html> <head> <base href="<%=basePath%>"/> </head> <body> <%--index.jsp的绝对路径是: http://localhost:8080/hello/index.jsp--%> <%--下面的三种方法可以访问到 index.jsp--%> <%--<a href="<%=basePath%>index.jsp">index.jsp</a><br>--%> <%--<a href="index.jsp">使用默认basePath的index.jsp(当在head标签中配置了base标签后)</a><br>--%> <a href="<%=path%>/index.jsp">request.getContextPath()的index.jsp</a><br> <%=path%><br> <%=basePath%><br> <h2>pathtest.jsp</h2> </body> </html>
5.调用Action的自定义方法
Action执行的时候并不一定要执行execute方法,可以在配置文件中配置method=来指定执行哪个方法,也可以在URL中动态指定(DMI推荐),前者会产生大量的action,不推荐
<!--调用Action的自定义方法--> <!--调用Action的自定义方法--> <package name="user" namespace="/user" extends="struts-default" > <global-allowed-methods>regex:.*</global-allowed-methods> <!--第一种:可以忘记 因为有多少方法 就要有多少Action--> <!-- http://localhost:8080/hello/user/userAdd --> <action name="userAdd" class="com.UserAction" method="add"> <result>/user_add_success.jsp</result> </action> <!--第二种:相对于第一种要好 推荐 DMI方式(dynamic method invoke)--> <!-- http://localhost:8080/hello/user/user!add --> <action name="user" class="com.UserAction" > <result>/user_add_success.jsp</result> </action> <!-- 第三种:通配符--> </package>
注意:使用Struts2.5版本DMI时,因为增加了安全性相关内容,要在struts.xml中添加
<constant name="struts.enable.DynamicMethodInvocation" value="true"/> //在相应package中添加 <global-allowed-methods>regex:.*</global-allowed-methods>
6.通配符(wildcard)
使用通配符,将配置量降到最低(需要约定好JSP和ACTION等的命名规则)
不过,一定要遵守“约定优于配置”的原则
通配符的方式比DMI的方式还要优,因为DMI的方式还是要定义多个result对应的jsp名字,而通配符的方式可以用于为jsp命名
如果某个访问action,能和多个name匹配,那么首先匹配最精确的那个(不带通配符的),再匹配次精确的(只要有的均算同一等级,不管几个 ,谁在前面调哪个)
<!-- 通配符 --> <package name="actions" extends="struts-default" namespace="/actions"> <action name="Student_*" class="com.StudentAction" method="{1}"> <result>/Student_{1}_success.jsp</result> <allowed-methods>add</allowed-methods> </action> <!--注意,如果是Struts2.5的话,要在package中添加 <allowed-methods>add</allowed-methods>--> <action name="*_*" class="com.{1}Action" method="{2}"> <result>/{1}_{2}_success.jsp</result> <allowed-methods>add</allowed-methods> </action> </package>
相关文章推荐
- Struts2自定义Action,自定义方法,DMI(动态方法调用),路径问题
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射 (
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射
- Struts2中的path路径问题、动态方法调用、通配符
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射
- Struts2一个Action内包含多个请求处理方法的处理,method的使用方法,struts2中的路径问题,通配符映射
- struts2动态方法调用和使用通配符定义action
- Struts2使用通配符完成方法和action的调用
- struts2开发action 的三种方法以及通配符、路径匹配原则、常量
- Struts2中DMI(动态方法调用)的错误问题(There is no Action mapped for namespace [/xxx] and action name [xxx!yyy] a)
- Struts2 Action中动态方法调用、通配符的使用
- Struts2 Action中动态方法调用、通配符的使用
- Struts2_动态方法调用和使用通配符定义action
- struts2中action的路径问题以及在MyEclipse8.5中的说明(通配符等等)
- struts2-10动态方法调用和使用通配符定义action
- Struts2动态方法调用和使用通配符定义action
- struts2:动态方法调用与通配符定义action
- Struts2基础之四:动态方法调用(DMI)和action通配符
- struts2的配置之could not find action or result 问题的解决方法(struts.xml文件路径)