Struts上路_02-入门流程

1.jar包:

Struts上路_02-入门流程

2.配置web.xml

Struts上路_02-入门流程

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <display-name></display-name>

    <!-- Struts2的核心拦截器 -->
    <filter>
        <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>


3.配置struts.xml

此文件在src目录下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd"> 

<struts>
	<!--
	 package可以有多个,其中又可包含多个action。
	 name:自定义包名。必选项,唯一的。用于提供给其他包来继承的,代码复用
	 namespace:自定义命名空间,可选项,有且至少有"/"。即请求连接的前半部分 -->
	<package name="mypackage" namespace="/mypackageofaction"
		extends="struts-default">

		<!--
			name:自定义action名称。即对应的请求连接中的后半部分。
			class:对应要执行的action类的完整路径 -->
		<action name="ActionHello" class="cn.cvu.action.ActionHello">

			<!--
			 name:对应执行的action类myAction的方法的返回值,默认值为"success"
			  文本内容:要转向到的页面。 -->
			<result name="success">/HelloSuccess.jsp</result>
		</action>
		
		<action name="ActionGood" class="cn.cvu.action.ActionGood">
			<result name="success">/GoodSuccess.jsp</result>
		</action> 
		<action name="ActionUser" class="cn.cvu.action.ActionUser">
			<result name="success">/UserSuccess.jsp</result>
		</action>
	</package>
	
      <!--  第二个命名空间 -->
	<package name="newpackage" namespace="/newpackageofaction" extends="struts-default">
		<action name="ActionNewPackage" class="cn.cvu.action.ActionNewPackage">
			<result name="success">/NewSuccess.jsp</result>
		</action>
	</package>

      <!--  第3个命名空间,继承自第2个 -->
	<package name="newpackage_2" namespace="/newpackageofaction_2" extends="newpackage">
		<!-- 因为继承自newpackage包,ActionNewPackage处理配置就可以直接使用了
		<action name="ActionNewPackage" class="cn.cvu.action.ActionNewPackage">
			<result name="success">/NewSuccess.jsp</result>
		</action> -->
		
		<!-- 创建自己的Action处理配置 -->
		<action name="ActionNewPackage_2" class="cn.cvu.action.ActionNewPackage_2">
			<result name = "success">/NewSuccess_2.jsp</result>
		</action>
	</package>

</struts>


4.请求处理类-Action:

对应struts.xml文件中package/actionclass属性。

1

package cn.cvu.action;
import com.opensymphony.xwork2.Action;
//myAction
public class ActionHello implements Action {
	public String execute() throws Exception {
		System.out.println("Hello i am in struts !");
		return SUCCESS; 	//对应到struts.xml中<result name="success"> 
	}
}


2

package cn.cvu.action;

import com.opensymphony.xwork2.Action;

public class ActionGood implements Action {
	public String execute() throws Exception {
		System.out.println("Good, i am in struts !");
		return SUCCESS; 
	}
}


3

package cn.cvu.action;

import com.opensymphony.xwork2.Action;

public class ActionUser implements Action {
	public String execute() throws Exception {
		System.out.println("User, i am in struts !");
		return SUCCESS;  
	}
}


4)第二个命名空间指定的处理类

package cn.cvu.action;

import com.opensymphony.xwork2.Action;

public class ActionNewPackage implements Action {
	public String execute() throws Exception {
		System.out.println("NewPackage, i am in struts !");
		return SUCCESS; 
	}
}


5

package cn.cvu.action;

import com.opensymphony.xwork2.Action;

public class ActionNewPackage_2 implements Action {
	public String execute() throws Exception {
		System.out.println("NewPackage 2 , 不经action类处理,直接转向<result>指定的页面 亦可!");
		return SUCCESS;  
	}
}


5.请求页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
	<a href="${ pageContext.request.contextPath }/mypackageofaction/ActionHello.action">包中的 Hello</a>
	<br>
	<a href="${ pageContext.request.contextPath }/mypackageofaction/ActionGood.action">包中的 Good</a>
	<br>
	<a href="${ pageContext.request.contextPath }/mypackageofaction/ActionUser.action">包中的User</a>
	<br>
	<a href="${ pageContext.request.contextPath }/newpackageofaction/ActionNewPackage.action">新的包中的 NewPackage</a>
	<br>
	<a href="${ pageContext.request.contextPath }/newpackageofaction_2/ActionNewPackage.action">继承自上级包的NewPackage</a>
	<br>
	<a href="${ pageContext.request.contextPath }/newpackageofaction_2/ActionNewPackage_2.action">继承包的自己的 NewPackage_2</a>
</body>

</html>


6.响应页面:

1

<title>HelloSuccess.jsp</title>
</head>
<body>
	Hello页面跳转成功!
</body>


2

<title>GoodSuccess.jsp</title>
</head>
<body>
	Good页面跳转成功!
</body>


3

<title>UserSuccess.jsp</title>
</head>
<body>
	User页面跳转成功!
</body>


4

<title>NewSuccess.jsp</title>
</head>
<body>
	New Package 页面跳转成功!
</body>


5

<title>NewSuccess_2.jsp</title>
</head>
<body>
	New Package 2  页面跳转成功!
</body>


7.发布访问:

1.

Struts上路_02-入门流程

2.

Struts上路_02-入门流程

- end

你可能感兴趣的:(mvc,struts,struts2,javaweb,Webwork)