Struts

栏目: Struts · 发布时间: 7年前

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

运行流程

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

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

struts

maven 安装

官网 : https://struts.apache.org/

  1. idea新建web项目
  2. 接着如下依赖 网址 https://search.maven.org/artifact/struts/struts/1.2.9/jar
<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>

此时

此时项目目录结构如下

Struts

创建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>

运行效果

Struts

Struts

最后

目前 jsp已经基本废弃 所以标签库已经基本没人用了.

struts起的作用,更多的是控制器的作用,请求送给spring


以上所述就是小编给大家介绍的《Struts》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据化运营速成手册

数据化运营速成手册

胡晨川 / 电子工业出版社 / 2017-4 / 55

《数据化运营速成手册》用于提升互联网公司员工的数据应用能力,即数据化运营能力。首先,从最常用的数据图表切入,帮助执行层正确地绘图,管理层正确地看图;接着,梳理运营中最基本的数据应用知识,涉及数据获取、数据清洗、数据认知、分析框架、指标体系、运营实验等内容。然后,介绍作者认为必要的统计学知识,包括假设检验、方差分析、回归分析和时间序列分解,并引入了管理科学中的规划求解方法。最后,介绍了数据分析工具的......一起来看看 《数据化运营速成手册》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具