首页 > 学院 > 开发设计 > 正文

Struts2动态调用方法的常见方式

2019-11-06 07:13:08
字体:
来源:转载
供稿:网友

使用Struts2关键的是struts.xml和web.xml文件的配置,这里我主要讲解struts.xml文件;调用方法的方法主要有三种:

第一种方式:在struts.文件下的package包下面配置action,里面有几个方法就调用几次例如:

<package name="default" extends="struts-default" namespace="/"><action name="hello"  class="com.imooc.action.HelloAction"><result name="success" >/result.jsp</result></action><action name="add"  class="com.imooc.action.HelloAction"><result name="add" >/add.jsp</result></action>

</package>

访问add.JSP页面方式:localhost:8080/HelloWorld/add.action.

这种方式在方法多的情况下不推荐使用

第二种方式:调用感叹号的方式,此时需要我们在struts.xml下的增加<constant></constant>例:

struts><package name="default" extends="struts-default" namespace="/"><action name="hello_*" method="{1}" class="com.imooc.action.HelloAction"><result name="success" >/result.jsp</result><result name="add">/add.jsp</result><result name="index">/indexx.jsp</result></action></package><constant name="struts.enable.DynamicMethodInvocation" value="false"></constant></struts>   

访问add.jsp页面方式:localhost:8080/Hello/hello!add.action

第三种方式:也就是最常用的,官方推荐使用:通配符方式:

<package name="default" extends="struts-default" namespace="/"><action name="hello_*" method="{1}" class="com.imooc.action.HelloAction"><result name="success" >/result.jsp</result><result name="add">/{1}.jsp</result><result name="index">/{1}.jsp</result></action></package>

我们需要给action的name添加_和*  然后添加一个Method”{1}“,{1}代表_后面的*参数,这种方法更简便:

同样访问index.jsp页面的方式:localhost:8080/Hello_add.action


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表