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>

二.配置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>/*</url-pattern>  <!-- /*:所有的页面都先经过过滤器 -->
  </filter-mapping>

  <!-- welcome-file-list是一个配置在web.xml中的一个欢迎页,
  用于当用户在url中输入项目名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

三.创建核心配置文件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>



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) {

    }

}

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>

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>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值