(2)struts2配置祥解
struts工作流程
反射 :
1.构造对象使用构造器
//类似为Servlet
public class AddAction { public AddAction(){
System.out.println("框架反射创建AddAction实例");
}
2.web.xml
2.4版本的servlet规范在部属描述符中新增加了一个<dispatcher>元素,这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,
可以在一个<filter-mapping>元素中加入任意数目的<dispatcher>,使得filter将会作用于
直接从客户端过来的request,通过forward过来的request,通过include过来的request,通过<error-page>过来的request。
如果没有指定任何< dispatcher >元素,默认值是REQUEST。
<!-- Struts2核心过滤器,专用于过滤所有请求-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
由于AddAction中是转发所以不经过StrutsPrepareAndExecuteFilter。要使其经过改成dispatcher为FORWARD
*四)struts2其本配置详解(上)
(一)名称空间和访问Action路径规则
访问URL=名称空间 / <aciton标签的name属性值>
名称空间:页面名相同时,用namespace区分请求路径
(二)默认名称空间和测试Action的访问路径规则
默认命名空间是"/",可不写;
若名称空间是/,url=http:localhost:8080/day31/add
http://localhost:8080/day31/add?num1=100&num2=200
若名称空间是/xx,url=http://localhost:8080/day31/xx/add
http://localhost:8080/day31/xx/add?num1=100&num2=200
为什么需要namespace?
如果名称空间是/,且<action>的name属性值为add
如果URL是:http://127.0.0.1:8080/day31/xx/yy/zz/add回车
http://127.0.0.1:8080/day31/xx/yy/zz/add(正确)
http://127.0.0.1:8080/day31/xx/yy/add(正确)
http://127.0.0.1:8080/day31/xx/add(正确)
http://127.0.0.1:8080/day31/add(正确)
如果名称空间是/xx,且<action>的name属性值为add
如果URL是:http://127.0.0.1:8080/day31/xx/yy/zz/add回车
http://127.0.0.1:8080/day31/xx/yy/zz/add(正确)
http://127.0.0.1:8080/day31/xx/yy/add(正确)
http://127.0.0.1:8080/day31/xx/add(正确)
http://127.0.0.1:8080/day31/add(出错)
结论:不管URL请求是什么,namespace的属性值,一定名称空间的底线,不能少于namespace的值,
但可以多于namespace的值。
路径中,大小写敏感
(3)如果没有指定<action>的method属性
默认执行Action中的execute()方法
(4)推荐,文件只存在加载和解析,没有编译,所以尽量在xml中配置
execute()方法使其return "success";
//转发到add.jsp中 由struts.xml中的result代替
//request.getRequestDispatcher("/add.jsp").forward(request, response);
return "success";
web.xml
<!-- struts2的核心配置文件,在应用部署时加载并解析 -->
<struts>
<package name="base" extends="struts-default" namespace="/"><!-- struts2内部的一个核心包 -->
<action name="add" class="cn.itcast.web.struts2.add.AddAction" >
<result name="success" type="dispatcher">
/add.jsp
</result>
</action>
</package>
</struts>
如果没有指定<result>的name属性
name默认为Action中execute()方法的返回值:小写字母"success"
(5)如果没有指定<result>的type属性
type默认为"dispatcher"
struts2专用转发
(6)设置访问Action的扩展名的二种设置方式【struts.properties和struts.xml】
struts.action.extension=action,,
struts2访问Action的扩展名默认为:无或者是.action(小写)
修改Actin访问的扩展名:
A)src/struts.xml文件配置扩展名(value中四种)
<!-- 修改action的扩展名方式1 -->
<constant name="struts.action.extension" value="xx,qq,aciton,,"></constant>
http://localhost:8080/day31/add.qq?num1=100&num2=100
B)src/struts.properties文件配置扩展名
struts.action.extension=yy,do
struts.i18n.encoding=UTF-8
上述二种方式,最终都会替换框架默认的扩展名
加载的顺序是:先加载框架的,后加载程序员的
当properties和xml文件同时存在时,properties文件起决定作用。
*四)struts2其本配置详解(下)
(7)指定包含多个struts.xml配置文件的规则
src/struts.xml是总的配置文件,用总的配置文件能过<include>标签,去包含子的配置文件,
便用多个模块操作,一个模块使用一个xml文件。
get.jsp
<a href="/day31/get" style="text-decoration:none">
这是GET请求
</a>
GetAction
package cn.itcast.web.struts2.add; public class GetAction {
public String execute(){
return "success";
}
}
WEB-INF下ok.jsp
<body>
ok.jsp
</body>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="get" extends="struts-default" namespace="/">
<action
name="get"
class="cn.itcast.web.struts2.add.GetAction"
method="execute">
<result name="success" type="dispatcher">
/WEB-INF/ok.jsp
</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="add" extends="struts-default" namespace="/">
<action name="add" class="cn.itcast.web.struts2.add.AddAction">
<result name="success" type="redirect">
/add.jsp
</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- struts2的核心配置文件,在应用部署时加载并解析 -->
<struts>
<!-- 包含子的xml文件 -->
<include file="cn/itcast/web/struts2/add/struts_add.xml"/>
<include file="cn/itcast/web/struts2/get/struts_get.xml"/>
</struts>
(8)Action采用单例还是非单例模式,需要解决线程安全问题吗?
一次请求创建一个Action实例,每次都不同。无线程安全问题
在AddAction中private Integer age;在execute中age++;不会有线程安全问题、
(2)struts2配置祥解的更多相关文章
- struts2系列(三):struts2配置详解
原文链接:http://www.cnblogs.com/fmricky/archive/2010/05/20/1740479.html 1.<include> 利用include标签,可以 ...
- struts2配置详解
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- Struts2 配置详解
1. web.xml 此文件的配置可以参看struts2的示例文档 <filter> <filter-name>struts2</filter-name> < ...
- Struts2配置详解_配置Action
Struts2的核心功能是action,对于开发人员来说,使用Struts2主要就是编写action,action类通常都要实现com.opensymphony.xwork2.Action接口,并实现 ...
- log4j配置祥解
第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 1 log4j.rootCategory=INFO, stdou ...
- 第三章 Struts2配置详解
3.1 Struts2执行过程 1.获取Struts2资源 2.在应用程序中导入Struts2的类库 3.在web.xml中配置StrutsPrepareAndExecuteFilt ...
- Struts2学习笔记二 配置详解
Struts2执行流程 1.简单执行流程,如下所示: 在浏览器输入请求地址,首先会被过滤器处理,然后查找主配置文件,然后根据地址栏中输入的/hello去每个package中查找为/hello的name ...
- struts2基本配置详解2
接上篇struts2基本配置详解,还有一些配置没有讲到,下面将继续. struts.xml <package name="com.amos.web.action" names ...
- Struts2学习笔记(二)——配置详解
1.Struts2配置文件加载顺序: default.properties(默认常量配置) struts-default.xml(默认配置文件,主要配置bean和拦截器) struts-plugin. ...
随机推荐
- mysql 导入sql文件时编码报错
1.命令行导入 mysql -uroot -pnewpwd --default-character-set=utf8 databasename < xxx.sql 2.使用source导入 进入 ...
- .net mvc项目 ajax
经常在后台用一般处理程序(.ashx)来处理前台的ajax请求 using System; using System.Collections.Generic; using System.IO; usi ...
- hdu 4862 KM算法 最小K路径覆盖的模型
http://acm.hdu.edu.cn/showproblem.php?pid=4862 选t<=k次,t条路要经过全部的点一次而且只一次. 建图是问题: 我自己最初就把n*m 个点分别放入 ...
- the Determine in June
今天是6月10号,周三. 自己做的CSDN博客端(仿小巫博客,ps:里边的框架和代码优化都是自己又一次用新框架做的或者自己又一次实现)也已经有三天了.进度还差非常多--- 结合了上次投简历的经验和期末 ...
- 我的ngnix 配置内容
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- ReactNative Navigator
https://facebook.github.io/react-native/docs/navigator.html Navigator实现了页面之间的跳转. Demo描述:打开即进入“课程”页面, ...
- 移动app性能测试(待完善)
移动终端性能测试是测试手机终端是否符合特定性能指标的测试,包括有:内存.CPU.电量.流量.流畅度.时延等 测试准备:测试账号.测试需求.测试用例.待测手机.待测应用包.测试工具.测试电脑 1. 时 ...
- 有一个投篮游戏。球场有p个篮筐,编号为0,1...,p-1。每个篮筐下有个袋子,每个袋子最多装一个篮球。有n个篮球,每个球编号xi 。规则是将数字为xi 的篮球投到xi 除p的余数为编号的袋里。若袋里已有篮球则球弹出游戏结束输出i,否则重复至所有球都投完。输出-1。问游戏最终的输出是什么?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- zookeeper参数的详解
安装和配置详解 本文介绍的 Zookeeper 是以 3.2.2 这个稳定版本为基础,最新的版本可以通过官网 http://hadoop.apache.org/zookeeper/来获取,Zookee ...
- Two stage U-Boot design
In AM335x the ROM code serves as the bootstrap loader, sometimes referred to as the Initial Program ...