• idea快速搭建struts2框架


    一.用maven创建一个javaweb项目:
    在这里插入图片描述
    pom.xml内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      Licensed to the Apache Software Foundation (ASF) under one
      or more contributor license agreements.  See the NOTICE file
      distributed with this work for additional information
      regarding copyright ownership.  The ASF licenses this file
      to you under the Apache License, Version 2.0 (the
      "License"); you may not use this file except in compliance
      with the License.  You may obtain a copy of the License at
    
       http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing,
      software distributed under the License is distributed on an
      "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      KIND, either express or implied.  See the License for the
      specific language governing permissions and limitations
      under the License.
    -->
    <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
      <packaging>war</packaging>
    
      <name>struts2demo</name>
      <groupId>org.example</groupId>
      <artifactId>struts2demo</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <properties>
        <maven.compiler.source>7</maven.compiler.source>
        <maven.compiler.target>7</maven.compiler.target>
      </properties>
      <build>
      <plugins>
      <!--tomcat 的插件 (相当于在maven内部放置了个tomcat服务器)-->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
      </plugin>
      </plugins>
      </build>
    
      <dependencies>
      <!-- struts2-core -->
      <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.33</version>
      </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    二.配置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>
    
      <!-- struts2的过滤器 -->
      <filter>
        <filter-name>struts2</filter-name>  <!-- struts.xml -->
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*  
      
    
      
      
        index.jsp
      
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    三.创建核心配置文件struts.xml
    在这里插入图片描述
    四.右键目录resources—>new—>XML Configuration File—>Struts Config,命名为struts,生成struts.xml文件
    五.例子:hello struts2
    访问路径:http://localhost:8080/test
    struts.xml文件:

    <?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>
        <!-- struts2是基于package管理每一个action配置
            package用来管理众多的action配置
            name:包名,随便起
            extends:继承  默认写成Struts-default
        -->
        <package name="default" extends="struts-default" >
            <!--管理TestAction
                action:作用:用来配置项目中的一个个action
                    name:当前action访问的路径
                    class:当前管理action的全类名
            -->
            <action name="test" class="com.slp.action.HelloAction">
                <result name="OK">index.jsp</result>
            </action>
        </package>
    </struts>
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    java下面建一个action类:
    在这里插入图片描述

    public class HelloAction implements Action {
        //execute()专门处理请求的方法
        public String execute() throws Exception {
            System.out.println("hello struts2~~~struts构建完成");
    //        return SUCCESS;  //返回的结果可以是:Action接口内部常量
            return "OK";   //返回的结果也可以是自定义的字符串
        }
        @Override
        public Object getValue(String s) {
            return null;
        }
        @Override
        public void putValue(String s, Object o) {
    
        }
    
        @Override
        public void setEnabled(boolean b) {
    
        }
    
        @Override
        public boolean isEnabled() {
            return false;
        }
    
        @Override
        public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
    
        }
    
        @Override
        public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
    
        }
    
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>index</title>
    </head>
    <body>
    <h1>
        <a href="HelloWorld.jsp">点击前往测试页面</a>
    </h1>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    HelloWorld.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>hello</title>
    </head>
    <body>
    <div align="center">
        <h1>HelloStruts2</h1>
    </div>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    参考例子:
    【Struts2】一_idea快速搭建struts2框架
    【IDEA版】简单快速上手撸Struts框架

  • 相关阅读:
    vue 使用 js 监听监听键盘按钮事件并阻止按键默认事件
    Win11如何删除升级包?Win11删除升级包的方法
    6.1_3 Python3.x入门 P3 【基础】流程语句【循环结构】
    Rainbond的 Gateway API 插件制作实践
    Java 基础复习 Day 21
    Vue 和 React 比,React 好在哪里?
    <C++>多态之纯虚函数与抽象类,学习不一样的析构函数
    区间预测 | Matlab实现QRCNN-BiGRU-Attention分位数回归卷积双向门控循环单元注意力机制时序区间预测
    ADS-NPU芯片架构设计的五大挑战
    小程序AI智能名片S2B2C商城:AIGC系统赋能多元化应用场景新探索
  • 原文地址:https://blog.csdn.net/s123l4/article/details/128033763