用一个简单的Struts2程序来总结Strut2的执行流程

该案例中重要部分已经标记为红色,该案例是我本人测试成功的案列,你可以直接拷贝放到自己工程中导入相应的jar包后运行

相应jar包如下:

一、创建 helloWord.jsp 页面。代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- 导入struts2标签 --%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>Hello Word</title>
</head>
<body>
    <h1>Hello Word</h1>
    <dir>
        <h2><!-- 使用Struts标签输出Action的message -->
        <s:property value="message"/></h2>
    </dir>
    <div>
        <form action="HelloAction" method="post">
        请输入您的姓名:<input type="text" name="name"/>
        <input type="submit" value="提交"/>
        </form>
    </div>
</body>
</html>

helloword.jsp即是表单提交页面,又是结果呈现页面,当用户提交至Action,Action处理完业务之后,再跳转回本页 。<s:property value="message"/>用于输出Action的属性“message”的值,起作用等价于${message}。是struts标签的写法。

二、创建Action类,代码如下

package com.zxf.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{
    //接收表单传输的姓名
    private String name;
    private String message;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    
    /**
     * 接收表单的请求方法,并返回某个视图隐射名
     */
    public String Hello() {
        //根据用户输入的名称,拼接返回信息
        this.setMessage("Hello"+name);
        //跳转到相应的逻辑视图
        return "info";
    }
}
三、创建Struts2配置文件,在src目录下创建struts.xml文件,配置如下:

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

<struts>
    <package name="my_default" namespace="/" extends="struts-default">
        <action name="HelloAction" method="Hello" class="com.zxf.action.HelloAction">
            <result name="info">helloWord.jsp</result>
        </action>
    </package>
</struts>

四、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts_web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

<!-- 下面红色部分为配置部分 -->
  <filter>
      <filter-name>Struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>Struts2</filter-name>
      <url-pattern>/*</url-pattern>
<!-- /*表示一切请求会通过该过滤器 -->
  </filter-mapping>
</web-app>

运行结果:

总结Struts2的执行流程:

1.表单提交请求HelloWord.action.

2.服务器接受请求后,根据web.xml的配置,将请求统一发给Struts2的核心控制器。

3.Struts2的核心控制器根据struts.xml的配置内容,将请求发给HelloWord类,并调用该类下的相应方法。

4.根据调用方法的返回结果,并在struts.xml文件中匹配返回结果,跳转至helloWord.jsp页面

猜你喜欢

转载自blog.csdn.net/Websphere_zxf/article/details/81740218