接上篇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. Jquery取得iframe中元素的几种方法(转载)

    iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作 DOM方法:父窗口操作IFRAME:window.frames["iframeSon ...

  2. 提高sql2005中带freetexttable和rank的全文检索的速度

    原来使用中的sql2005的全文检索的速度总是不如意,尤其是带rank的.今天搜了一下,原来在freetexttable中还有一个参数top_n_by_rank,是第4个参数.注意还要设置:EXEC ...

  3. 不能从const char *转换为LPCWSTR --VS经常碰到

    不能从const char *转换为LPCWSTR 在VC 6.0中编译成功的项目在VS2005 vs2005.vs2008.vs2010中常会出现类型错误. 经常出现的错误是:不能从const ch ...

  4. Android -- java代码设置margin

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  5. PDO 增删改查封装的类

    Selecting Data 你在mysql_*中是这样做的 <?php $result = mysql_query('SELECT * from table') or die(mysql_er ...

  6. POJ 3009:Curling 2.0 推箱子

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14090   Accepted: 5887 Desc ...

  7. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

  8. 【转】application.properties 常见配置

    Various properties can be specified inside your application.properties/application.yml file or as co ...

  9. Spring开发 - 通过实现ApplicationContextAware在Servlet中调用注解的Service

    用过Spring MVC的人都知道,我们如何在Controller中注入Service,可以使用@Resource注解的方法. 有时候,实际在项目的过程中,我们需要在某个Servlet中使用Servi ...

  10. 分布式系统介绍-PNUTS

    PNUTS是Yahoo!的分布式数据库系统,支持地域上分布的大规模并发操作.它根据主键的范围区间或者其哈希值的范围区间将表拆分为表单元(Tablet),多个表单元存储在一个服务器上.一个表单元控制器根 ...