分层思想:
HelloWorldActio:
package cn.itcast.action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action {
public String execute() throws Exception {
System.out.println("HelloWorldAction ************* execute()");
return "success";
}
}
Struts.xml
<?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>
<!-- /primer/helloWorldAction.action
package:包
* name:包名,唯一的,必选项
* namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分
* extends:继承
* extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
* 为什么要继承这个struts-default.xml文件?
-->
<package name="primer" namespace="/primer" extends="struts-default">
<!--
action:
* name:对应页面中请求连接的后面半部分
* class:对应要执行的类的完整路径
-->
<action name="helloWorldAction" class="cn.itcast.action.HelloWorldAction">
<!--
result:结果类型
* name:对应的是执行的类的方法的返回值
public String execute() throws Exception {
System.out.println("HelloWorldAction ************* execute()");
return "success";
}
* 后半部分的文本内容:要转向到的页面
-->
<result name="success">/primer/success.jsp</result>
</action>
</package>
</struts>
web.xml:
<?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核心,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>
<welcome-file-list>
<welcome-file>primer/test.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<!-- 获取绝对路径 -->
绝对路径:${pageContext.request.contextPath}
入门的路径:<br>
<!-- action路径:绝对路径/命名空间/action名.action -->
入门的路径:<br>
<br>
<a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>
</body>
</html>
primer目录下success.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
primer文件夹下 helloworld成功!!!!<br>
</body>
</html>