Struts2 的相关知识梳理

作者: 右耳菌 | 来源:发表于2022-05-08 00:04 被阅读0次
1. servlet、struts1.x和struts2.x的关系说明

共同点

  • 都是用来作为MVC模型中的Control层;
  • 主要功能在于交互式地浏览和修改数据,生成动态Web内容

不同点

  • struts1.x需要继承Action类,和servlet的耦合度很高,甚至可以看成就是个servlet的封装,而struts2的就是一个javabean,实现是基于拦截器。
  • servlet和struts1均是单实例、多线程,线程不安全,struts2本身就是多实例、单线程的,所以可以说本身就是线程安全的。
2. Struts 2.x的知识脉络梳理
  • Struts2.X的知识脉络


    Struts2.X的知识脉络
  • Struts的完整脑图

Struts的完整脑图
3. 例子
  • Struts2入门案例流程梳理


    Struts2入门案例
  1. 创建Maven的web项目,并且配置好tomcat,测试可以正常启动访问


    创建Maven的web项目
  2. 引入struts2的maven依赖
    可以访问 https://mvnrepository.com/ 进行查询

<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.30</version>
</dependency>
  1. 在web.xml里配置拦截器,即struts2入口
<!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>
  1. 在java包下新建action包,然后构建HelloAction类,实现Action接口,重写execute方法,书写相关的业务逻辑
package action;

import com.opensymphony.xwork2.Action;

/**
 * @Author: Neco
 * @Description:
 * 请求处理的类,Action里面已经预定义了一些处理请求常用的响应
 * String SUCCESS = "success"; // 处理请求成功
 * String NONE = "none"; // 没有找到请求资源
 * String ERROR = "error"; // 发现异常
 * String INPUT = "input"; // 当你输入有错,比如说用户名密码为空,或者是长度不正确之类的
 * String LOGIN = "login"; // 当你需要登录的时候返回login
 * @Date: create in 2022/5/8 10:17
 */
public class HelloAction implements Action {
    @Override
    public String execute() throws Exception {
        System.out.println("Hello World!!!");
        return SUCCESS;
    }
}
  1. 在resource下新建struts.xml文件,并且在里面构建package标签,然后在里面书写action标签建立请求名和类的关联关系
  1. 在官网获取模板
<?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="basicstruts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
     
        <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>
  1. 修改以适配本例子
<?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的热部署-->
    <constant name="struts.devMode" value="true" />

    <package name="default" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>

        <action name="hello" class="action.HelloAction">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>
  1. 创建HelloWorld.jsp
<%--
  Created by IntelliJ IDEA.
  User: lazyf
  Date: 2022/5/8
  Time: 10:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <h1>Hello World From Struts!!!</h1>
</body>
</html>
  1. 运行并测试访问 http://localhost:8080/struts/hello
http://localhost:8080/struts/hello
  1. Struts 后台接收参数或者返回数据的例子
  1. 创建实体类 MessageStore
package action.entity;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/5/8 10:50
 */
public class MessageStore {

    private String message;

    public MessageStore() {
        message = "Hello Struts User";
    }

    public String getMessage() {
        return message;
    }

}

  1. 创建User实体类
package action.entity;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/5/8 10:52
 */
public class User {

    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

  1. 修改HelloAction
package action;

import action.entity.MessageStore;
import action.entity.User;
import com.opensymphony.xwork2.Action;

/**
 * @Author: Neco
 * @Description:
 * 请求处理的类,Action里面已经预定义了一些处理请求常用的响应
 * String SUCCESS = "success"; // 处理请求成功
 * String NONE = "none"; // 没有找到请求资源
 * String ERROR = "error"; // 发现异常
 * String INPUT = "input"; // 当你输入有错,比如说用户名密码为空,或者是长度不正确之类的
 * String LOGIN = "login"; // 当你需要登录的时候返回login
 * @Date: create in 2022/5/8 10:17
 */
public class HelloAction implements Action {

    private User user;

    private MessageStore messageStore;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public MessageStore getMessageStore() {
        return messageStore;
    }

    @Override
    public String execute() throws Exception {
       System.out.println("Hello World!!!" + "   User:" + user);
       messageStore = new MessageStore();
       return SUCCESS;
    }

}

  1. 修改HelloWorld.jsp
<%--
  Created by IntelliJ IDEA.
  User: lazyf
  Date: 2022/5/8
  Time: 10:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" > %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <h1>Hello World From Struts!!!</h1>
    <h2><s:property value="messageStore.message" /></h2>
    <h2><s:property value="user.name" /></h2>
</body>
</html>

  1. 运行并测试
    访问 http://localhost:8080/struts/hello?user.name=Neco
    http://localhost:8080/struts/hello?user.name=Neco

如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~

相关文章

网友评论

    本文标题:Struts2 的相关知识梳理

    本文链接:https://www.haomeiwen.com/subject/ypgturtx.html