IntelliJ IDEA2021.2搭建struts2框架

在网上看了很多IDEA搭建struts2的教程,搭建后总是无法启动服务器,下面介绍2021版本如何正确搭建struts2框架。

一、首先去struts2官网下载所需包

官网传送门

下载后进行解压

struts2下载

all是完整包,体积较大;min是官方提供的对于初学者的基础包,大家自行选择,这里我选的是min

二、下载Struts2插件

因为更新到2021版本之后,官方已经取消图中红框的选项了,要实现如图的效果需要下载插件Struts2

官方取消的内容

注意这里的Tomcat版本,一定要和下面的web.xml版本对应,否则可能会出问题,具体可以看我的关于struts2的另一篇文章

记-在用IntelliJ IDEA搭建Struts2框架时遇到的一些问题

从设置里下载插件

Struts2插件下载

三、创建struts2项目

创建项目

创建struts2项目步骤

找到你解压后的包并全选,然后点击OK

选择你解压后的包

然后给项目命名,这里示例命名为demo,然后点击Finish

命名

创建完成后,打开Project Structure选择Facets>Struts 2,点击加减号旁边的小铅笔标签,将struts2-defalut.xml与struts.xml放在同一个文件里

image.png

创建完成后会自动在src和WEB-INF下产生struts.xml和web.xml配置文件,然后我们对其进行修改web.xml内容改为以下代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>

然后进行测试实例

  • 编写login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>struts2小例子</title>
</head>
<body>
<form method="post" action="login.action">
    用户名:<input name="userName" type="text"/><br/>
    密码:<input name="password" type="password"/><br/>
    <input type="submit" value="登录"/>
</form>
</body>
</html>
  • 编写success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>成功页面</title>
</head>
<body>
登录成功
</body>
</html>
  • 编写JavaBean类LoginBean.java
package com.demo.bean;

/**
 * @author 张子
 * @date 2021/10/1 20:09
 */

public class LoginBean {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean login(String userName, String password) {
        if ("qq".equals(userName) && "123".equals(password)) {
            return true;
        }
        return false;
    }
}
  • 编写action类LoginAction.java
package com.demo.action;

/**
 * @author 张子
 * @date 2021/10/1 20:10
 */

import com.demo.bean.LoginBean;

public class LoginAction {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String execute() throws Exception {
        LoginBean l = new LoginBean();
        if (l.login(userName, password)) {
            return "success";
        }
        return "error";
    }
}
  • 配置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>
    <!-- 所有的Action配置都应放在package下,name定义包名,extends定义继承包空间 -->
    <package name="xx" extends="struts-default">
        <!-- Action配置可以有多对;name是对业务控制器命名
        在表单中指定的action的名字需要与该名字一致;class指定Action类的位置 -->
        <action name="login" class="com.demo.action.LoginAction">
            <!-- 定义两个逻辑视图与物理资源之间的映射
            name值是Action中execute()方法返回的结果,即逻辑视图 -->
            <result name="success">/success.jsp</result>
            <result name="error">/login.jsp</result>
        </action>
    </package>
</struts>
  • 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <display-name>demo</display-name>
    <!-- 设置欢迎界面,登录网站时首先展示的页面 -->
    <welcome-file-list>
        <welcome-file>/login.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <!-- 配置struts2核心控制器的名称 -->
        <filter-name>struts</filter-name>
        <filter-class>
            <!-- 这是struts-2.5及以上版本的控制器 -->
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <!-- struts2控制器的名称 -->
        <filter-name>struts</filter-name>
        <!-- 拦截所有的以action结尾的请求,让静态资源可以正常加载 -->
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

</web-app>

如果复制完web.xml的代码后,标签前面报红的话,把注释删掉即可
在这里插入图片描述

运行结果

登录界面

登录成功
我的个人博客:https://zhangz1.top

  • 8
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangz1z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值