struts基本工程结构:

1. struts.xml支持语法提示;
2. struts.xml配置常量, 用来覆盖struts.properties中的默认常量配置
  一般情况下, 这个配置放在struts.xml中, 不要放在各个模块的xml中
    constant元素和package是同一个级别的
 <struts>
  <constant name="" value=""></constant>
 </struts>
 
 3. 模块xml配置文件引入(将各个模块的xml分开写,之后导入struts.xml)
 <struts>
  <include file="各个模块的xml配置文件的目录"></include>
 </struts>

4. 常用常量(在struts核心包中可以找到文件,只需复制到xml中进行修改)
  1)开发模式:修改struts.xml之后,如果没有大的改动,则不要多次重启Tomcat.
   devMode模式是开发模式,开启它则默认开启了i18n.reload、configuration.xml.reload.
   <constant name="struts.devMode" value="true" />
  2)设置当struts的配置文件修改后,系统是否自动重新加载该文件: 默认值为false(生产环境下使用),开发阶段最好打开
   <constant name="struts.configuration.xml.reload" value="true"/>
  3)指定每次请求到达,重新加载资源文件
   <constant name="struts.i18n.reload" value="true"/>
  4)指定XSLT Result使用样式表缓存
   <constant name="struts.xslt.nocache" value="true"/>
  5)设置actionName的后缀: 默认值是action,即所有匹配*.action的请求都由Struts2处理。
   如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开
   <constant name="struts.action.extension" value="action,,"/>
  6)默认的视图主题
   <constant name="struts.ui.theme" value="simple" />
  7)设置浏览器是否缓存静态内容: 默认值为true(生产环境下使用),开发阶段最好关闭
   true: 表示有缓存, false:表示没有缓存
   <constant name="struts.serve.static.browserCache" value="false"/>
  8)设置是否支持动态方法调用
   <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
  9)指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出
   <constant name="struts.i18n.encoding" value="UTF-8"/>
  
 5. 各个元素
  1)package: 是struts.xml根元素(strtus)的子元素. 用来管理多个action元素.
   * 格式为:
    <package name="" extends="" namespace="" abstract="true/false">
   * 用法: 一个模块分出一个<package>元素.
   * 常用的属性:
    - name: 表示当前<package>元素的名称,多个<package>元素的name不能相同.
    - extends: 自定义的<package>元素必须继承于包struts-default或其的子包.
    - namespace: 命名空间,和actionName共同决定了一个action的访问路径, 与程序中的命名空间的作用类似.
    - abstract: 表示当前<package>元素是否是抽象的.
      如果为true,那么当前<package>就不能定义action元素,只能用于被其他<package>所继承.
  2)action: 是<package>元素的子元素,用于配置Action类. 表示对一次请求的动作的配置:找哪一个类的哪一个方法.
   * 语法:<action name="" class="" method="">
   * 常用属性:
    - name: action的名称,该名称和当前action所在package的namespace共同决定了访问路径.
     访问Action的格式:http://host:port/contextPath/namsspace/actionName[.action]
     注意:action名称没有/,在同一个<package>不能同名.
    - class: Action类的全限定名,表示把哪一个Action对象交给Strtus2框架来管理.
     class的默认值:com.opensymphony.xwork2.ActionSupport.
    - method: 当前action需要执行哪一个方法.
     method的默认值:execute().
  3)result元素:是<action/>元素的子元素,表示对应的action的执行结果: `action执行完成后输出哪个页面`.
   * 语法格式:<result name="" type=""></result>
   * 分类: 先在当前的Action中找, 找不到就去找<global-results>中的result, 如果还找不到→报错;
    - 全局: 配置在<package>元素中的<global-results>元素里面,<package>下的所有Action都可以跳转进去.
    - 局部: 配置在<action>元素里面,就只能在当前Action中跳转.
    $ 在配置的时候需要先配置global, 然后才能配置action
   * 常用属性及文本:
    - name属性: 同一个action中的result的name不能同名,该name就是action方法的返回结果.默认值:success.
    - type属性: 表示资源的跳转方式(请求转发/URL重定向...),这些属性存放在struts-default包.默认:dispatcher.
     ~ dispatcher: 请求转发:(Action请求转发到JSP),是默认值.
     ~ redirect: URL重定向:(Action重定向到JSP).
     ~ chain: 请求转发:(Action请求转发到Action).
     ~ redirectAction: URL重定向:(Action重定向到Action).
     ~ stream: 文件下载.
    - innerText: 表示需要跳转资源的路径.

Struts2框架配置文件加载顺序:
(服务器启动之后, 这些配置文件会按照顺序一一加载进内存, 进行类等匹配的时候才会去内存查找):
 1. default.properties
 2. struts-default.xml
 3. struts-plugin.xml
 4. struts.xml
 5. struts.properties
 6. web.xml

