Struts

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

内容简介:客户端浏览器通过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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Rails 5敏捷开发

Rails 5敏捷开发

[美] Sam Ruby、[美] Dave Thomas、[美] David Heinemeier Hansson / 安道、叶炜、大疆Ruby技术团队 / 华中科技大学出版社 / 2017-12-30 / 115.00

本书以讲解“购书网站”案例为主线,逐步介绍Rails的内置功能。全书分为3部分,第一部分介绍Rails的安装、应用程序验证、Rails框架的体系结构,以及Ruby语言知识;第二部分用迭代方式构建应用程序,然后依据敏捷开发模式开展测试,最后用Capistrano完成部署;第三部分补充日常实用的开发知识。本书既有直观的示例,又有深入的分析,同时涵盖了Web开发各方面的知识,堪称一部内容全面而又深入浅出......一起来看看 《Rails 5敏捷开发》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

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

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换