接上篇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的更多相关文章

  1. Struts2 XML配置详解

    struts官网下载地址:http://struts.apache.org/   1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: S ...

  2. Struts2基本配置详解

    Struts2配置文件加载顺序 struts2 配置文件由核心控制器加载 StrutsPrepareAndExecuteFilter (预处理,执行过滤) init_DefaultProperties ...

  3. Struts2(三)配置详解

    一.概述 Struts2提供了多种可选的配置文件形式. 其中,struts-default.xml和default.properties是框架级别的配置文件,这两个文件在Struts的核心JAR包中, ...

  4. Struts2学习笔记二 配置详解

    Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...

  5. java web.xml配置详解(转)

    源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...

  6. JAVAEE——spring01:介绍、搭建、概念、配置详解、属性注入和应用到项目

    一.spring介绍 1.三层架构中spring位置 2.spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还 ...

  7. Spark log4j日志配置详解(转载)

    一.spark job日志介绍    spark中提供了log4j的方式记录日志.可以在$SPARK_HOME/conf/下,将 log4j.properties.template 文件copy为 l ...

  8. log4j.properties配置详解与实例

    log4j.properties配置详解与实例 第一步:加入log4j-1.x.x.jar到lib下. 第二步:在工程的src下下建立log4j.properties.内容如下: #OFF,syste ...

  9. Spring 入门 web.xml配置详解

    Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...

随机推荐

  1. C#基础知识整理:C#类和结构(1)

    1.结构功能特性? 实现代码?结构用struct关键字定义的,与类类似,但有本质区别.结构实质是一个值类型,它不需要对分配的.结构的特性:(1).结构作为参数传递时,是值传递.(2).结构的构造函数必 ...

  2. Flask Restful服务简单实现

    官网:http://flask.pocoo.org/docs/1.0/quickstart/#routing 1.安装 windows下:pip3 install Flask 具体参照:windows ...

  3. MonoDB的数据准备

      首先是数据的录入,为了分析我们服务器集群的性能,需要准备大量的用户数据,幸运的是mtools提供了mgenerate方法供我们使用.他可以根据一个数据模版向 MongoDB 中插入任意条 json ...

  4. Android应用Preference相关及源代码浅析(SharePreferences篇)

    1 前言 在我们开发Android过程中数据的存储会有非常多种解决方式,譬如常见的文件存储.数据库存储.网络云存储等,可是Android系统为咱们提供了更加方便的一种数据存储方式.那就是SharePr ...

  5. Android Device Monitor工具的使用

    在最新的Android Studio3.x版本中,已经去掉了Android Device Monitor工具,但是不代表Android Device Monitor工具就不能用了,找到sdk的目录: ...

  6. iOS9 Error Domain=NSURLErrorDomain Code=-1022 App Transport Security (ATS)

    iOS 9在HTTP 访问时会出错  iOS9 Error Domain=NSURLErrorDomain Code=-1022 这时需要修改info.plist 文件 在Info.plist中添加N ...

  7. iOS开发技巧 - 使用Alerts和Action Sheets显示弹出框

    解决方案: (Swift) 使用UIAlertController类 (Objective-C) 使用UIAlertView类 代码: (Swift) import UIKit class ViewC ...

  8. Using LACP with a vSphere Distributed Switch 5.1

    Using LACP with a vSphere Distributed Switch 5.1 by Chris Wahl on Oct 15th, 2012 | 6,347 views One o ...

  9. C++ 第六课:C/C++关键字及其用法

    asm 插入一个汇编指令. auto 声明一个本地变量. bool 声明一个布尔型变量. break 结束一个循环. case 一个switch语句的一部分. catch 处理 thrown 产生的异 ...

  10. Linux安装和设置Samba服务器

    1. 安装 安装前先关闭iptables和SELinux. Centos输入以下命令: yum install samba samba-client Ubuntu输入以下命令: apt-get ins ...