1.default.properties: 该文件保存在 struts2-core-x.x.x.jar的根路径的包org.apache.struts2中
  * 其中包含了Struts2的默认常量配置(比如actionName的extention等).
 2.struts-default.xml: 该文件保存在 struts2-core-x.x.x.jar根路径中
  * 其中包含了框架依赖的对象配置和结果类型,拦截器等配置.
 3.struts-plugin.xml: 该文件保存在Struts2框架的插件中:struts-`functionName`-plugin-x.x.x.jar.
 4.struts.xml: 该文件是一个自配置文件, 是web应用默认的struts配置文件, 配置自定义的Action和其他信息.
 5.struts.properties: 该文件是一个自配置文件, 用来覆盖default.properties中的常量配置.
  * 但是一般情况下, 我们不会配置这个文件, 需要的话可以直接在struts.xml中配置, 具体配置详情参考struts.xml
 6.web.xml: 就是在项目的web.xml中配置信息, 前端控制器等等, 一般也不糊在这里配置
 上述三个文件是我们可以修改操作的.
 
 如果多个文件配置了同一个struts2 常量,则后一个文件中配置的常量值会覆盖前面文件配置的常量值.
 注意:一般的,我们只在struts.xml中做常量配置.
 <constant name="struts.action.extension" value="action,do,,"/> .(可以在请求URL后面添加后缀名)

总的struts.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"> <struts> <constant name="struts.action.extension" value="action,nice,,"></constant> <!-- 加后缀 -->
<constant name="struts.devMode" value="true" /> <!-- 小的改动不需要再启动服务器 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 动态调用 --> <include file="\accesspath\hello_struts.xml"></include>
<include file="/listgloble/List_struts.xml"></include>
<include file="/multimethod/multimethod.xml"></include>
<include file="/servletapi/api.xml"></include>
<include file="/params/params.xml"></include> </struts>

action.Java类

package accesspath;

public class HelloStrutsAction {
public String hello(){
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
return "h";
}
}

action类的.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"> <struts>
<package name="struts-hello" extends="struts-default" abstract="true"></package>
<package name="nihao" namespace="/nice" extends="struts-hello">
<action name="hello" class="accesspath.HelloStrutsAction" method="hello">
<result name="h">/JSP/HE.jsp</result>
</action>
</package>
</struts>

list的action类

package listgloble;

public class ListAction {//如果不继承自ActionSupport,则需要根据普通类来处理,
//即,在xml中需要按普通方法的配置来写出name,class,method
public String list() {
return "success";
}
}

list.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"> <struts> <package name="struts-globle" extends="struts-default" abstract="true"></package> <package name="glob" namespace="/globle" extends="struts-globle">
    <!--globle 必须写在action之前 -->
<global-results>
<result>/JSP/glob.jsp</result>
</global-results> <action name="gg" class="listgloble.ListAction" method="list">
<!-- 程序进来后先执行此action,当有下面这句的时候, 先执行下面的result,若没有下面此句,则执行global-results里的-->
<!-- <result name="success">/JSP/HE.jsp</result> -->
</action>
</package> </struts>

多方法调用:

package multimethod;

import com.opensymphony.xwork2.ActionSupport;

public class MultiMethodAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String add() {
System.out.println("MultiMethodAction.add()");
return "add";
}
public String delete() {
System.out.println("MultiMethodAction.delete()");
return "delete";
}
public String update() {
System.out.println("MultiMethodAction.update()");
return "update";
}
public String query() {
System.out.println("MultiMethodAction.query()");
return "query";
} }

多方法调用的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"> <struts>
<package name="multium" namespace="/method" extends="struts-default" > <!-- 第一种方法,每一个方法都写一个action <action name="adds" class="multimethod.MultiMethodAction" method="add">
<result name = "add">/JSP/glob.jsp</result>
</action>
<action name="deletes" class="multimethod.MultiMethodAction" method="delete">
<result name = "delete">/JSP/HE.jsp</result>
</action>
<action name="updates" class="multimethod.MultiMethodAction" method="update">
<result name = "update">/JSP/glob.jsp</result>
</action>
<action name="querys" class="multimethod.MultiMethodAction" method="query">
<result name = "query">/JSP/HE.jsp</result>
</action>
-->
<!--
第二种方法:DMI(动态调用)
访问方式:http//host:port//contextPath/namespace/actionName!methodName <action name="dmi" class="multimethod.MultiMethodAction">
<result name = "add">/JSP/glob.jsp</result>
<result name = "delete">/JSP/glob.jsp</result>
<result name = "update">/JSP/glob.jsp</result>
<result name = "query">/JSP/glob.jsp</result>
</action>
-->
<!-- 第三种方法 通配符
http//host:port//contextPath/namespace/actions_methodName
-->
<action name="actions_*" class="multimethod.MultiMethodAction" method="{1}">
<result name = "add">/JSP/HE.jsp</result>
<result name = "delete">/JSP/glob.jsp</result>
<result name = "update">/JSP/HE.jsp</result>
<result name = "query">/JSP/glob.jsp</result>
</action> </package> </struts>

