Maven项目整合Struts2框架
-------------------------siwuxie095
Maven 项目整合 Struts2 框架
1、先创建
Maven 项目:maven-struts2
注意:要勾选跳过原型选项
或
跳过骨架选项
2、创建完成,项目目录结构一览:
3、
在
pom.xml 中添加 struts2-core 的依赖
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.33</version> </dependency> |
4、不难发现,除了手动添加的依赖
struts2-core,
struts2-core 的依赖也自动添加进来了
5、在
src/main/java 目录下创建 Action 类
UserAction.java:
package com.siwuxie095.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { public String add() { System.out.println("----- add -----"); return }
} |
6、在
src/main/resources 目录下创建 Struts2 核心配置文件
struts.xml:
<?xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package
<action <result </action>
</package> </struts> |
7、在
src/main/webapp 目录下创建 JSP 页面
index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <html> <head> <meta <title>首页</title> </head> <body> <h1>添加成功!</h1> </body> </html> |
8、此时,index.jsp 无法编译通过,向 pom.xml 中添加依赖
jsp-api
<!-- 创建的 JSP 文件需要 jsp-api 依赖才能编译 通过,但因为 Tomcat 中已经有了该 jar 包, 所以将依赖范围设置为 provided
同时,servlet-api 是 jsp-api 的依赖,将 自动添加进来
其实,这里只添加 servlet-api 也是可以编译 通过的
综上,共有三种解决方法: (1)手动添加 jsp-api 依赖,servlet-api 因 为是 jsp-api 的依赖,也将自动添加进来 (2)手动添加 servlet-api 依赖 (3)手动添加 jsp-api 和 servlet-api 依赖
--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> |
9、综上,该
Maven 项目的核心配置文件如下:
pom.xml:
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 当前 Maven 项目的坐标信息 --> <groupId>com.siwuxie095.maven</groupId> <artifactId>maven-struts2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
<dependencies>
<!-- 只需要添加 struts2-core 一个依赖, struts2-core 的依赖就会自动添加 进来 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.33</version> </dependency>
<!-- 创建的 JSP 文件需要 jsp-api 依赖才能编译 通过,但因为 Tomcat 中已经有了该 jar 包, 所以将依赖范围设置为 provided
同时,servlet-api 是 jsp-api 的依赖,将 自动添加进来
其实,这里只添加 servlet-api 也是可以编译 通过的
综上,共有三种解决方法: (1)手动添加 jsp-api 依赖,servlet-api 因 为是 jsp-api 的依赖,也将自动添加进来 (2)手动添加 servlet-api 依赖 (3)手动添加 jsp-api 和 servlet-api 依赖
--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency>
</dependencies>
</project> |
10、最后,别忘了在部署描述文件中配置 Struts2 核心过滤器
web.xml:
<?xml <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<!-- 配置 Struts2 核心过滤器 --> <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> |
11、访问路径
http://localhost:8080/maven-struts2/user_add.action
【made by siwuxie095】
最新文章
- ASP.NET Core 中文文档 第四章 MVC(3.9)视图组件
- Python(八)进程、线程、协程篇
- C语言之函数可变参数
- 原生javascript加载运行
- Webpack+React+ES6入门指南[转]
- requirejs自己的学习
- saltstack之(二)软件包下载安装
- hql语句理解2
- 64位系统中开启32位应用,特别是OLEDB
- SQL SERVER的检查点checkpoint
- Spring mybatis源码篇章-MybatisDAO文件解析(二)
- [SinGuLaRiTy] NOIP互测模拟赛
- k8s常用命令演示
- session和cookie的区别是什么,他们都是什么.
- Mysql sql 功能分类
- 异常处理与MiniDump 用于投放市场c++异常捕获
- CentOS6.8 配置LVM
- protocol error, got &#39;n&#39; as reply type byte + redis如何后台启动
- NotifyIcon实现托盘程序
- Http协议中Cookie详细介绍(转)
热门文章
- [转]NuGet 包升级
- ni_set()函数的使用 以及 post_max_size,upload_max_filesize的修改方法
- 同一个IP段ping不通同事的电脑
- 触屏设备上的多点触碰检测C++代码实现
- Protocol Buffer Basics: Python
- 处理存在UNASSIGNED的主分片导致Elasticsearch集群状态为Red的问题
- 20180130之PYTHON学习笔记【PYTHON3写个自动听课功能】
- BP神经网络的公式推导
- jsp 传多个值给后端
- centos7.3下curl支持https协议