struts访问
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访问的更多相关文章
- struts访问jsp api内置对象的集中方式
1 default-action-ref元素改元素用来配置默认的action,如果struts找不到对应的action,就会调用这个默认的action 2 dmi处理方式是通过请求action中的一个 ...
- 关于Struts访问不到静态资源的问题
今天重新配置了Struts的项目进行开发,但是项目静态资源一直访问不到. 将一些静态资源放在WebRoot下的static包下面便于管理. 一开始以为采用拦截.do,只拦截do后缀的请求,解决了静态资 ...
- Struts访问的时候出现request=null的情况
今天用Struts框架写个小应用的时候,出现了如下问题 private File upload; private String uploadContentType; private Str ...
- Struts访问序号的设置
- Struts访问servletAPI方式
1.原理
- struts理解
最近大家都在找工作,我好迷茫,觉得自己会的东西太少了.所以决定开始学习SSH三大框架. 首先是struts. struts是基于mvc模式的框架.(struts其实也是servlet封装,提高开发效率 ...
- Struts(七):action配置文件之通配符映射
通配符映射:一个Web应用可能有成百上千个action声明,可以使用struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系. 通配符映射规则: 若找到多个匹配,没有通配符的那个将胜 ...
- Struts2(七) Struts2访问Servlet的API
当接受表单参数,向页面保持数据时.要用到Struts访问Servlet 的API .下面只做参考,有错误或不同意见可以发送邮箱2440867831@qq.com .建议大家看struts文档,源代码 ...
- Struts 2 执行流程 配置信息
Struts 2 执行流程 首先,浏览器访问,经过Filter,Filter从src/struts.xml中寻找命名空间和action的名字,获取action类,从方法中拿到返回值,接着从result ...
随机推荐
- appium自动化测试 环境搭建
最 近接手的项目是移动端自动化测试 ,需要用的appium ,头一回使用, 项目特点:1)数据有时效性,需要在短时间内验证大量数据, 2) 人工去一个一个核对发现不了太多BUG. 环境搭建:参考虫师的 ...
- MySQL Server and Server-Startup Programs
1. mysqld-The MySQL Server mysqld,also known as mysql server, is the main program that does most of ...
- 算法:最短路径之弗洛伊德(Floyd)算法
https://cloud.tencent.com/developer/article/1012420 为了能讲明白弗洛伊德(Floyd)算法的主要思想,我们先来看最简单的案例.图7-7-12的左图是 ...
- 雷林鹏分享:jQuery EasyUI 扩展
jQuery EasyUI 扩展 Portal(制作图表.列表.球形图等) 数据网格视图(DataGrid View) 可编辑的数据网格(Editable DataGrid) 可编辑的树(Editab ...
- ROC曲线(receiver-operating-characteristic curve)-阈值评价标准(转)
转自:http://blog.csdn.net/abcjennifer/article/details/7359370 ROC曲线指受试者工作特征曲线 / 接收器操作特性曲线(receiver ope ...
- ORM--Object Relational Mapping
ORM 对象关系映射 Object Relational Mapping, 简称ORM,或O/RM,或O/R mapping 一种程序技术 用于实现面向对象编程语言里 不同类型系统 ...
- 使用validateXxx()方法进行输入校验 --Struts2框架
1.本例是在使用validate()方法进行输入校验 --Struts2框架的基础上接着做的,上一篇使用validate()方法进行输入校验时会对当前Action中的所有方法有效,由于Struts2框 ...
- 20190118_xlVBA多表合并
Public Sub simple() Set wb = ActiveWorkbook Set sht = ActiveSheet msg = MsgBox("程序准备清除活动工作表内容?按 ...
- 20180518VSTO多簿单表汇总
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- arm ncnn
ncnn网址:https://github.com/Tencent/ncnn 1. sudo apt-get update sudo apt-get upgrade 2. 命令:sudo apt-ge ...