struts2基本配置详解2

2014-11-24 07:11:38 · 作者: · 浏览: 0
struts.xml
suc.jsp
1)、中不指定method属性会是什么结果?
将exeucte方法改名为execute2(),并在struts.xml中删除method属性,然后重新发布项目。
HelloWorldAction.java
public class HelloWorldAction extends ActionSupport{
  public String execute2() throws Exception {
    System.out.println("欢迎使用struts2!"); return "success";
  }
}
结果依然是正确的。为什么不写method的属性也会正常输出??
查看一下ActionSupport,然后发现execute方法会有默认的返回值为,如下,截取文档内容:
A default implementation that does nothing an returns "success".
Subclasses should override this method to provide their business logic.
由此可知,method默认为excute(),返回值为"success"
2)、 中不指定name属性会是什么结果?
重新发布项目,依然是成功,由此可知,name默认为"success"
3)、中不指定type属性会是什么结果?
重新发布项目,依然是成功,由此可知,type默认为"dispatcher",即默认为转发
如果要使用重定向,必须将type赋值为redirect,即type="redirect",如下所示
suc.jsp
浏览器中输入http://localhost:8080/struts2/HelloWorldAction
发现已经重定向到suc.jsp了。
4)、两种方式访问Action
>>方法1,使用.action扩展名 例,http://localhost:8080/struts2/HelloWorldAction.action
>>方法2,不使用扩展名 例,http://localhost:8080/struts2/HelloWorldAction
两种方式访问效果一样。从 源码中查看原因?
在struts2-core/2.3.16/struts2-core-2.3.16-sources.jar/org/apache/struts2/default.properties下第79行有如下设置:
struts.action.extension=action,,
action或者为空
如何更改默认扩展名?
>>方法1,在src/main/resource目录下,即与struts.xml同级目录下,新建struts.properties,在其中写入想要的扩展名即可
struts.action.extension=action,hi,amos,,
这里新添加了.hi和.amos扩展名。
效果如下:
>>方法2,将值配置到struts.xml中,如下所示:
suc.jsp
将struts.properties删除,重新部署并访问 浏览器
推荐第二种方式。
注:当struts.properties和struts.xml同时存在时,以struts.properties为主。