struts2 CRUD 入门 配置

时间:2023-03-09 23:12:04
struts2 CRUD 入门 配置

本文介绍struts2在eclipse下的配置,实现一个具有CRUD功能的图书管理系统。

1         开发环境配置

1.1           在Eclipse中配置Struts2

1.1.1 新建struts.xml 文件,并写入如图信息。

struts2 CRUD 入门 配置

struts2 CRUD 入门 配置

1.1.2 在web.xml 文件中配置struts2

struts2 CRUD 入门 配置

2.1.3 在WEB-INF 目录下导入如下的struts2 相关的jar 包

struts2 CRUD 入门 配置

1.2           在Eclipse中配置MySQL

如下图,继续在WEB-INF 目录下导入Java 连接MySQL 的驱动包。

  struts2 CRUD 入门 配置

1.3           在Eclipse中配置Tomcat

2.3.1 新建Tomcat

struts2 CRUD 入门 配置

struts2 CRUD 入门 配置

2.3.2 新建Dynamic Web Project,并关联到Tomcat 上

struts2 CRUD 入门 配置

2         图书SaaS设计

2.1       Web.xml

源代码(XML)

struts2 CRUD 入门 配置

该部分web.xml 可从struts2.3 的struts2-blank 项目中拷贝,省去开头烦人的手打。具体需要修改的部分是welcome-file 里设置的首页。

2.2       Struts.xml

struts2 CRUD 入门 配置

struts2 CRUD 入门 配置

2.3       Action类

Action类名

作用

操作列表

操作说明

BooksAction

处理所有action

index

返回首页

Query

执行查询并返回查询结果

Delete

删除图书

Show

展示图书信息

popUpdate

跳转到更新图书页面

Update

处理并更新图书

popCreate

弹出创建图书页面

Create

处理并创建图书

CreateAuthor

处理并创建作者

2.4       辅助类

类名

作用

操作列表

操作说明

Book

图书数据模型

Getters、Setters

Struts 自动创建

Author

作者数据模型

Getters、Setters

Struts 自动创建

Dao

数据库连接与访问

Dao

创建与数据库的连接

Close

断开与数据库的连接

Execute

执行数据库的表插入

ExecuteQuery

执行数据库的表查询

ExecuteUpdate

执行数据库的表更新、删除

2.5       JSP页面

序号

页面名

作用

页面核心元素(form)

Form对应的action name

Form中提交的数据项

1

Index.jsp

首页,查询与创建图书

Form1

PopCreate

Form2

Query

Author.name

2

Result.jsp

展示某作者所有图书,查询、更新、删除与创建图书

Form1

PopCreate

Form2

Query

Author.name

A1

Show

ISBN

A2

Delete

ISBN, author.name

A3

Update

ISBN, title

3

Create.jsp

提供创建图书信息

Form

Create

ISBN, title,

authorID,

publisher,

publishDate

price

4

Show.jsp

展示图书信息

5

Update.jsp

提供更新图书信息

Form

Update

ISBN, title,

authorID,

publisher,

publishDate

price

6

CreateAuthor.jsp

提供创建新作者的信息

Form

CreateAuthor

authorID, name,

country, age,

ISBN, title,

authorID,

publisher,

publishDate

price

7

createSUCCESS.jsp

提示创建成功,关闭或返回主页

index

8

createFAIL.jsp

提示创建图书失败,返回创建页面

popCreate

9

Error.jsp

提示错误,跳转回主页

A

index

2.6       各Action/JSP之间的调用和消息传递关系

给出一张图示描述彼此之间的数据传递和调用关系。

struts2 CRUD 入门 配置

3         图书SaaS核心代码

针对下列功能,分别给出ActionJSP页面中的核心代码。

3.1       按作者查询

struts2 CRUD 入门 配置

图1-1查询入口

struts2 CRUD 入门 配置

图1-2查询action

struts2 CRUD 入门 配置

图-1-3 查询返回界面

3.2       展示图书详细信息

struts2 CRUD 入门 配置

图2-1 图书展示入口

struts2 CRUD 入门 配置

图2-2 图书展示action

struts2 CRUD 入门 配置

图2-3 图书展示返回页面

3.3       删除图书

struts2 CRUD 入门 配置

图3-1 删除图书入口界面

struts2 CRUD 入门 配置

图3-2 删除图书action

struts2 CRUD 入门 配置

图3-3 删除图书返回页面

3.4       新增图书/作者

struts2 CRUD 入门 配置

图4-1 新增图书入口界面

struts2 CRUD 入门 配置

图4-2 新增图书弹出页面action

struts2 CRUD 入门 配置

图4-3 新增图书界面

struts2 CRUD 入门 配置

图4-4 新增图书action

struts2 CRUD 入门 配置

图4-5 新增作者入口页面

struts2 CRUD 入门 配置

图4-6 新增作者action

3.5       更新图书信息(可选)

struts2 CRUD 入门 配置

图5-1 更新图书入口界面

struts2 CRUD 入门 配置

图5-2 更新图书action-Part I

struts2 CRUD 入门 配置

图5-3 更新图书action-Part II

3.6       数据库连接与访问

struts2 CRUD 入门 配置

图6-1 数据库连接与操作Dao类

struts2 CRUD 入门 配置

图6-2 数据库的执行操作