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配置详解

原文地址:https://www.cnblogs.com/Jeely/p/11438063.html

时间: 2024-08-28 14:16:44

struts2基本配置详解2的相关文章

Struts2 XML配置详解

struts官网下载地址:http://struts.apache.org/ 1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管理Action和拦截器等.每个包就是多个Action.多个拦截器.多个拦截器引用的集合. 在struts.xml文件中package元素用于定义包配置,每个package元素定义了一个包配置.它的常用属性有: l name

Struts2基本配置详解

Struts2配置文件加载顺序 struts2 配置文件由核心控制器加载 StrutsPrepareAndExecuteFilter (预处理,执行过滤) init_DefaultProperties(); // [1] ---------- org/apache/struts2/default.properties init_TraditionalXmlConfigurations(); // [2] --- struts-default.xml,struts-plugin.xml,strut

struts2使用配置详解

struts2.xml的文件配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <strut

struts2的执行流程与配置详解

本章主要讲解Struts的执行流程以及Struts的配置以及访问servletApi 全部代码下载: github链接:链接 写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github:多谢: 1.Struts的执行流程: 1.服务器启动时: 加载项目web.xml 创建Struts核心过滤器对象, 执行StrutsPrepareAndExecuteFilter的doFilter 的 init()方法: 在StrutsPrepareAndExecuteFilter

【SSH2(理论篇)】--Struts2配置详解

上篇博客讨论了SSH2框架模型,在开发过程中发现SSH2的开发模型其实类似于经典的三层模式,在每一层中分别添加了不同的框架,显示层使用的是Struts2进行配置的,业务逻辑层使用的是Spring配置,数据持久层则采用的是Hibernate,开发模式简单易懂,接下来将会分别从三层着手讨论每一层的运行内容. 一.Struts体系简介 struts,是Apache软件基金会(ASF)赞助的一个开源项目,它通过采用Java Servlet/JSP技术,实现了基于Java EE Web应用的Model-V

(转)struts2.0配置文件、常量配置详解

一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.propreties文件配置,是因为为了保持与WebWork的向后兼容 2.在实际开发中,在web.xml中配置常量相比其他两种,需要更多的代码量,会降低了web.xml的可读性 3.通常推荐在struts.xml文件中配置struts2的常量,而且便于集中管理 sturt2中搜索加载常量的顺序是: str

Struts2学习笔记二 配置详解

Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的namespace,然后在包下寻找名为HelloAction的action,反射创建控制器HelloAction对象,调用method指定的方法hello,拿到返回值“success”,去result的配置节找success对应的页面(hello.js),呈现给用户. 2.Struts的架构,具体参考这里

Struts2配置详解_配置Action

Struts2的核心功能是action,对于开发人员来说,使用Struts2主要就是编写action,action类通常都要实现com.opensymphony.xwork2.Action接口,并实现该接口中的execute()方法. public String execute() throws Exception Struts2并不是要求所有编写的action类都要实现Action接口,也可以直接编写一个普通的Java类作为action,只要实现一个返回类型为String的无参的public方

Struts2配置详解

1.Namespace 1)namespace决定action的访问路径,默认为“”,可以接受所有路径的Action:       2)namespace可以写为/,或者/xxx,或者/xxx/yyy,对应action访问路径为/index.action,/xxx/index.action或者/xxx/yyy/index.action       3)namespace最好也用模块来进行命名 当url中输入不存在的Action的时候,定向到默认的Action 1.1 struts2配置详解 1.