Maven项目整合Struts2框架
来源:互联网 发布:软件工程学什么 编辑:程序博客网 时间:2024/06/06 09:08
-------------------------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 UserActionextends ActionSupport {
public String add() {
System.out.println("----- add -----");
return"add";
}
}
6、在src/main/resources 目录下创建 Struts2 核心配置文件
struts.xml:
<?xmlversion="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>
<packagename="demo"extends="struts-default"namespace="/">
<actionname="user_*"class="com.siwuxie095.action.UserAction"method="{1}">
<resultname="add">/index.jsp</result>
</action>
</package>
</struts>
7、在src/main/webapp 目录下创建 JSP 页面
index.jsp:
<%@ pagelanguage="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>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<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:
<projectxmlns="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/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:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1">
<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】
- Maven项目整合Struts2框架
- Maven项目整合SSM框架
- Maven项目整合SSH框架
- 使用Maven实现Struts2+Hibernate4+Spring3整合SSH框架(一):Maven项目创建
- Struts2+Spring+Hibernate+Maven框架整合
- 使用Maven实现Struts2+Hibernate4+Spring3整合SSH框架(三):将项目上传Github
- maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea
- maven项目,struts2+spring+mybatis框架搭建整合,tomcat部署,开发工具Idea
- ssh框架整合项目struts2、spring、hibernate
- SSM 框架 Maven项目整合实例
- IDEA创建maven项目 整合SSM框架
- SSM框架整合之maven项目
- SSM 框架 Maven项目整合实例
- IDEA创建maven项目 整合SSM框架
- SSH框架整合的MAVEN项目之配置文件整合
- MAVEN构建spring struts2 ibatis框架整合开发 使用包
- 使用maven项目sping和struts2整合pom.xml文件
- struts2 maven整合tiles3
- 和为S的两个数字
- cron Expression(cron表达式)
- 资本,能和您喝杯茶吗
- 安卓7.0上在状态栏上加电池百分比
- windows安装MySQL5.7.19
- Maven项目整合Struts2框架
- 学习笔记-参考博客链接保存
- Python学习笔记 --- 循环遍历时间区间
- insertAdjacentHTML方法详解
- Leetcode之Merge Sorted Array 问题
- 207. Course Schedule
- WEB会话跟踪技术
- Kali Linux入门第二课 | 一次简单的渗透笔记
- QCon上海2017,一起探索新技术的奥秘