struts

12次阅读

共计 2855 个字符,预计需要花费 8 分钟才能阅读完成。

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

组成部分
struts

视图
标签库

控制器
action

模型层
ActionFrom JavaBean

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

idea 新建 web 项目
接着如下依赖 网址 https://search.maven.org/arti…

<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>
此时
此时项目目录结构如下
创建 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>
运行效果

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

正文完
 0