本系列教程将详细介绍Struts 1.x
的基本原理和使用方法,读者可以参阅《Struts 2系列教程》
来比较Struts 1.x
和
Struts 2.x
的相同点和不同点。
在这篇文章中将以一个简单的例子(mystruts)
来演示如何使用MyEclipse
来开发、运行Struts
程序,并给出了解决ActionForm
出现乱码问题的方法。读者可以从本文中了解开发Struts 1.x
程序的基本过程。
一、本文给出的程序要实现什么功能
mystruts
是一个录入和查询产品信息的程序。为了方便起见,本例中的产品信息表只包括了产品ID
、产品名称和产品价格三个字段。mystruts
的主要功能如下:
1. 接受用户输入的产品ID
、产品名称和产品价格。
2.
验证这些字段的合法性。如果某些字段的输入不合法(如未输入产品ID
),程序会forward
到一个信息显示页,并显示出错原因。
3.
如果用户输入了正确的字段值,程序会将这些字段值保存到数据库中,并显示“保存成功”信息。
4.
用户输入产品名称,并根据产品名称进行模糊查询。如果存在符合要求的产品信息。程序会以表格形式显示这些产品的信息,同时显示记录数。如果未查到任何记录,会显示“没有符合要求的记录!
”信息。
二、编写程序前的准备工作
1. 建立数据库
在编写程序之前,需要建立一个数据库(
struts
)和一个表
(t_products)
,建立数据库和表的
SQL
脚本如下所示:
# 建立数据库struts
CREATE
DATABASE
IF
NOT
EXISTS
struts
DEFAULT
CHARACTER
SET
GBK;
# 建立表t_products
CREATE
TABLE
IF
NOT
EXISTS
struts.t_products (
product_id
varchar
(
4
)
NOT
NULL
,
product_name
varchar
(
50
)
NOT
NULL
,
price
float
NOT
NULL
,
PRIMARY
KEY
(product_id)
) ENGINE
=
InnoDB
DEFAULT
CHARSET
=
gbk;
2
建立一个支持
struts1.x
的
samples
工程
用MyEclipse
建立一个samples
工程(Web
工程),现在这个samples
工程还不支持Struts1.x
(没有引入相应的Struts jar
包、struts-config.xml
文件以及其他和Struts
相关的配置)。然而,在MyEclipse
中这一切并不需要我们手工去加入。而只需要使用MyEclipse
的【New Struts Capabilities
】对话框就可以自动完成这些工作。
首先选中samples
工程,然后在右键菜单中选择【MyEclipse
】 >
【New Struts Capabilities
】,启动【New Struts Capabilities
】对话框。对默认的设置需要进行如下的改动:
(1)将Struts specification改为Struts 1.2。
(2)
将
Base package for new classes
改为struts
。
(3)
将
Default application resources
改为struts.ApplicationResources
。
改完后的【New Struts Capabilities
】对话框如图1
所示。
图1
在设置完后,点击Finish
按钮关闭对话框。在向samples
工程添加支持Struts
的功能后,主要对samples
工程进行了三个操作。
(1
)引入了Struts 1.2
的jar
包(在samples
的工程树中多了一个Struts 1.2 Libraries
节点)。
(2
)在WEB-INF
目录中添加了一个struts-config.xml
文件。文件的默认内容如下面的代码所示:
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd"
>
<
struts-config
>
<
data-sources
/>
<
form-beans
/>
<
global-exceptions
/>
<
global-forwards
/>
<
action-mappings
/>
<
message-resources
parameter
="struts.ApplicationResources"
/>
</
struts-config
>
(3
)在WEB-INF
中的web.xml
文件中添加了处理Struts
动作的ActionServlet
的配置,代码如下:
<
servlet
>
<
servlet-name
>
action
</
servlet-name
>
<
servlet-class
>
org.apache.struts.action.ActionServlet
</
servlet-class
>
<
init-param
>
<
param-name
>
config
</
param-name
>
<
param-value
>
/WEB-INF/struts-config.xml
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
debug
</
param-name
>
<
param-value
>
3
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
detail
</
param-name
>
<
param-value
>
3
</
param-value
>
</
init-param
>
<
load-on-startup
>
0
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
action
</
servlet-name
>
<
url-pattern
>
*.do
</
url-pattern
>
</
servlet-mapping
>
到目前为止,samples
工程已经完全支持Struts
了。读者可以看到,如果不使用MyEclipse
,那么上面所列出的配置文件的内容都必须手工输入。因此,使用MyEclipse
来开发Struts
程序可以省去很多配置xml
文件的工作。
三、实现程序的首页
(index.jsp)
首先在<samples
工程目录>
中建立一个mystruts
目录,然后在<samples
工程目录>" mystruts
目录中建立一个index.jsp
文件,这个文件的内容如下。
<
%@ page
pageEncoding
="GBK"
%
>
<
%--
引用Struts tag--%
>
<
%@ taglib
uri
="http://struts.apache.org/tags-html"
prefix
="html"
%
>
<
html
>
<
head
>
<
title
>
主界面
</
title
>
</
head
>
<
body
>
<
table
align
="center"
cellpadding
="10"
width
="100%"
>
<
tr
>
<
td
align
="right"
width
="50%"
>
<
%--
使用Struts tag--%
>
<
html:link
forward
="newProduct"
>
录入产品信息
</
html:link
>
</
td
>
<
td
>
<
html:link
forward
="searchProduct"
>
查询产品信息
</
html:link
>
</
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
在MyEclipse
中启动Tomcat
(如果Tomcat
处于启动状态,在修改完配置文件后,建议在MyEclipse
的Servers
页重新发布samples
工程,以使修改生效)。在IE
中输入如下的URL
:
http://localhost:8080/samples/mystruts/index.jsp
我们发现在输入上面的URL
后,在IE
中并未显示正确的运行结果,而是抛出了如下的异常:
java.net.MalformedURLException: Cannot retrieve ActionForward named newProduct
这个异常表明程序并未找到一个叫newProduct
的forward
(forward
将在后面详细地讲述)。因此,可以断定,在JSP
中使用forward
时,这个forward
必须存在。下面我们来添加index.jsp
页面中所使用的两个forward
:newProduct
和searchProduct
。这两个forward
分别引向了建立产品信息的页面(newProduct.jsp)
和查询产品信息的页面(searchProduct.jsp)
。我们可以在struts-config.xml
文件中<struts-config>
节点中添加两个全局的forward
,代码如下:
<
global-forwards
>
<
forward
name
="newProduct"
path
="/mystruts/newProduct.jsp"
/>
<
forward
name
="searchProduct"
path
="/mystruts/searchProduct.jsp"
/>
</
global-forwards
>
上面的代码中所示的newProduct.jsp
和searchProduct.jsp
目前并不存在(将在以后实现这两个JSP
页面),现在重新输入上述的URL
,会得到如图2
所示的效果。
图2
如果想让index.jsp
成为默认的JSP
页面,可以在web.xml
中的<welcome-file-list>
节点中加入如下的内容:
<
welcome-file
>
index.jsp
</
welcome-file
>
这时在IE
中只要输入如下的URL
就可以访问index.jsp
页面了。
http://localhost:8080/samples/mystruts
四、实现添加和查询产品信息页面
在本节中主要实现了用于输入产品信息(newProduct.jsp
)和查询产品信息(searchProduct.jsp)
的JSP
页面。
在newProduct.jsp
页面中有一个form
,在form
中含有三个文本框,用于分别输入产品ID
、产品名称和产品价格。
在<samples
工程目录>"mystruts
目录中建立一个newProduct.jsp
文件,代码如下:
<%
@ page pageEncoding
=
"
GBK
"
%>
<%
@ taglib uri
=
"
http://struts.apache.org/tags-html
"
prefix
=
"
html
"
%>
<
html
>
<
head
>
<
title
>
录入产品信息
</
title
>
</
head
>
<
body
>
<%
--
向saveProduct动作提交产品信息
--
%>
<
html:form
action
="saveProduct"
>
<
table
width
="100%"
>
<
tr
>
<
td
align
="center"
>
产品编号:
<
html:text
property
="productID"
maxlength
="4"
/>
<
p
>
产品名称:
<
html:text
property
="productName"
/>
<
p
>
产品价格:
<
html:text
property
="price"
/>
</
td
>
</
tr
>
<
tr
>
<
td
align
="center"
>
<
br
>
<
html:submit
value
=" 保存 "
/>
</
td
>
</
tr
>
</
table
>
</
html:form
>
</
body
>
</
html
>
在
searchProduct.jsp
页面中有一个
form
,为了方便起见,在
form
中只提供了一个文本框用于对产品名称进行模糊查询。在
<samples
工程目录
>" mystruts
目录中建立一个
searchProduct.jsp
文件,代码如下:
<%
@ page pageEncoding
=
"
GBK
"
%>
<%
@ taglib uri
=
"
http://struts.apache.org/tags-html
"
prefix
=
"
html
"
%>
<
html
>
<
head
>
<
title
>
查询产品信息
</
title
>
</
head
>
<
body
>
<%
--
向searchProduct动作提交查询请求
--
%>
<
html:form
action
="searchProduct"
>
<
table
width
="100%"
>
<
tr
>
<
td
align
="center"
>
产品名称:
<
html:text
property
="productName"
/>
</
td
>
</
tr
>
<
tr
>
<
td
align
="center"
>
<
br
>
<
html:submit
value
=" 查询 "
/>
</
td
>
</
tr
>
</
table
>
</
html:form
>
</
body
>
</
html
>
现在启动Tomcat
,并使用如下两个URL
来访问newProduct.jsp
和searchProduct.jsp
:
http://localhost:8080/samples/mystruts/newProduct.jsp
http://localhost:8080/samples/mystruts/searchProduct.jsp
在IE
中输入上面的两个URL
后,并不能显示出相应的界面,而会抛出
JspException
异常,表明未找到
saveProduct
和
searchProduct
动作。从这一点可以看出,如果在
JSP
中使用
Struts Action
,这些
Action
必须事先在
struts-config.xml
文件中定义,否则,
JSP
程序就无法正常访问。在这两个页面所使用的动作(
saveProduct
和
searchProduct
)将会在下面的部分介绍。