struts

129

运行流程

客户端浏览器通过HTTP请求,访问控制器,然后控制器读取配置文件,然后执行服务器端跳转,执行相应的业务逻辑,然后,在调用模型层,取得的结果展示给jsp页面,最后返回给客户端浏览器

组成部分 struts
视图 标签库
控制器 action
模型层 ActionFrom JavaBean

struts

maven 安装 官网 : struts.apache.org/

  1. idea新建web项目
  2. 接着如下依赖 网址 search.maven.org/artifact/st…
<dependencies>
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.20</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

此时将会自动处理好依赖

一直采用的是直接打包好war包的方式的

编写配置文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
      org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

创建拦截器,拦截所有请求.交给struts控制器执行

编写struts控制文件

<?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>
 
</struts>

此时

此时项目目录结构如下

2019-03-23-18-11-04----

创建action类,控制器类

创建控制器类,完成页面的信息的传递

package com.ming;

public class HelloWorldAction {
    private String name;

    public String execute() throws Exception {
        return "success";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

此时,定义私有String类型的name,定义set,get方法,当执行的时候,调用execute方法.

此为控制器,起到连接两者的视图层,和模型层之间的关系.

创建视图层

定义页面提交视图层

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="hello">
    <input type="text" name="name"/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

此时,定义表单.提交内容,将会发送到hello控制里

定义数据接收层

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
Hello World, <s:property value="name"/>
</body>
</html>

再次编写配置文件

再次编写配置文件,两者联合起来

<?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>
    <!-- 定义调试 -->
    <constant name="struts.devMode" value="true" />
    <!-- 定义数据包 -->
    <package name="helloworld" extends="struts-default">
        <!-- 定义处理逻辑 name为指定处理的名称 class 处理的包文件 method 处理将会调用的方法-->
        <action name="hello"
                class="com.ming.HelloWorldAction"
                method="execute">
            <!-- 成功返回页面 -->
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

运行效果

2019-03-23-18-18-45----

2019-03-23-18-18-53----

最后

目前 jsp已经基本废弃 所以标签库已经基本没人用了. struts起的作用,更多的是控制器的作用,请求送给spring