1. 程式人生 > >Struts2學習三----------Action搜索順序

Struts2學習三----------Action搜索順序

round col uil 1-1 earch sch 預覽 image pin

? 版權聲明:本文為博主原創文章,轉載請註明出處

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搜索順序