struts访问的更多相关文章

  1. struts访问jsp api内置对象的集中方式

    1 default-action-ref元素改元素用来配置默认的action,如果struts找不到对应的action,就会调用这个默认的action 2 dmi处理方式是通过请求action中的一个 ...

  2. 关于Struts访问不到静态资源的问题

    今天重新配置了Struts的项目进行开发,但是项目静态资源一直访问不到. 将一些静态资源放在WebRoot下的static包下面便于管理. 一开始以为采用拦截.do,只拦截do后缀的请求,解决了静态资 ...

  3. Struts访问的时候出现request=null的情况

    今天用Struts框架写个小应用的时候,出现了如下问题 private File upload;    private String uploadContentType;    private Str ...

  4. Struts访问序号的设置

  5. Struts访问servletAPI方式

    1.原理

  6. struts理解

    最近大家都在找工作,我好迷茫,觉得自己会的东西太少了.所以决定开始学习SSH三大框架. 首先是struts. struts是基于mvc模式的框架.(struts其实也是servlet封装,提高开发效率 ...

  7. Struts(七):action配置文件之通配符映射

    通配符映射:一个Web应用可能有成百上千个action声明,可以使用struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系. 通配符映射规则: 若找到多个匹配,没有通配符的那个将胜 ...

  8. Struts2(七) Struts2访问Servlet的API

    当接受表单参数,向页面保持数据时.要用到Struts访问Servlet 的API .下面只做参考,有错误或不同意见可以发送邮箱2440867831@qq.com  .建议大家看struts文档,源代码 ...

  9. Struts 2 执行流程 配置信息

    Struts 2 执行流程 首先,浏览器访问,经过Filter,Filter从src/struts.xml中寻找命名空间和action的名字,获取action类,从方法中拿到返回值,接着从result ...

随机推荐

  1. 为HTML表格添加交互功能------DataTables

    DataTables是一个功能强大的Javascript库,用于为HTML表格添加交互功能,虽然简单性是整个项目的核心设计原则,但入门看起来相当艰巨.但是,采取这些第一步并在您的网站上运行DataTa ...

  2. Codeforces 686 D - Kay and Snowflake

    D - Kay and Snowflake 思路: 树的重心 利用重心的一个推论,树的重心必定在子树重心的连线上. 然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心 ...

  3. 全栈性能测试修炼宝典--Jmeter实战(二)

    性能测试初体验 1.测试分类 从图中可以看出,性能测试在整个软件测试环节中占了50%的内容,比如负载测试.压力测试.性能测试.大数据量测试.恢复测试.内容泄露测试.竞品测试(比较测试)和可靠性测试. ...

  4. ubuntu 安装 firefox 的 jre plugin

    https://www.java.com/en/download/help/enable_browser_ubuntu.xml Mozilla Firefox Become the root user ...

  5. Servlet Exception and Error Handling

    Servlet API support for custom Exception and Error Handler servlets that we can congiure in deployme ...

  6. Python 编程快速上手 第十四章 处理 CSV 文件和 JSON 数据

    前言 这一章分为两个部分,处理 CSV 格式的数据和处理 JSON 格式个数据. 处理 CSV 理解 csv csv 的每一行代表了电子表格中的每一行,每个逗号分开两个单元格csv 的内容全部为文本, ...

  7. (转)3个常用基于Linux系统命令行WEB网站浏览工具(w3m/Links/Lynx)

    一般我们常用的浏览器肯定是基于可视化界面的图文结合的浏览界面效果,比如FireFox.Chrome.Opera等等,但是有些时候折腾和项目 的需要,在Linux环境中需要查看某个页面的文字字符,我们需 ...

  8. English Voice of <<if were a boy >>

    <if i were a boy>中英文歌词: If I were a boy 如果我是个男孩 Even just for a day 就算只是一天 I' roll out of bed ...

  9. English trip M1 - PC6 Likes and Dislike Teacher:Jade

    In this lesson you will learn to talk about likes and dislikes. 课上内容(Lesson) # 通常在习惯性的表达式用 it's 来表达w ...

  10. LeetCode--002--两数相加(java版)

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...