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

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. ubuntu php编译安装配置

    安装参考:http://ilanni.blog.51cto.com/526870/1569322/ 加载扩展的一些参数参考:http://java-er.com/blog/nginx-php-fpm/

  2. android程序入口

    android程序的真正入口是Application类的onCreate方法

  3. Android蓝牙介绍

    1. 介绍 自从Android 4.2开始,Android开始使用自己的蓝牙协议栈BlueDroid,而不是bluez BlueDroid可分为两层: - BTE: Bluetooth Embedde ...

  4. linux内核情景分析之锁机制

    /* * These are the generic versions of the spinlocks and read-write * locks.. *///自旋锁加锁,irqsave表示把标志 ...

  5. php通过$_SERVER['HTTP_USER_AGENT']获取浏览器相关参数

    最近不忙,同事在忙一个app项目.当听到领导安排让他做一个判断苹果还是安卓手机,如果是安卓手机下载安卓app.如果是苹果手机下载苹果app;然后我就上网搜了一下学习学习: php通过$_SERVER[ ...

  6. Python Challenge 第十三关

    第13关.一张电话的图片,一句话:phone that evil.看到电话,加上之前关卡有些图片有链接,我就在电话按键上都点点试试,果然 5 是个链接,就点了进去.出来一个XML文件,第一句写着:Th ...

  7. ie8 不能加载dll的问题解决

    请问是在打开IE的时候提示无法加载DLL文件吗? 请尝试重置IE: 1. 关闭所有Internet Explorer窗口. 2. 单击开始,点击运行,输入inetcpl.cpl,按回车. 3. 点击高 ...

  8. Mac测试模拟慢网速

    先普及一下Kb,KB,Kbps,Mb,Mbps等一些列概念 1Byte = 8bit 1KB (Kilobyte 千字节)=1024Byte 1MB (Megabyte,兆字节,简称“兆”)=1024 ...

  9. ios文件管理

    <Application_Home>/AppName.app This is the bundle directory containing the applicationitself. ...

  10. git——简易指南

    Git对于我来说,只知道是一个版本控制器,类似于乌龟的svn.其中也仅仅会几个常的命令,比如说“更新git pull”.“提交git push”等等,因为记得当初使用的时候,师傅告诉我,对于你不懂这个 ...