maven整合struts2

一.maven整合struts

  • 环境
  • tomcat8
  • JDK8(最好jdk版本和tomcat版本一致,有时候版本不一致会出现很多兼容性的错误)
  • 在setting.xml中写入如下代码,默认maven是使用jdk版本为1.8
 <profile>
            <id>jdk18</id>
            <activation>
                <jdk>1.8</jdk>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
1.步骤(我们以eclipse为例)
  • 创建一个maven web项目

  • 当项目文件不存在web.xml,通过工具生成web.xml,如下图所示
    在这里插入图片描述

  • 添加struts2依赖(在pom.xml中加入如下代码,maven会自动下载对应的jar包到项目下来)

 
   <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24.1</version><!--struts对应的版本-->
        </dependency>
    </dependencies>

在这里插入图片描述

  • 创建struts.xml(配置文件都要创建在src/main/resources下)
    在这里插入图片描述
<?xml version="1.0" encoding="UTF-8" ?>
 
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
 
<struts>
       <package name="demo" namespace="/" extends="struts-default">
                <action name="customerAction_*" class="it.playmaker.action.customerAction" method="{1}">
                 <result name="success">/index.jsp</result>
                </action>
       
       </package>
    
 
</struts>
  • 创建action类
public class customerAction extends ActionSupport {

	public String save() throws Exception{
		System.out.println("CustomerAction的save方法被调用了");
		return SUCCESS;
	}
}

  • 在web.xml中配置struct2框架核心过滤器

  <!-- 配置struct2核心过滤器 -->
  <filter>	
         <filter-name>struts</filter-name>
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         <!--使用快捷键Ctrl+Shift+T来查找-->
  </filter>
  
  <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>
  • 创建响应的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>Insert title here</title>
</head>
<body>
          <h1>我是通过Action来到这个世界的</h1>
</body>
</html>
  • 运行

在这里插入图片描述

二.Maven概念模型

1.两个核心
  • 依赖管理:对jar包管理
  • 项目构建:通过命令进行项目构建
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值