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

posted @ 2017-05-04 12:46  禁忌夜色153  阅读(244)  评论(0编辑  收藏  举报