struts2基本配置详解2
接上篇struts2基本配置详解,还有一些配置没有讲到,下面将继续。
struts.xml
<package name="com.amos.web.action" namespace="/" extends="struts-default">
<action name="HelloWorldAction" method="execute">
<result name="success" type="dispatcher">
suc.jsp
</result>
</action>
</package>
1)、<action>中不指定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)、<result> 中不指定name属性会是什么结果?
重新发布项目,依然是成功,由此可知,name默认为"success"
3)、<result>中不指定type属性会是什么结果?
重新发布项目,依然是成功,由此可知,type默认为"dispatcher",即默认为转发
如果要使用重定向,必须将type赋值为redirect,即type="redirect",如下所示
<result name="success" type="redirect">
suc.jsp
</result>
浏览器中输入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中,如下所示:
<struts>
<constant name="struts.action.extension" value="hello,abc"></constant>
<package name="com.amos.web.action" namespace="/" extends="struts-default">
<action name="HelloWorldAction" >
<result name="success" type="dispatcher">
suc.jsp
</result>
</action>
</package>
</struts>
将struts.properties删除,重新部署并访问浏览器:

推荐第二种方式。
注:当struts.properties和struts.xml同时存在时,以struts.properties为主。
5)、如何配置多个struts.xml文件?
项目由多人开发,每个人可能都会有一个自己的struts.xml,如何避免冲突并使他人的struts.xml文件有效?
如下图所示:

新建目录config,新建dao_struts.xml和email_struts.xml两个配置文件,如何将它们加载到内存中?并解析为javabean对象
<!-- 加载其他配置文件 -->
<include file="config/dao_struts.xml"></include>
<include file="config/email_struts.xml"></include>
使用include指定文件路径即可,注意查看tomcat的启动提示信息,如果路径错误,那么将提示Unable to locate ...

6)、action使用单例还是非单例模式,需不需要解决线程安全问题?
重写HelloWorldAction的构造函数,如下:
public HelloWorldAction(){
System.out.println("hello actions:"+this.hashCode());
}
浏览器中多次访问,然后查看控制台输出,结果如下:

注:一次请求对应一次Action实例,因此不会产生线程安全问题,即在Action中不会产生synchronized同步代码块。
本文源代码:点此查看 struts2配置详解
struts2基本配置详解2的更多相关文章
- Struts2 XML配置详解
struts官网下载地址:http://struts.apache.org/ 1. 深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1. 包配置: S ...
- Struts2基本配置详解
Struts2配置文件加载顺序 struts2 配置文件由核心控制器加载 StrutsPrepareAndExecuteFilter (预处理,执行过滤) init_DefaultProperties ...
- Struts2(三)配置详解
一.概述 Struts2提供了多种可选的配置文件形式. 其中,struts-default.xml和default.properties是框架级别的配置文件,这两个文件在Struts的核心JAR包中, ...
- Struts2学习笔记二 配置详解
Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...
- java web.xml配置详解(转)
源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...
- JAVAEE——spring01:介绍、搭建、概念、配置详解、属性注入和应用到项目
一.spring介绍 1.三层架构中spring位置 2.spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还 ...
- Spark log4j日志配置详解(转载)
一.spark job日志介绍 spark中提供了log4j的方式记录日志.可以在$SPARK_HOME/conf/下,将 log4j.properties.template 文件copy为 l ...
- log4j.properties配置详解与实例
log4j.properties配置详解与实例 第一步:加入log4j-1.x.x.jar到lib下. 第二步:在工程的src下下建立log4j.properties.内容如下: #OFF,syste ...
- Spring 入门 web.xml配置详解
Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...
随机推荐
- 如何去除图片上的文字(PS使用教程)
很多时候由于工作的需要,需要对我们的图片进行修改,修改的同时还想要保存我们的图片背景,所以很多人就不知道怎么弄了,小编跟大家分享一下使用PS如何简单的去掉图片上的文字,希望对大家有所帮助! 方法/步骤 ...
- 【python3】 enumerate用法总结(转)
http://blog.csdn.net/churximi/article/details/51648388 enumerate()说明 enumerate()是python的内置函数 enumera ...
- WordPress 获取指定分类ID的分类信息
get_term:直接从数据库中获取分类信息get_the_category:使用post_id作为参数,先根据post_id查询对应的文章然后再返回对应的分类信息,如果没有文章信息则返回Null 之 ...
- Extend一个web application没有反应怎么办?
通过SharePoint管理中心Extend一个web application的时候, 点完确定按钮后,没有反应,怎么回事? [解决方法] 多等一会,不要连续点. 等待的过程中看看iis, 过一会 ...
- vs 2017 正规表达式替换整行多行数据
((<OutputFile>..*</OutputFile>)[\S\s])[\S\s] 从 <OutputFile> 开始 到 </OutputFile&g ...
- 页面显示This is the initial start page for the WebDriver server.的解决办法
今天在做项目的时候,遇到一个奇怪的问题,打开浏览器是正常的,但是页面不会跳转到需要的URL,而是提示一行白字,如图: 反复研究了脚本,没有问题啊,但是就是不跳转. 后来查了下,在某论坛上找到了答案: ...
- firefox chrome ie9,10,11 不支持selectSingleNode和selectNodes的解决方法
firefox并不支持selectSingleNode和selectNodes的解决方法 function test(){ var perid = document.thisForm.PerID.va ...
- Eclipse远程连接HBase
在Eclipse下新建一个Map/Reduce项目,并将以下jar添加到Build path: 程序代码: package thathbase; import java.io.IOException; ...
- How do I fix a “Unknown configuration key `foreign-architecture' found in your `dpkg' configuration files.” error?
My /etc/dpkg/dpkg.cfg.d/multiarch contained: foreign-architecture i386 I deleted the file. I then is ...
- jquery 如何获取有多个class名的元素
1.情景展示 如何使用jquery获取带有多个class样式的元素? 2.解决方案 $("p.opinion.mb15.gray2e.max2line.mr20:contains('大摩 ...