JavaEE实验7Word文档格式.docx
- 文档编号:5935025
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:14
- 大小:125.02KB
JavaEE实验7Word文档格式.docx
《JavaEE实验7Word文档格式.docx》由会员分享,可在线阅读,更多相关《JavaEE实验7Word文档格式.docx(14页珍藏版)》请在冰点文库上搜索。
a19.jpg'
0),
(2,2,'
书2'
22,'
a15.jpg'
1),
(3,2,'
书3'
33,'
(5,2,'
书5'
55,'
(6,2,'
书6'
66,'
(7,2,'
书7'
77,'
(8,2,'
书8'
88,'
(9,2,'
书9'
99,'
(10,2,'
书10'
10,'
(11,2,'
书11'
(12,2,'
书12'
12,'
(13,2,'
书13'
13,'
(14,2,'
书14'
14,'
1);
--表的结构`catalog`
CREATETABLE`catalog`(
`catalogid`int(11)NOTNULLauto_increment,
`catalogname`varchar(20)NOTNULL,
PRIMARYKEY(`catalogid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=3;
--导出表中的数据`catalog`
INSERTINTO`catalog`(`catalogid`,`catalogname`)VALUES(1,'
类别1'
),(2,'
类别2'
);
--表的结构`orderitem`
CREATETABLE`orderitem`(
`orderitemid`int(11)NOTNULLauto_increment,
`orderid`int(11)defaultNULL,
`bookid`int(11)defaultNULL,
`quantity`int(11)NOTNULL,
PRIMARYKEY(`orderitemid`),
KEY`FK_Relationship_2`(`orderid`),
KEY`FK_Relationship_4`(`bookid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=1;
--导出表中的数据`orderitem`
--表的结构`orders`
CREATETABLE`orders`(
`orderid`int(11)NOTNULLauto_increment,
`userid`int(11)defaultNULL,
`orderdate`timestampNOTNULLdefaultCURRENT_TIMESTAMPonupdateCURRENT_TIMESTAMP,
PRIMARYKEY(`orderid`),
KEY`FK_Relationship_1`(`userid`)
--导出表中的数据`orders`
--表的结构`user`
CREATETABLE`user`(
`userid`int(11)NOTNULLauto_increment,
`username`varchar(20)NOTNULL,
`password`varchar(20)NOTNULL,
`sex`varchar(4)defaultNULL,
`age`int(11)defaultNULL,
PRIMARYKEY(`userid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=2;
--导出表中的数据`user`
INSERTINTO`user`(`userid`,`username`,`password`,`sex`,`age`)VALUES
(1,'
admin'
'
NULL,NULL)
3、编写功能代码
第一步:
实现数据层的对象关系映射。
(1)编写可保存图书信息的可持久化对象:
org/apex/bookstore/vo/Book.java
packageorg.apex.bookstore.vo;
importjava.util.HashSet;
importjava.util.Set;
/**
*Bookentity.
*
*@authorMyEclipsePersistenceTools
*/
publicclassBookimplementsjava.io.Serializable{
//Fields
privateIntegerbookid;
privateCatalogcatalog;
privateStringbookname;
privateIntegerprice;
privateStringpicture;
//Constructors
/**defaultconstructor*/
publicBook(){
}
/**minimalconstructor*/
publicBook(Stringbookname,Integerprice,Stringpicture){
this.bookname=bookname;
this.price=price;
this.picture=picture;
/**fullconstructor*/
publicBook(Catalogcatalog,Stringbookname,Integerprice,
Stringpicture){
this.catalog=catalog;
//Propertyaccessors
publicIntegergetBookid(){
returnthis.bookid;
publicvoidsetBookid(Integerbookid){
this.bookid=bookid;
publicCataloggetCatalog(){
returnthis.catalog;
publicvoidsetCatalog(Catalogcatalog){
publicStringgetBookname(){
returnthis.bookname;
publicvoidsetBookname(Stringbookname){
publicIntegergetPrice(){
returnthis.price;
publicvoidsetPrice(Integerprice){
publicStringgetPicture(){
returnthis.picture;
publicvoidsetPicture(Stringpicture){
}
(2)编写Hibernate映射文件以实现ORM:
org/apex/bookstore/vo/Book.hbm.xml
<
?
xmlversion="
1.0"
encoding="
utf-8"
>
!
DOCTYPEhibernate-mappingPUBLIC"
-//Hibernate/HibernateMappingDTD3.0//EN"
"
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
hibernate-mapping>
<
classname="
org.apex.bookstore.vo.Book"
table="
book"
idname="
bookid"
type="
java.lang.Integer"
columnname="
/>
generatorclass="
native"
/id>
many-to-onename="
catalog"
class="
org.apex.bookstore.vo.Catalog"
fetch="
select"
lazy="
false"
catalogid"
/many-to-one>
propertyname="
bookname"
java.lang.String"
length="
20"
not-null="
true"
/property>
price"
picture"
30"
/class>
/hibernate-mapping>
(3)在Spring配置文件applicationContext.xml中的sessionFactoryBean配置中合适的位置添加映射文件声明:
value>
org/apex/bookstore/vo/Book.hbm.xml<
/value>
第二步:
实现DAO、Service、Action三层处理代码。
(1)在org/apex/bookstore/dao/impl/BookDAO.java中实现以下方法:
publicListgetNewBook(){
Sessionsession=getSession();
Queryquery=session.createQuery("
fromBookb"
query.setFirstResult(0);
query.setMaxResults(5);
Listbooks=query.list();
session.close();
returnbooks;
(2)在org/apex/bookstore/action/BookAction.java中实现以下方法:
publicStringnewBook()throwsException{
Listbooks=bookService.getNewBook();
Maprequest=(Map)ActionContext.getContext().get("
request"
request.put("
books"
books);
returnSUCCESS;
第三步:
通过applictionContext.xml完成Bean配置。
(1)配置struts.xml,添加对newBookAction的请求处理:
actionname="
newBook"
bookAction"
method="
resultname="
success"
/newBook_succcess.jsp<
/result>
/action>
(2)修改applictionContext.xml内容,实现依赖注入以及ActionBean配置。
a)声明bookDAOBean,该Bean从已声明的baseDAO派生:
beanid="
bookDAO"
class="
org.apex.bookstore.dao.impl.BookDAO"
parent="
baseDAO"
/bean>
b)声明bookServiceBean并注入BookDAO。
bookService"
org.apex.bookstore.service.impl.BookService"
<
refbean="
/>
c)修改bookActionBean配置,注入bookService依赖:
第四步:
实现视图。
(1)将视图newBook_succcess.jsp文件修改为以下内容:
%@pagecontentType="
text/html;
charset=gb2312"
%>
%@taglibprefix="
s"
uri="
/struts-tags"
H1>
SPANclass=green14b>
新书展示<
/SPAN>
/H1>
DIVclass=info_bk1>
s:
iteratorvalue="
#request['
books'
]"
id="
DIVclass=xh5>
imgheight=105width=80src="
/BookStore/picture/<
propertyvalue="
#book.picture"
BR>
#book.bookname"
br>
价格:
#book.price"
元<
formaction="
addToCart.action"
post"
数量:
inputtype="
text"
name="
quantity"
value="
1"
size="
4"
hidden"
#book.bookid"
image"
submit"
src="
/BookStore/picture/buy.gif"
/form>
/DIV>
/s:
iterator>
(2)在index.jsp页面合适的位置用以下内容替换。
该服务器端strutstag的作用是嵌入newBook请求的响应内容:
executeResult="
4、运行结果
首页显示用户书籍信息如图1所示。
图1首页运行图
添加数据到购物车的运行结果如图2所示。
图2购物车运行图
四、实验思考
1、Hibernate、Spring、Struts三个框架的作用分别是什么?
Hibernate框架用于与数据库内的表进行映射,形成实体类便于数据操作;
Spring框架是对业务层代码进行处理的;
Struts框架则是用于与界面相互联系,实现数据请求的操作。
2、Book.hbm.xml、struts.xml、applicationContext.xml文件中的粗体大号字显示的部分的作用是什么?
Book.hbm.xml中的table指的是实体类所映射的数据表表名;
many-to-one表示与catalog是一对多的关系;
columnname表示与catalog所相连的字段是catalogid。
Struct.xml中的<
…使用来将用户newBook的请求指定到bookAction所对应的类进行处理。
表示处理成功后返回到/newBook_succcess.jsp。
applicationContext.xml中的<
class=…用来指定Bean的名称及所对应类。
propertyref则是用于注入一个已存在的JavaBean。
3、视图newBook_succcess.jsp文件中粗体大号字显示的部分的作用是什么?
用于向后台提交addToCart.action的post请求。
定义一个输入框。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JavaEE 实验