Struts2学习三----------Action搜索顺序
© 版权声明:本文为博主原创文章,转载请注明出处
Struts2的Action的搜索顺序
http://localhost:8080/path1/path2/student.action
1)判断package是否存在,如:/path1/path2
2)存在,判断action是否存在,没有,则报错
3)不存在,检查上一级路径的package是否存在(直到默认的namespace),重复第一步,没有,则报错
实例
1.项目结构

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.struts</groupId>
<artifactId>Struts2-ActionSearchOrder</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Struts2-ActionSearchOrder Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
<!-- 统一Struts2的开发环境 -->
<struts.version>2.5.10</struts.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies> <build>
<finalName>Struts2-ActionSearchOrder</finalName>
</build> </project>
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <!-- 配置Struts2过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
4.SearchAction.java
package org.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class SearchAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String search() throws Exception {
return SUCCESS;
}
}
5.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts> <package name="search1" extends="struts-default" namespace="/">
<action name="search" class="org.struts.action.SearchAction" method="search">
<result>/search.jsp</result>
</action>
<action name="CommonSearch" class="org.struts.action.SearchAction" method="search">
<result>/Common.jsp</result>
</action>
</package> <package name="search2" extends="struts-default" namespace="/aa">
<action name="CommonSearch" class="org.struts.action.SearchAction" method="search">
<result>/aaCommon.jsp</result>
</action>
</package> </struts>
6.aaCommon.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is aaCommon.jsp</title>
</head>
<body>
This is aaCommon.jsp
</body>
</html>
7.Common.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is Common.jsp</title>
</head>
<body>
This is Common.jsp
</body>
</html>
8.search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is search.jsp</title>
</head>
<body>
This is search.jsp
</body>
</html>
9.效果预览
9.1 访问http://localhost:8080/Struts2-ActionSearchOrder/aa/CommonSearch.action,此时存在namespace="/aa"的package,搜索action,存在

9.2 访问http://localhost:8080/Struts2-ActionSearchOrder/aa/search.action,此时存在namespace="/aa"的package,搜索action,不存在,报错

9.3 访问http://localhost:8080/Struts2-ActionSearchOrder/aa/bb/CommonSearch.action,不存在namespace="/aa/bb"的package,搜索上一级路径的package,此时存在namespace="/aa"的package,搜索action,存在

9.4 访问http://localhost:8080/Struts2-ActionSearchOrder/CommonSearch.action,存在namespace="/"的package,搜索action,存在

9.5 访问http://localhost:8080/Struts2-ActionSearchOrder/search.action,存在namespace="/"的package,搜索action,存在

参考:http://www.imooc.com/video/8998
Struts2学习三----------Action搜索顺序的更多相关文章
- 第二篇——Struts2的Action搜索顺序
Struts2的Action的搜索顺序: 地址:http://localhost:8080/path1/path2/student.action 1.判断package是否存在,例如:/pat ...
- Struts2学习笔记 - Action篇<配置文件中使用通配符>
有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下在配置文件中使用通配符,这里的关键就是struts.xml配置文件,在最简单的 ...
- Struts2学习:Action获取properties文件的值
配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...
- Struts2学习笔记 - Action篇<定义逻辑Action>
有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这文章就谈论一下定义逻辑Action 这里主要关注的是struts.xml配置文件,一般情 ...
- Struts2学习笔记 - Action篇<动态方法调用>
有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下Dynamic Method nvocation ,动态方法调用,什么是动态方 ...
- Linux 学习 (三) 文件搜索命令
Linux达人养成计划 I 学习笔记 locate 文件名 搜索速度比较快 只能根据文件名搜索 搜索的是保存在 /var/lib/mlocate 的数据库(每天更新一次) 新建文件需要执行 updat ...
- Struts2 学习笔记--Action Method--接收参数
struts2中的路径问题 注意:在jsp中”/”表示tomcat服务器的根目录,在struts.xml配置文件中”/”表示webapp的根路径,即MyEclipse web项目中的WebRoot路径 ...
- Struts2学习:Action使用@Autowired注入为null的解决方案
1.pom.xml引入struts2-spring-plugin <dependency> <groupId>org.apache.struts</groupId> ...
- shell学习三十八天----运行顺序和eval
运行顺序和eval shell从标准输入或脚本中读取的每一行称为管道,它包括了一个或多个命令,这些命令被一个或多个管道字符(|)隔开. 其实嗨哟非常多特殊符号可用来切割单个的命令:分号(;),管道(| ...
随机推荐
- swarm集群数据管理
1.volume [root@manager ~]# docker service create --mount type=volume,src=vol1,dst=/usr/local/nginx/h ...
- POJ 3090
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8397 Accepted: ...
- [LeetCode] Search a 2D Matrix 二分搜索
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【字符集及字符编码】UTF-8、UTF-16和UTF-32
UTF-32 用 4 个字节存储每一个字符,以保证能把 UCS 完全表达出来.但实际上 UCS 的字符数量根本不需要用 32 位表示,UTF-32 极大地浪费了空间.另外,由于组合字符的存在,定长表示 ...
- 老郭带你学数据结构(C语言系列)2-线性表之动态顺序表
一.基本概念: 线性表:由n个类型相同的数据元素组成的有限序列,记为(a1,a2,--an). 线性表的特征:其中的元素存在这序偶关系,元素之间存在着严格的次序关系. 顺序存储表:线性表中的元素依次存 ...
- 如何得知 GIC 的所有中斷
can get the supported GIC interrupts from the below adb command, adb root adb shell cat /proc/interr ...
- 最简单方法远程调试Python多进程子程序
Python 2.6新增的multiprocessing,即多进程,给子进程代码调试有点困难,比如python自带的pdb如果直接在子进程代码里面启动会抛出一堆异常,原因是子进程的stdin/out/ ...
- 浅谈控件(组件)制作方法一(附带一delphi导出数据到Excel的组件实例)(原创)
来自:http://blog.csdn.net/zhdwjie/article/details/1490741 -------------------------------------------- ...
- CF501D Misha and Permutations Summation(康托展开)
将一个排列映射到一个数的方法就叫做康托展开.它的具体做法是这样的,对于一个给定的排列{ai}(i=1,2,3...n),对于每个ai求有多少个aj,使得j>i且ai>aj,简单来说就是求a ...
- php基础 gd图像生成、缩放、logo水印和验证码
gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等. 图像生成 <?php /* 用windows画图板画图 1.新建空白画布(指定 ...