idea+tomcat+struts2 搭建一个web实例

本文详细介绍了如何使用IntelliJ IDEA搭建Struts2框架的web项目,包括配置struts-default.xml,处理不同版本的注意事项,部署操作,TOMCAT配置,以及文件路径和库文件加载等关键步骤。通过一个具体示例,展示了从创建项目到成功运行的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考这个博客搭建:

Struts2快速入门与使用IntelliJ IDEA创建第一个web项目


需要注意几点:

1)可能会遇到struts-default高亮变红的情况,其实是没有把struts-default.xml加入到Default File Set

解决办法:


2)注意struts的版本,2.3有ng,2.5没有



3)注意要进行部署操作


4)加载TOMCAT的时候要记得配置Deployment


5)注意各个文件的路径,库文件的加载


在这个实例中,我把namespace="/hello",也就是当执行HelloAction的sayHi这个ACTION方法之后,如果返回success状态,那么又因为在上面配置的namespace=hello,页面跳转时寻找规则为web/namespace/xxx.jsp,也即是:

web/hello/sayHi.jsp

因此我们在浏览器输入http://localhost:8080/hello/sayHi 访问项目


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


struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="sayHi" class="com.southstar.demo.HelloAction" method="sayHi">
            <result name="success">sayHi.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>Struts Blank</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <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>
</web-app>

Action

package com.southstar.demo;

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

sayHi.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
没鱼,SB
</body>
</html>

至此,一个完整的简单struts实例,创建完成!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值