Maven构建Struts2实例

本文介绍如何使用Maven构建Struts2 Web项目,并通过IntelliJ IDEA进行配置。文章详细展示了pom.xml文件配置、struts.xml配置、web.xml配置及简单的登录示例。

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

怎么构建Maven项目就不多说了。很简单,就直接贴代码了。IntelliJ IDEA 创建Maven 工程

项目目录:

这里写图片描述

pom.xml

可以用Maven的Tomcat7插件,也可以用自己本地的Tomcat。本地的Tomcat在Run>>Edit Configurations中配置。

<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>
  <groupId>com.mistra.ms</groupId>
  <artifactId>MistraRMStruts2</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MistraRMStruts2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.5.2</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>MistraRMStruts2</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <path>/${project.build.finalName}</path>
          <port>8080</port>
          <uriEncoding>UTF-8</uriEncoding>
          <finalName>${project.build.finalName}</finalName>
          <!-- 名称必须与配置文件的id名称一致 -->
          <server>tomcat7</server>
        </configuration>
        <!-- 在打包周期运行tomcat7 -->
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

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>
    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.locale" value="zh_CN"></constant>

    <package name="hurricane" extends="struts-default">
        <action name="loginAction" class="com.mistra.ms.LoginAction" method="execute">
            <result>
                /success.jsp
            </result>
        </action>
    </package>
</struts>
web.xml
<web-app >
  <display-name>Archetype Created Web Application</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>

LoginAction.java
package com.mistra.ms;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by Administrator on 2017/12/28/028.
 */
public class LoginAction extends ActionSupport{
    public String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

}

index.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8"%>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h2>Hello World!</h2>

<form action="loginAction">
    姓名<input type='text' name="Name"><input type="submit" value="提交">
</form>
</body>
</html>

success.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page 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>Welcome</title>
</head>
<body>
Hello <s:property value="Name" />

</body>
</html>

源码GitHub地址

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值