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 ...
随机推荐
- java 使用AXIS调用远程的web service
1.服务 2.代码 import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.ax ...
- 操作系统重点双语阅读 - 上下文切换 Context Switch
The context is represented in the PCB of the process. It includes the value of the CPU registers, th ...
- svn commit --cl app 时手动输入提交的注释,而不是在 -m 'comments here'这里输入
原来只需要,提交的时候不指定 -m ,也不指定 -F就可以了,提交之前,svn会自动弹出编辑框来,可以修改信息. https://stackoverflow.com/questions/1746891 ...
- 转:Raft一致性选举算法的ppt与视频
http://xiaorui.cc/2016/07/08/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB-%E3%80%8A%E5%88%86%E5%B8%83%E5%BC% ...
- Ubuntu mysql开启远程登录的方法
一.问题 Ubuntu 16.0.4 mysql5.7 二.解决问题 Ubuntu中MySQL的配置文件是在/etc/mysql/mysql.conf.d/mysqld.cnf,VI该文件把 b ...
- 使用Robot Framework做接口测试
http://chuansong.me/n/1858477 1.RF框架 1.1 RF框架介绍Robot Framework 框架是一个通用的测试框架,一直是由诺西网络(Nokia Siemens N ...
- docker toolbox在win7下的安装
1.下载安装docker toolbox docker toolbox的下载地址: http://mirrors.aliyun.com/docker-toolbox/windows/docker-to ...
- Javascript中怎样获取统一管理的Java提示语
项目开发中,各个页面.各个业务操作都会使用提示语.面对这么多message,更好的方式是统一管理这些消息. 这样在做国际化的时候进行统一处理也变的方便. 推荐方案使用数据库来管理全部提示语,在项目启动 ...
- 微信小程序尝鲜一个月现状分析
概述 曾记得在微信小程序还没有上线的时候,大家都是翘首以待.希望在张小龙,在企鹅的带领下,走出差别于原生开发的还有一条移动开发的道路,我也是一直关注着.知道1月9号,微信小程序最终对外开放了,作为第一 ...
- string int 类型转换
string int 类型转换 (int) 此方法不适用于将string 转换为int 只能转换数值类型为int 而不能转换引用类型 不会四舍五入 直接去掉小数位 Conver.ToInt() 会 ...