© 版权声明:本文为博主原创文章,转载请注明出处

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搜索顺序的更多相关文章

  1. 第二篇——Struts2的Action搜索顺序

    Struts2的Action的搜索顺序: 地址:http://localhost:8080/path1/path2/student.action     1.判断package是否存在,例如:/pat ...

  2. Struts2学习笔记 - Action篇<配置文件中使用通配符>

    有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下在配置文件中使用通配符,这里的关键就是struts.xml配置文件,在最简单的 ...

  3. Struts2学习:Action获取properties文件的值

    配置文件路径: 配置内容: 方法一: Action内被调用的函数添加下段代码: Properties props = new Properties(); props.load(UploadFileAc ...

  4. Struts2学习笔记 - Action篇<定义逻辑Action>

    有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这文章就谈论一下定义逻辑Action 这里主要关注的是struts.xml配置文件,一般情 ...

  5. Struts2学习笔记 - Action篇<动态方法调用>

    有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下Dynamic Method nvocation ,动态方法调用,什么是动态方 ...

  6. Linux 学习 (三) 文件搜索命令

    Linux达人养成计划 I 学习笔记 locate 文件名 搜索速度比较快 只能根据文件名搜索 搜索的是保存在 /var/lib/mlocate 的数据库(每天更新一次) 新建文件需要执行 updat ...

  7. Struts2 学习笔记--Action Method--接收参数

    struts2中的路径问题 注意:在jsp中”/”表示tomcat服务器的根目录,在struts.xml配置文件中”/”表示webapp的根路径,即MyEclipse web项目中的WebRoot路径 ...

  8. Struts2学习:Action使用@Autowired注入为null的解决方案

    1.pom.xml引入struts2-spring-plugin <dependency> <groupId>org.apache.struts</groupId> ...

  9. shell学习三十八天----运行顺序和eval

    运行顺序和eval shell从标准输入或脚本中读取的每一行称为管道,它包括了一个或多个命令,这些命令被一个或多个管道字符(|)隔开. 其实嗨哟非常多特殊符号可用来切割单个的命令:分号(;),管道(| ...

随机推荐

  1. Write with Vim (1)

    Write with Vim (1) 本文出自Svitter的blog 原文在之前的也曾发表 大约在一年前使用vim?这个也是个不确定的时间.具体什么时间使用Vim早已经忘记了. 现在用的还算顺手,但 ...

  2. 小米监控 open-falcon部署

    具体详情请参考官方文档  http://book.open-falcon.org/zh_0_2/quick_install/    centos6.8    建议centos7系统  否则后面按照官方 ...

  3. django model:auto_now_add 和 auto_now

    创建django的model时,有DateTimeField.DateField和TimeField三种类型可以用来创建日期字段,其值分别对应着datetime().date().time()三中对象 ...

  4. 微信小程序底部弹框动画

    在写小程序的时候,一般会碰到底部弹出动画,就像下面这样的效果 直接进入正题 https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-animation.ht ...

  5. 清理雪道(bzoj 2502)

    Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时 ...

  6. 小甲鱼PE详解之资源(PE详解11)

    原文出自:www.fishc.com 最近一直在安排第一届鱼C 学习班的事情,忙活了好一阵子,真是对不住大家,还大家久等了,这里要跟大家说声不好意思 ^_^ 今天我们来谈谈资源部分,资源部分可以说是 ...

  7. vue element-ui Tabs 标签页实现【更多】功能

    element-ui Tabs本身是没有更多功能的,如果在外边添加一个更多按钮,又非常不好看, 而利用API中Tabs Attributes的before-leave勾子方法可以实现这个功能, 简单P ...

  8. 大型网站优化-memcache技术

    大型网站优化-memcache技术 memory+cache 内存缓存 memcache简介 memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发 ...

  9. ArcGIS 10.2 二次开发,兼容Visual Studio 2012二次开发,完美安装教程

    GIS 经常安装是常有的事,每次重装系统都要浪费大半天去安装这个.所以凑这一次安装,把这个软件重新安装的步骤整理了一下,希望对大家有所帮助.这次整理的内容的关键优点是,对常见的出错内容进行了归纳整理. ...

  10. php分页显示文章列表

    <div class="content"> <ul> <?php $querySel = "select * from news where ...