idea搭建struts2框架dome

第一步

在这里插入图片描述

第二步

在这里插入图片描述

第三步

进入项目目录后,在web/WEB-INF下新建lib文件夹,同时将在上一节整理的struts2开发包拷入lib文件,之后,右键lib目录,选择Add as library选项,将开发包配置好。如下:
在这里插入图片描述
jar包下载连接:https://pan.baidu.com/s/1w6NZRVr6LIMpQusVvpw64w
提取码:5034

第四步

打开web/WEB-INF中的web.xml,配置filter,这个就是之前提到过的前端控制器。如下:

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

这里注意:struts2每个版本不同org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
路径地址不一样,具体请根据版本的不同书写正确的拦截器路径地址

第五步

在项目目录src下新建包com.happy.demo,新建HelloAction.java,代码如下:

public class HelloAction {
    public String sayHi() {
        System.out.println("hello world");
        return "success";
    }
}

第六步

打开项目目录下的struts.xml,将刚编写的HelloAction进行配置,如下:

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="sayHi" class="com.happy.demo.HelloAction" method="sayHi">
            <result name="success">sayHi.jsp</result>
        </action>
    </package>
</struts>

如struts-default报红请参考:https://blog.csdn.net/weixin_41060905/article/details/82872737
里面有解决方法

第七步

在上面xml配置中,通过result标签配置了当success时跳转的页面sayHi.jsp,下面我们就来新建这个页面。
又因为在上面配置的namespace=hello,页面跳转时寻找规则为web/namespace/xxx.jsp,所以,在web目录下,新建hello文件夹,并新建sayHi.jsp文件。详细如下:
在这里插入图片描述
sayHi.jsp内容如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>SayHi</h1>
</body>
</html>

通过浏览器访问http://localhost:8080/hello/sayHi,可控制台会打印HelloAction中的hello world,同时页面会跳转到struts.xml中配置的sayHi.jsp页面,页面显示之前写的SayHi文字。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值